@@ -9,94 +9,76 @@ import 'dart:async';
9
9
10
10
import 'package:http/browser_client.dart' ;
11
11
import 'package:http/http.dart' as http;
12
- import 'package:http/src/exception .dart' ;
12
+ import 'package:http/http .dart' ;
13
13
import 'package:test/test.dart' ;
14
14
15
15
import 'utils.dart' ;
16
16
17
17
void main () {
18
- late int port ;
18
+ late Uri url ;
19
19
setUp (() async {
20
- final channel = spawnHybridUri (Uri (path: '/test/stub_server.dart' ),
21
- stayAlive : true ) ;
22
- port = await channel.stream.first as int ;
20
+ final channel = spawnHybridUri (Uri (path: '/test/stub_server.dart' ));
21
+ var port = await channel.stream.first as int ;
22
+ url = echoUrl. replace (port : port) ;
23
23
});
24
24
25
- test ('#send a POST Request with default type' , () async {
26
- var client = BrowserClient ();
27
-
28
- var request = http.StreamedRequest ('POST' , echoUrl.replace (port: port));
29
- var responseFuture = client.send (request);
30
- request.sink.add ('{"hello": "world"}' .codeUnits);
31
- unawaited (request.sink.close ());
32
-
33
- var response = await responseFuture;
34
-
35
- client.close ();
36
-
37
- expect (response.statusCode, 200 );
38
- expect ('response' , response.contentLength);
39
- client.close ();
40
-
41
- });
42
-
43
- test ('#send a StreamedRequest with default type' , () async {
25
+ test ('#send a GET with default type' , () async {
44
26
var client = BrowserClient (cacheMode: CacheMode .defaultType);
45
- var request = http.StreamedRequest ('POST' , echoUrl);
46
- var responseFuture = client.send (request);
47
- request.sink.add ('{"hello": "world"}' .codeUnits);
48
- unawaited (request.sink.close ());
49
-
50
- var response = await responseFuture;
51
-
27
+ await client.get (url);
28
+ var response = await client.get (url);
52
29
client.close ();
53
30
54
31
expect (response.statusCode, 200 );
55
32
expect (response.reasonPhrase, 'OK' );
56
- }, skip: 'Need to fix server tests for browser' );
33
+ expect (response.body, parse (allOf (containsPair ('numOfRequests' , 2 ))));
34
+ });
57
35
58
- test ('#send a POST Request with reload type' , () async {
36
+ test ('#send a GET Request with reload type' , () async {
59
37
var client = BrowserClient (cacheMode: CacheMode .reload);
38
+ await client.get (url);
39
+ var response = await client.get (url);
40
+ expect (response.body, parse (allOf (containsPair ('numOfRequests' , 2 ))));
41
+ client.close ();
42
+ });
60
43
61
- var responseFuture = client.post (echoUrl.replace (port: port));
44
+ test ('#send a POST with no-cache type' , () async {
45
+ var client = BrowserClient (cacheMode: CacheMode .noCache);
62
46
47
+ await client.post (url);
48
+ var response = await client.post (url);
63
49
client.close ();
50
+ expect (
51
+ response.body,
52
+ parse (anyOf (containsPair ('numOfRequests' , 2 ),
53
+ containsPair ('cache-control' , ['max-age=0' ]))));
54
+ });
64
55
65
- }, skip: 'Need to fix server tests for browser' );
56
+ test ('#send a POST with no-store type' , () async {
57
+ var client = BrowserClient (cacheMode: CacheMode .noStore);
66
58
67
- test ('#send a StreamedRequest with no-cache type' , () async {
68
- var client = BrowserClient (cacheMode: CacheMode .noCache);
69
- var request = http.StreamedRequest ('POST' , echoUrl);
59
+ await client.post (url);
60
+ var response = await client.post (url);
61
+ client.close ();
62
+ expect (response.body, parse (allOf (containsPair ('numOfRequests' , 2 ))));
63
+ });
70
64
71
- var responseFuture = client.send (request);
72
- request.sink.add ('{"hello": "world"}' .codeUnits);
73
- unawaited (request.sink.close ());
74
- var response = await responseFuture;
75
- var bytesString = await response.stream.bytesToString ();
65
+ test ('#send a POST with force-store type' , () async {
66
+ var client = BrowserClient (cacheMode: CacheMode .forceCache);
76
67
68
+ await client.post (url);
69
+ var response = await client.post (url);
77
70
client.close ();
78
- expect (bytesString, contains ( 'max-age=0' ));
79
- }, skip : 'Need to fix server tests for browser' );
71
+ expect (response.body, parse ( allOf ( containsPair ( 'numOfRequests' , 2 )) ));
72
+ });
80
73
81
74
test ('#send a StreamedRequest with only-if-cached type' , () {
82
75
var client = BrowserClient (cacheMode: CacheMode .onlyIfCached);
83
- var request = http.StreamedRequest ('POST' , echoUrl );
76
+ var request = http.StreamedRequest ('POST' , url );
84
77
85
78
expectLater (client.send (request), throwsA (isA <ClientException >()));
86
79
request.sink.add ('{"hello": "world"}' .codeUnits);
87
80
unawaited (request.sink.close ());
88
81
89
82
client.close ();
90
- }, skip: 'Need to fix server tests for browser' );
91
-
92
- test ('#send with an invalid URL' , () {
93
- var client = BrowserClient (cacheMode: CacheMode .onlyIfCached);
94
- var url = Uri .http ('http.invalid' , '' );
95
- var request = http.StreamedRequest ('POST' , url);
96
-
97
- expect (client.send (request), throwsClientException ());
98
-
99
- request.sink.add ('{"hello": "world"}' .codeUnits);
100
- request.sink.close ();
101
- }, skip: 'Need to fix server tests for browser' );
83
+ });
102
84
}
0 commit comments