pkg/shelf: include the original `onHijack` callback for Request.change
BUG= https://code.google.com/p/dart/issues/detail?id=21043
R=nweiz@google.com
Review URL: https://codereview.chromium.org//616463004
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/shelf@41102 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d99158d..d49e252 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,12 @@
+## 0.5.5+1
+
+* Updated `Request.change` to include the original `onHijack` callback if one
+ exists.
+
## 0.5.5
* Added default body text for `Response.forbidden` and `Response.notFound` if
-null is provided.
+ null is provided.
* Clarified documentation on a number of `Response` constructors.
diff --git a/lib/src/request.dart b/lib/src/request.dart
index 26fe011..15d8b87 100644
--- a/lib/src/request.dart
+++ b/lib/src/request.dart
@@ -116,16 +116,31 @@
///
/// See also [hijack].
// TODO(kevmoo) finish documenting the rest of the arguments.
- Request(this.method, Uri requestedUri, {String protocolVersion,
+ Request(String method, Uri requestedUri, {String protocolVersion,
+ Map<String, String> headers, Uri url, String scriptName,
+ Stream<List<int>> body, Map<String, Object> context,
+ OnHijackCallback onHijack})
+ : this._(method, requestedUri, protocolVersion: protocolVersion,
+ headers: headers, url: url, scriptName: scriptName,
+ body: body, context: context,
+ onHijack: onHijack == null ? null : new _OnHijack(onHijack));
+
+ /// This constructor has the same signature as [new Request] except that
+ /// accepts [onHijack] as [_OnHijack].
+ ///
+ /// Any [Request] created by calling [change] will pass [_onHijack] from the
+ /// source [Request] to ensure that [hijack] can only be called once, even
+ /// from a changed [Request].
+ Request._(this.method, Uri requestedUri, {String protocolVersion,
Map<String, String> headers, Uri url, String scriptName,
Stream<List<int>> body, Map<String, Object> context,
- OnHijackCallback onHijack})
+ _OnHijack onHijack})
: this.requestedUri = requestedUri,
this.protocolVersion = protocolVersion == null ?
'1.1' : protocolVersion,
this.url = _computeUrl(requestedUri, url, scriptName),
this.scriptName = _computeScriptName(requestedUri, url, scriptName),
- this._onHijack = onHijack == null ? null : new _OnHijack(onHijack),
+ this._onHijack = onHijack,
super(body == null ? new Stream.fromIterable([]) : body,
headers: headers, context: context) {
if (method.isEmpty) throw new ArgumentError('method cannot be empty.');
@@ -194,9 +209,10 @@
if (url == null) url = this.url;
if (scriptName == null) scriptName = this.scriptName;
- return new Request(this.method, this.requestedUri,
+ return new Request._(this.method, this.requestedUri,
protocolVersion: this.protocolVersion, headers: headers, url: url,
- scriptName: scriptName, body: this.read(), context: context);
+ scriptName: scriptName, body: this.read(), context: context,
+ onHijack: _onHijack);
}
/// Takes control of the underlying request socket.
diff --git a/pubspec.yaml b/pubspec.yaml
index d4b6c09..955b4fa 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: shelf
-version: 0.5.5
+version: 0.5.5+1
author: Dart Team <misc@dartlang.org>
description: Web Server Middleware for Dart
homepage: http://www.dartlang.org
diff --git a/test/hijack_test.dart b/test/hijack_test.dart
index fa67a6d..ee4255a 100644
--- a/test/hijack_test.dart
+++ b/test/hijack_test.dart
@@ -48,4 +48,50 @@
expect(() => request.hijack((_, __) => null), throwsStateError);
});
+
+ group('calling change', () {
+ test('hijacking a non-hijackable request throws a StateError', () {
+ var request = new Request('GET', LOCALHOST_URI);
+ var newRequest = request.change();
+ expect(() => newRequest.hijack((_, __) => null),
+ throwsStateError);
+ });
+
+ test('hijacking a hijackable request throws a HijackException and calls '
+ 'onHijack', () {
+ var request = new Request('GET', LOCALHOST_URI,
+ onHijack: expectAsync((callback) {
+ var streamController = new StreamController();
+ streamController.add([1, 2, 3]);
+ streamController.close();
+
+ var sinkController = new StreamController();
+ expect(sinkController.stream.first, completion(equals([4, 5, 6])));
+
+ callback(streamController.stream, sinkController);
+ }));
+
+ var newRequest = request.change();
+
+ expect(() => newRequest.hijack(expectAsync((stream, sink) {
+ expect(stream.first, completion(equals([1, 2, 3])));
+ sink.add([4, 5, 6]);
+ sink.close();
+ })), throwsA(new isInstanceOf<HijackException>()));
+ });
+
+ test('hijacking the original request after calling change throws a '
+ 'StateError', () {
+ // Assert that the [onHijack] callback is only called once.
+ var request = new Request('GET', LOCALHOST_URI,
+ onHijack: expectAsync((_) => null, count: 1));
+
+ var newRequest = request.change();
+
+ expect(() => newRequest.hijack((_, __) => null),
+ throwsA(new isInstanceOf<HijackException>()));
+
+ expect(() => request.hijack((_, __) => null), throwsStateError);
+ });
+ });
}