OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.hijack_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:test/test.dart'; |
| 10 import 'package:shelf/shelf.dart'; |
| 11 |
| 12 import 'test_util.dart'; |
| 13 |
| 14 void main() { |
| 15 test('hijacking a non-hijackable request throws a StateError', () { |
| 16 expect(() => new Request('GET', LOCALHOST_URI).hijack((_, __) => null), |
| 17 throwsStateError); |
| 18 }); |
| 19 |
| 20 test('hijacking a hijackable request throws a HijackException and calls ' |
| 21 'onHijack', () { |
| 22 var request = new Request('GET', LOCALHOST_URI, |
| 23 onHijack: expectAsync((callback) { |
| 24 var streamController = new StreamController(); |
| 25 streamController.add([1, 2, 3]); |
| 26 streamController.close(); |
| 27 |
| 28 var sinkController = new StreamController(); |
| 29 expect(sinkController.stream.first, completion(equals([4, 5, 6]))); |
| 30 |
| 31 callback(streamController.stream, sinkController); |
| 32 })); |
| 33 |
| 34 expect(() => request.hijack(expectAsync((stream, sink) { |
| 35 expect(stream.first, completion(equals([1, 2, 3]))); |
| 36 sink.add([4, 5, 6]); |
| 37 sink.close(); |
| 38 })), throwsA(new isInstanceOf<HijackException>())); |
| 39 }); |
| 40 |
| 41 test('hijacking a hijackable request twice throws a StateError', () { |
| 42 // Assert that the [onHijack] callback is only called once. |
| 43 var request = new Request('GET', LOCALHOST_URI, |
| 44 onHijack: expectAsync((_) => null, count: 1)); |
| 45 |
| 46 expect(() => request.hijack((_, __) => null), |
| 47 throwsA(new isInstanceOf<HijackException>())); |
| 48 |
| 49 expect(() => request.hijack((_, __) => null), throwsStateError); |
| 50 }); |
| 51 |
| 52 group('calling change', () { |
| 53 test('hijacking a non-hijackable request throws a StateError', () { |
| 54 var request = new Request('GET', LOCALHOST_URI); |
| 55 var newRequest = request.change(); |
| 56 expect(() => newRequest.hijack((_, __) => null), throwsStateError); |
| 57 }); |
| 58 |
| 59 test('hijacking a hijackable request throws a HijackException and calls ' |
| 60 'onHijack', () { |
| 61 var request = new Request('GET', LOCALHOST_URI, |
| 62 onHijack: expectAsync((callback) { |
| 63 var streamController = new StreamController(); |
| 64 streamController.add([1, 2, 3]); |
| 65 streamController.close(); |
| 66 |
| 67 var sinkController = new StreamController(); |
| 68 expect(sinkController.stream.first, completion(equals([4, 5, 6]))); |
| 69 |
| 70 callback(streamController.stream, sinkController); |
| 71 })); |
| 72 |
| 73 var newRequest = request.change(); |
| 74 |
| 75 expect(() => newRequest.hijack(expectAsync((stream, sink) { |
| 76 expect(stream.first, completion(equals([1, 2, 3]))); |
| 77 sink.add([4, 5, 6]); |
| 78 sink.close(); |
| 79 })), throwsA(new isInstanceOf<HijackException>())); |
| 80 }); |
| 81 |
| 82 test('hijacking the original request after calling change throws a ' |
| 83 'StateError', () { |
| 84 // Assert that the [onHijack] callback is only called once. |
| 85 var request = new Request('GET', LOCALHOST_URI, |
| 86 onHijack: expectAsync((_) => null, count: 1)); |
| 87 |
| 88 var newRequest = request.change(); |
| 89 |
| 90 expect(() => newRequest.hijack((_, __) => null), |
| 91 throwsA(new isInstanceOf<HijackException>())); |
| 92 |
| 93 expect(() => request.hijack((_, __) => null), throwsStateError); |
| 94 }); |
| 95 }); |
| 96 } |
OLD | NEW |