|
6 | 6 | library;
|
7 | 7 |
|
8 | 8 | import 'dart:async';
|
9 |
| -import 'dart:convert'; |
10 | 9 |
|
11 | 10 | import 'package:http/browser_client.dart';
|
12 | 11 | import 'package:http/http.dart' as http;
|
| 12 | +import 'package:http/src/exception.dart'; |
13 | 13 | import 'package:test/test.dart';
|
14 | 14 |
|
15 | 15 | import 'utils.dart';
|
16 | 16 |
|
17 | 17 | void main() {
|
18 |
| - |
19 | 18 | test('#send a StreamedRequest with default type', () async {
|
20 | 19 | var client = BrowserClient(cacheMode: CacheMode.defaultType);
|
21 |
| - var request = http.StreamedRequest('POST',echoUrl); |
22 |
| - |
| 20 | + var request = http.StreamedRequest('POST', echoUrl); |
23 | 21 | var responseFuture = client.send(request);
|
24 | 22 | request.sink.add('{"hello": "world"}'.codeUnits);
|
25 | 23 | unawaited(request.sink.close());
|
| 24 | + |
26 | 25 | var response = await responseFuture;
|
27 |
| - var bytesString = await response.stream.bytesToString(); |
28 | 26 |
|
29 |
| - final jsonResponse = jsonDecode(bytesString); |
30 |
| - var bodyUnits = jsonResponse['body'] as List; |
31 | 27 | client.close();
|
32 | 28 |
|
33 |
| - expect(bodyUnits, |
34 |
| - equals('{"hello": "world"}'.codeUnits)); |
| 29 | + expect(response.statusCode, 200); |
| 30 | + expect(response.reasonPhrase, 'OK'); |
35 | 31 | }, skip: 'Need to fix server tests for browser');
|
36 | 32 |
|
37 | 33 | test('#send a StreamedRequest with reload type', () async {
|
38 | 34 | var client = BrowserClient(cacheMode: CacheMode.reload);
|
39 |
| - var request = http.StreamedRequest('POST',echoUrl); |
| 35 | + var request = http.StreamedRequest('POST', echoUrl); |
40 | 36 |
|
41 | 37 | var responseFuture = client.send(request);
|
42 | 38 | request.sink.add('{"hello": "world"}'.codeUnits);
|
43 | 39 | unawaited(request.sink.close());
|
44 | 40 | var response = await responseFuture;
|
45 | 41 | var bytesString = await response.stream.bytesToString();
|
46 | 42 |
|
47 |
| - final jsonResponse = jsonDecode(bytesString); |
48 | 43 | client.close();
|
49 | 44 |
|
50 |
| - expect(jsonResponse["headers"]["cache-control"], |
51 |
| - contains('no-cache')); |
| 45 | + expect(bytesString, contains('no-cache')); |
52 | 46 | }, skip: 'Need to fix server tests for browser');
|
53 | 47 |
|
54 | 48 | test('#send a StreamedRequest with no-cache type', () async {
|
55 | 49 | var client = BrowserClient(cacheMode: CacheMode.noCache);
|
56 |
| - var request = http.StreamedRequest('POST',echoUrl); |
| 50 | + var request = http.StreamedRequest('POST', echoUrl); |
57 | 51 |
|
58 | 52 | var responseFuture = client.send(request);
|
59 | 53 | request.sink.add('{"hello": "world"}'.codeUnits);
|
60 | 54 | unawaited(request.sink.close());
|
61 | 55 | var response = await responseFuture;
|
62 | 56 | var bytesString = await response.stream.bytesToString();
|
63 | 57 |
|
64 |
| - final jsonResponse = jsonDecode(bytesString); |
65 | 58 | client.close();
|
| 59 | + expect(bytesString, contains('max-age=0')); |
| 60 | + }, skip: 'Need to fix server tests for browser'); |
| 61 | + |
| 62 | + test('#send a StreamedRequest with only-if-cached type', () { |
| 63 | + var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); |
| 64 | + var request = http.StreamedRequest('POST', echoUrl); |
66 | 65 |
|
67 |
| - expect(jsonResponse["headers"]["cache-control"], |
68 |
| - contains('max-age=0')); |
| 66 | + expectLater(client.send(request), throwsA(isA<ClientException>())); |
| 67 | + request.sink.add('{"hello": "world"}'.codeUnits); |
| 68 | + unawaited(request.sink.close()); |
| 69 | + |
| 70 | + client.close(); |
| 71 | + }, skip: 'Need to fix server tests for browser'); |
| 72 | + |
| 73 | + test('#send with an invalid URL', () { |
| 74 | + var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); |
| 75 | + var url = Uri.http('http.invalid', ''); |
| 76 | + var request = http.StreamedRequest('POST', url); |
| 77 | + |
| 78 | + expect(client.send(request), throwsClientException()); |
| 79 | + |
| 80 | + request.sink.add('{"hello": "world"}'.codeUnits); |
| 81 | + request.sink.close(); |
69 | 82 | }, skip: 'Need to fix server tests for browser');
|
70 | 83 | }
|
0 commit comments