Skip to content

Commit cab59fc

Browse files
committed
Added some cache tests
1 parent a1f47db commit cab59fc

File tree

3 files changed

+70
-111
lines changed

3 files changed

+70
-111
lines changed

pkgs/http/test/html/cache_test.dart

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
@TestOn('browser')
6+
library;
7+
8+
import 'dart:async';
9+
import 'dart:convert';
10+
11+
import 'package:http/browser_client.dart';
12+
import 'package:http/http.dart' as http;
13+
import 'package:test/test.dart';
14+
15+
import 'utils.dart';
16+
17+
void main() {
18+
19+
test('#send a StreamedRequest with default type', () async {
20+
var client = BrowserClient(cacheMode: CacheMode.defaultType);
21+
var request = http.StreamedRequest('POST',echoUrl);
22+
23+
var responseFuture = client.send(request);
24+
request.sink.add('{"hello": "world"}'.codeUnits);
25+
unawaited(request.sink.close());
26+
var response = await responseFuture;
27+
var bytesString = await response.stream.bytesToString();
28+
29+
final jsonResponse = jsonDecode(bytesString);
30+
var bodyUnits = jsonResponse['body'] as List;
31+
client.close();
32+
33+
expect(bodyUnits,
34+
equals('{"hello": "world"}'.codeUnits));
35+
});
36+
37+
test('#send a StreamedRequest with reload type', () async {
38+
var client = BrowserClient(cacheMode: CacheMode.reload);
39+
var request = http.StreamedRequest('POST',echoUrl);
40+
41+
var responseFuture = client.send(request);
42+
request.sink.add('{"hello": "world"}'.codeUnits);
43+
unawaited(request.sink.close());
44+
var response = await responseFuture;
45+
var bytesString = await response.stream.bytesToString();
46+
47+
final jsonResponse = jsonDecode(bytesString);
48+
client.close();
49+
50+
expect(jsonResponse["headers"]["cache-control"],
51+
contains('no-cache'));
52+
});
53+
54+
test('#send a StreamedRequest with no-cache type', () async {
55+
var client = BrowserClient(cacheMode: CacheMode.noCache);
56+
var request = http.StreamedRequest('POST',echoUrl);
57+
58+
var responseFuture = client.send(request);
59+
request.sink.add('{"hello": "world"}'.codeUnits);
60+
unawaited(request.sink.close());
61+
var response = await responseFuture;
62+
var bytesString = await response.stream.bytesToString();
63+
64+
final jsonResponse = jsonDecode(bytesString);
65+
client.close();
66+
67+
expect(jsonResponse["headers"]["cache-control"],
68+
contains('max-age=0'));
69+
});
70+
}

pkgs/http/test/html/utils.dart

Lines changed: 0 additions & 11 deletions
This file was deleted.

pkgs/http/test/stub_server.dart

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)