-
Notifications
You must be signed in to change notification settings - Fork 378
Added cache control for http request within BrowserClient #1706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9ba5471
ee01325
3a26def
3ba65df
f5c9c11
52ff2c5
7f70434
ae7be97
1ca0c3a
7a5c30d
85daade
7ece5aa
4bca978
dad889c
2889cb2
3d17957
a1f47db
cab59fc
5831578
2fd11c1
53dc82f
1adce3e
7d50328
410d227
83672f8
40609bc
ff7f7e8
a63d6ce
7e1bc9f
0eabc8d
c144307
34a87fb
5294e96
d12c0a4
3c53220
b485213
f65dd4f
42251ea
4c7471f
4896114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
@TestOn('browser') | ||
library; | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:http/browser_client.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:http/http.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'utils.dart'; | ||
|
||
void main() { | ||
late Uri url; | ||
setUp(() async { | ||
final channel = spawnHybridUri(Uri(path: '/test/stub_server.dart')); | ||
var port = (await channel.stream.first as num).toInt(); | ||
url = echoUrl.replace(port: port); | ||
}); | ||
|
||
test('#send a GET with default type', () async { | ||
var client = BrowserClient(cacheMode: CacheMode.defaultType); | ||
await client.get(url); | ||
var response = await client.get(url); | ||
client.close(); | ||
|
||
expect(response.statusCode, 200); | ||
expect(response.reasonPhrase, 'OK'); | ||
expect(response.body, parse(allOf(containsPair('numOfRequests', 1)))); | ||
}); | ||
|
||
test('#send a GET Request with reload type', () async { | ||
var client = BrowserClient(cacheMode: CacheMode.reload); | ||
await client.get(url); | ||
var response = await client.get(url); | ||
expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); | ||
client.close(); | ||
}); | ||
|
||
test('#send a GET with no-cache type', () async { | ||
var client = BrowserClient(cacheMode: CacheMode.noCache); | ||
|
||
await client.get(url); | ||
var response = await client.get(url); | ||
client.close(); | ||
expect( | ||
response.body, | ||
parse(anyOf(containsPair('numOfRequests', 2), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, see most of other tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have other suggestions regarding the tests? @brianquinlan |
||
containsPair('cache-control', ['max-age=0'])))); | ||
}); | ||
|
||
test('#send a GET with no-store type', () async { | ||
var client = BrowserClient(cacheMode: CacheMode.noStore); | ||
|
||
await client.get(url); | ||
var response = await client.get(url); | ||
client.close(); | ||
expect(response.body, parse(allOf(containsPair('numOfRequests', 2)))); | ||
}); | ||
|
||
test('#send a GET with force-store type', () async { | ||
var client = BrowserClient(cacheMode: CacheMode.forceCache); | ||
|
||
await client.get(url); | ||
var response = await client.get(url); | ||
client.close(); | ||
expect(response.body, parse(allOf(containsPair('numOfRequests', 1)))); | ||
}); | ||
|
||
test('#send a StreamedRequest with only-if-cached type', () { | ||
var client = BrowserClient(cacheMode: CacheMode.onlyIfCached); | ||
var request = http.StreamedRequest('GET', url); | ||
|
||
expectLater(client.send(request), throwsA(isA<ClientException>())); | ||
unawaited(request.sink.close()); | ||
|
||
client.close(); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are only doing a single request here so I'm not sure how this is testing the caching behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, it is two GET requests, when it is set to default type it is cached once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess, I need to drain the request so, I can see the difference.