Update web-platform-tests to revision 58eb04cecbbec2e18531ab440225e38944a9c444

This commit is contained in:
Josh Matthews 2017-04-17 12:06:02 +10:00 committed by Anthony Ramine
parent 25e8bf69e6
commit 665817d2a6
35333 changed files with 1818077 additions and 16036 deletions

View file

@ -0,0 +1,124 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - 304 Updates</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
{
name: 'HTTP cache updates returned headers from a Last-Modified 304.',
requests: [
{
response_headers: [
["Expires", http_date(-5000)],
["Last-Modified", http_date(-3000)],
["Test-Header", "A"]
]
},
{
response_headers: [
["Expires", http_date(-3000)],
["Last-Modified", http_date(-3000)],
["Test-Header", "B"]
],
expected_type: "lm_validated",
expected_response_headers: [
["Test-Header", "B"]
]
}
]
},
{
name: 'HTTP cache updates stored headers from a Last-Modified 304.',
requests: [
{
response_headers: [
["Expires", http_date(-5000)],
["Last-Modified", http_date(-3000)],
["Test-Header", "A"]
]
},
{
response_headers: [
["Expires", http_date(3000)],
["Last-Modified", http_date(-3000)],
["Test-Header", "B"]
],
expected_type: "lm_validated",
expected_response_headers: [
["Test-Header", "B"]
]
},
{
expected_type: "cached",
expected_response_headers: [
["Test-Header", "B"]
]
}
]
},
{
name: 'HTTP cache updates returned headers from a ETag 304.',
requests: [
{
response_headers: [
["Expires", http_date(-5000)],
["ETag", "ABC"],
["Test-Header", "A"]
]
},
{
response_headers: [
["Expires", http_date(-3000)],
["ETag", "ABC"],
["Test-Header", "B"]
],
expected_type: "etag_validated",
expected_response_headers: [
["Test-Header", "B"]
]
}
]
},
{
name: 'HTTP cache updates stored headers from a ETag 304.',
requests: [
{
response_headers: [
["Expires", http_date(-5000)],
["ETag", "DEF"],
["Test-Header", "A"]
]
},
{
response_headers: [
["Expires", http_date(3000)],
["ETag", "DEF"],
["Test-Header", "B"]
],
expected_type: "etag_validated",
expected_response_headers: [
["Test-Header", "B"]
]
},
{
expected_type: "cached",
expected_response_headers: [
["Test-Header", "B"]
]
}
]
},
];
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,24 @@
## HTTP Caching Tests
These tests cover HTTP-specified behaviours for caches, primarily from
[RFC7234](http://httpwg.org/specs/rfc7234.html), but as seen through the
lens of Fetch.
A few notes:
* By its nature, caching is optional; some tests expecting a response to be
cached might fail because the client chose not to cache it, or chose to
race the cache with a network request.
* Likewise, some tests might fail because there is a separate document-level
cache that's ill-defined; see [this
issue](https://github.com/whatwg/fetch/issues/354).
* [Partial content tests](partial.html) (a.k.a. Range requests) are not specified
in Fetch; tests are included here for interest only.
* Some browser caches will behave differently when reloading /
shift-reloading, despite the `cache mode` staying the same.
* At the moment, Edge doesn't appear to using HTTP caching in conjunction
with Fetch at all.

View file

@ -0,0 +1,212 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Cache-Control Request Directives</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
{
name: "HTTP cache doesn't use aged but fresh response when request contains Cache-Control: max-age=0.",
requests: [
{
template: "fresh",
pause_after: true
},
{
request_headers: [
["Cache-Control", "max-age=0"]
],
expected_type: "not_cached",
}
]
},
{
name: "HTTP cache doesn't use aged but fresh response when request contains Cache-Control: max-age=1.",
requests: [
{
template: "fresh",
pause_after: true
},
{
request_headers: [
["Cache-Control", "max-age=1"]
],
expected_type: "not_cached",
}
]
},
{
name: "HTTP cache doesn't use fresh response with Age header when request contains Cache-Control: max-age that is greater than remaining freshness.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
["Age", "1800"]
]
},
{
request_headers: [
["Cache-Control", "max-age=600"]
],
expected_type: "not_cached",
}
]
},
{
name: "HTTP cache does use aged stale response when request contains Cache-Control: max-stale that permits its use.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=1"]
],
pause_after: true
},
{
request_headers: [
["Cache-Control", "max-stale=1000"]
],
expected_type: "cached",
}
]
},
{
name: "HTTP cache does reuse stale response with Age header when request contains Cache-Control: max-stale that permits its use.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=1500"],
["Age", "2000"]
]
},
{
request_headers: [
["Cache-Control", "max-stale=1000"]
],
expected_type: "cached",
}
]
},
{
name: "HTTP cache doesn't reuse fresh response when request contains Cache-Control: min-fresh that wants it fresher.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=1500"]
]
},
{
request_headers: [
["Cache-Control", "min-fresh=2000"]
],
expected_type: "not_cached",
}
]
},
{
name: "HTTP cache doesn't reuse fresh response with Age header when request contains Cache-Control: min-fresh that wants it fresher.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=1500"],
["Age", "1000"]
]
},
{
request_headers: [
["Cache-Control", "min-fresh=1000"]
],
expected_type: "not_cached",
}
]
},
{
name: "HTTP cache doesn't reuse fresh response when request contains Cache-Control: no-cache.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
]
},
{
request_headers: [
["Cache-Control", "no-cache"]
],
expected_type: "not_cached",
}
]
},
{
name: "HTTP cache validates fresh response with Last-Modified when request contains Cache-Control: no-cache.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
["Last-Modified", http_date(-10000)]
]
},
{
request_headers: [
["Cache-Control", "no-cache"]
],
expected_type: "lm_validate",
}
]
},
{
name: "HTTP cache validates fresh response with ETag when request contains Cache-Control: no-cache.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
["ETag", http_content("abc")]
]
},
{
request_headers: [
["Cache-Control", "no-cache"]
],
expected_type: "etag_validate",
}
]
},
{
name: "HTTP cache doesn't reuse fresh response when request contains Cache-Control: no-store.",
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
]
},
{
request_headers: [
["Cache-Control", "no-store"]
],
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache generates 504 status code when nothing is in cache and request contains Cache-Control: only-if-cached.',
requests: [
{
request_headers: [
["Cache-Control", "only-if-cached"]
],
expected_status: 504
}
]
}
];
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,226 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Freshness</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
// response directives
{
name: 'HTTP cache reuses a response with a future Expires.',
requests: [
{
response_headers: [
["Expires", http_date(30*24*60*60)]
]
},
{
expected_type: "cached",
}
]
},
{
name: 'HTTP cache does not reuse a response with a past Expires.',
requests: [
{
response_headers: [
["Expires", http_date(-30*24*60*60)]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache does not reuse a response with a present Expires.',
requests: [
{
response_headers: [
["Expires", http_date(0)]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache does not reuse a response with an invalid Expires.',
requests: [
{
response_headers: [
["Expires", "0"]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache reuses a response with positive Cache-Control: max-age.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"]
]
},
{
expected_type: "cached",
}
]
},
{
name: 'HTTP cache does not reuse a response with Cache-Control: max-age=0.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=0"]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache reuses a response with positive Cache-Control: max-age and a past Expires.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
['Expires', http_date(-10000)]
]
},
{
expected_type: "cached",
}
]
},
{
name: 'HTTP cache reuses a response with positive Cache-Control: max-age and an invalid Expires.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
['Expires', '0']
]
},
{
expected_type: "cached",
}
]
},
{
name: 'HTTP cache does not reuse a response with Cache-Control: max-age=0 and a future Expires.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=0"],
['Expires', http_date(10000)]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache does not prefer Cache-Control: s-maxage over Cache-Control: max-age.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=1, s-maxage=3600"]
],
pause_after: true,
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache does not reuse a response when the Age header is greater than its freshness lifetime.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=3600"],
["Age", "12000"]
],
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache does not store a response with Cache-Control: no-store.',
requests: [
{
response_headers: [
["Cache-Control", "no-store"]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache does not store a response with Cache-Control: no-store, even with max-age and Expires.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=10000, no-store"],
['Expires', http_date(10000)]
]
},
{
expected_type: "not_cached",
}
]
},
{
name: 'HTTP cache stores a response with Cache-Control: no-cache, but revalidates upon use.',
requests: [
{
response_headers: [
["Cache-Control", "no-cache"],
['ETag', 'abcd']
]
},
{
expected_type: "etag_validated",
}
]
},
{
name: 'HTTP cache stores a response with Cache-Control: no-cache, but revalidates upon use, even with max-age and Expires.',
requests: [
{
response_headers: [
["Cache-Control", "max-age=10000, no-cache"],
['Expires', http_date(10000)],
['ETag', 'abcd']
]
},
{
expected_type: "etag_validated",
}
]
},
];
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,104 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Heuristic Freshness</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
{
name: 'HTTP cache reuses an unknown response with Last-Modified based upon heuristic freshness when Cache-Control: public is present.',
requests: [
{
response_status: [299, "Whatever"],
response_headers: [
['Last-Modified', http_date(-3 * 100)],
['Cache-Control', 'public']
],
},
{
expected_type: "cached",
}
]
},
{
name: 'HTTP cache does not reuse an unknown response with Last-Modified based upon heuristic freshness when Cache-Control: public is not present.',
requests: [
{
response_status: [299, "Whatever"],
response_headers: [
['Last-Modified', http_date(-3 * 100)],
['Cache-Control', 'public']
],
},
{
expected_type: "not_cached",
}
]
}
];
function check_status(status) {
var succeed = status[0];
var code = status[1];
var phrase = status[2];
var body = status[3];
if (body === undefined) {
body = http_content(code);
}
var expected_type = "not_cached";
var desired = "does not use"
if (succeed === true) {
expected_type = "cached";
desired = "reuses";
}
tests.push(
{
name: 'HTTP cache ' + desired + ' a ' + code + ' ' + phrase + ' response with Last-Modified based upon heuristic freshness.',
requests: [
{
response_status: [code, phrase],
response_headers: [
['Last-Modified', http_date(-3 * 100)]
],
response_body: body,
},
{
expected_type: expected_type,
response_status: [code, phrase],
response_body: body,
}
]
}
)
}
[
[true, 200, 'OK'],
[true, 203, "Non-Authoritative Information"],
[true, 204, "No Content", ""],
[true, 404, "Not Found"],
[true, 405, "Method Not Allowed"],
[true, 410, "Gone"],
[true, 414, "URI Too Long"],
[true, 501, "Not Implemented"]
].forEach(check_status);
[
[false, 201, 'Created'],
[false, 202, "Accepted"],
[false, 403, "Forbidden"],
[false, 502, "Bad Gateway"],
[false, 503, "Service Unavailable"],
[false, 504, "Gateway Timeout"],
].forEach(check_status);
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,285 @@
/**
* Each test run gets its own URL and randomized content and operates independently.
*
* Tests are an array of objects, each representing a request to make and check.
* The cache.py server script stashes an entry containing observed headers for
* each request it receives. When the test fetches have run, this state is retrieved
* and the expected_* lists are checked, including their length.
*
* Request object keys:
* - template - A template object for the request, by name -- see "templates" below.
* - request_method - A string containing the HTTP method to be used.
* - request_headers - An array of [header_name_string, header_value_string] arrays to
* emit in the request.
* - request_body - A string to use as the request body.
* - mode - The mode string to pass to fetch().
* - credentials - The credentials string to pass to fetch().
* - cache - The cache string to pass to fetch().
* - pause_after - Boolean controlling a 3-second pause after the request completes.
* - response_status - A [number, string] array containing the HTTP status code
* and phrase to return.
* - response_headers - An array of [header_name_string, header_value_string] arrays to
* emit in the response. These values will also be checked like
* expected_response_headers, unless there is a third value that is
* false.
* - response_body - String to send as the response body. If not set, it will contain
* the test identifier.
* - expected_type - One of ["cached", "not_cached", "lm_validate", "etag_validate", "error"]
* - expected_status - A number representing a HTTP status code to check the response for.
* If not set, the value of response_status[0] will be used; if that
* is not set, 200 will be used.
* - expected_request_headers - An array of [header_name_string, header_value_string] representing
* headers to check the request for.
* - expected_response_headers - An array of [header_name_string, header_value_string] representing
* headers to check the response for. See also response_headers.
* - expected_response_text - A string to check the response body against.
*/
function make_url(uuid, requests, idx) {
var arg = "";
if ("query_arg" in requests[idx]) {
arg = "&target=" + requests[idx].query_arg;
}
return "resources/http-cache.py?token=" + uuid + "&info=" + btoa(JSON.stringify(requests)) + arg;
}
function server_state(uuid) {
return fetch("resources/http-cache.py?querystate&token=" + uuid)
.then(function(response) {
return response.text();
}).then(function(text) {
// null will be returned if the server never received any requests
// for the given uuid. Normalize that to an empty list consistent
// with our representation.
return JSON.parse(text) || [];
});
}
templates = {
"fresh": {
"response_headers": [
['Expires', http_date(100000)],
['Last-Modified', http_date(0)]
]
},
"stale": {
"response_headers": [
['Expires', http_date(-5000)],
['Last-Modified', http_date(-100000)]
]
},
"lcl_response": {
"response_headers": [
['Location', "location_target"],
['Content-Location', "content_location_target"]
]
},
"location": {
"query_arg": "location_target",
"response_headers": [
['Expires', http_date(100000)],
['Last-Modified', http_date(0)]
]
},
"content_location": {
"query_arg": "content_location_target",
"response_headers": [
['Expires', http_date(100000)],
['Last-Modified', http_date(0)]
]
}
}
function make_test(raw_requests) {
var requests = [];
for (var i = 0; i < raw_requests.length; i++) {
var request = raw_requests[i];
if ("template" in request) {
var template = templates[request["template"]];
for (var member in template) {
if (! request.hasOwnProperty(member)) {
request[member] = template[member];
}
}
}
if ("expected_type" in request && request.expected_type === "cached") {
// requests after one that's expected to be cached will get out of sync
// with the server; not currently supported.
if (raw_requests.length > i + 1) {
assert_unreached("Making requests after something is expected to be cached.");
}
}
requests.push(request);
}
return function(test) {
var uuid = token();
var fetch_functions = [];
for (var i = 0; i < requests.length; ++i) {
fetch_functions.push({
code: function(idx) {
var init = {};
var url = make_url(uuid, requests, idx);
var config = requests[idx];
if ("request_method" in config) {
init.method = config["request_method"];
}
if ("request_headers" in config) {
init.headers = config["request_headers"];
}
if ("request_body" in config) {
init.body = config["request_body"];
}
if ("mode" in config) {
init.mode = config["mode"];
}
if ("credentials" in config) {
init.mode = config["credentials"];
}
if ("cache" in config) {
init.cache = config["cache"];
}
return fetch(url, init)
.then(function(response) {
var res_num = parseInt(response.headers.get("Server-Request-Count"));
var req_num = idx + 1;
if ("expected_type" in config) {
if (config.expected_type === "error") {
assert_true(false, "Request " + req_num + " should have been an error");
return [response.text(), response_status];
}
if (config.expected_type === "cached") {
assert_less_than(res_num, req_num, "Response used");
}
if (config.expected_type === "not_cached") {
assert_equals(res_num, req_num, "Response used");
}
}
if ("expected_status" in config) {
assert_equals(response.status, config.expected_status, "Response status");
} else if ("response_status" in config) {
assert_equals(response.status, config.response_status[0], "Response status");
} else {
assert_equals(response.status, 200, "Response status")
}
if ("response_headers" in config) {
config.response_headers.forEach(function(header) {
if (header.len < 3 || header[2] === true) {
assert_equals(response.headers.get(header[0]), header[1], "Response header")
}
})
}
if ("expected_response_headers" in config) {
config.expected_response_headers.forEach(function(header) {
assert_equals(response.headers.get(header[0]), header[1], "Response header");
});
}
return response.text();
}).then(function(res_body) {
if ("expected_response_text" in config) {
assert_equals(res_body, config.expected_response_text, "Response body");
} else if ("response_body" in config) {
assert_equals(res_body, config.response_body, "Response body");
} else {
assert_equals(res_body, uuid, "Response body");
}
}, function(reason) {
if ("expected_type" in config && config.expected_type === "error") {
assert_throws(new TypeError(), function() { throw reason; });
} else {
throw reason;
}
});
},
pause_after: "pause_after" in requests[i] && true || false
});
}
function pause() {
return new Promise(function(resolve, reject) {
step_timeout(function() {
return resolve()
}, 3000);
});
}
// TODO: it would be nice if this weren't serialised.
var idx = 0;
function run_next_step() {
if (fetch_functions.length) {
var fetch_function = fetch_functions.shift();
if (fetch_function.pause_after > 0) {
return fetch_function.code(idx++)
.then(pause)
.then(run_next_step);
} else {
return fetch_function.code(idx++)
.then(run_next_step);
}
} else {
return Promise.resolve();
}
}
return run_next_step()
.then(function() {
// Now, query the server state
return server_state(uuid);
}).then(function(state) {
for (var i = 0; i < requests.length; ++i) {
var expected_validating_headers = []
var req_num = i + 1;
if ("expected_type" in requests[i]) {
if (requests[i].expected_type === "cached") {
assert_true(state.length <= i, "cached response used for request " + req_num);
continue; // the server will not see the request, so we can't check anything else.
}
if (requests[i].expected_type === "not_cached") {
assert_false(state.length <= i, "cached response used for request " + req_num);
}
if (requests[i].expected_type === "etag_validated") {
expected_validating_headers.push('if-none-match')
}
if (requests[i].expected_type === "lm_validated") {
expected_validating_headers.push('if-modified-since')
}
}
for (var j in expected_validating_headers) {
var vhdr = expected_validating_headers[j];
assert_own_property(state[i].request_headers, vhdr, " has " + vhdr + " request header");
}
if ("expected_request_headers" in requests[i]) {
var expected_request_headers = requests[i].expected_request_headers;
for (var j = 0; j < expected_request_headers.length; ++j) {
var expected_header = expected_request_headers[j];
assert_equals(state[i].request_headers[expected_header[0].toLowerCase()],
expected_header[1]);
}
}
}
});
};
}
function run_tests(tests)
{
tests.forEach(function(info) {
promise_test(make_test(info.requests), info.name);
});
}
function http_date(delta) {
return new Date(Date.now() + (delta * 1000)).toGMTString();
}
var content_store = {};
function http_content(cs_key) {
if (cs_key in content_store) {
return content_store[cs_key];
} else {
var content = btoa(Math.random() * Date.now());
content_store[cs_key] = content;
return content;
}
}

View file

@ -0,0 +1,245 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Invalidation</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
{
name: 'HTTP cache invalidates after a successful response from a POST',
requests: [
{
template: "fresh"
}, {
request_method: "POST",
request_body: "abc",
}, {
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache does not invalidate after a failed response from an unsafe request',
requests: [
{
template: "fresh"
}, {
request_method: "POST",
request_body: "abc",
response_status: [500, "Internal Server Error"]
}, {
expected_type: "cached"
}
]
},
{
name: 'HTTP cache invalidates after a successful response from a PUT',
requests: [
{
template: "fresh"
}, {
template: "fresh",
request_method: "PUT",
request_body: "abc",
}, {
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates after a successful response from a DELETE',
requests: [
{
template: "fresh"
}, {
request_method: "DELETE",
request_body: "abc",
}, {
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates after a successful response from an unknown method',
requests: [
{
template: "fresh"
}, {
request_method: "FOO",
request_body: "abc",
}, {
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates Location URL after a successful response from a POST',
requests: [
{
template: "location"
}, {
request_method: "POST",
request_body: "abc",
template: "lcl_response"
}, {
template: "location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache does not invalidate Location URL after a failed response from an unsafe request',
requests: [
{
template: "location"
}, {
template: "lcl_response",
request_method: "POST",
request_body: "abc",
response_status: [500, "Internal Server Error"]
}, {
template: "location",
expected_type: "cached"
}
]
},
{
name: 'HTTP cache invalidates Location URL after a successful response from a PUT',
requests: [
{
template: "location"
}, {
template: "lcl_response",
request_method: "PUT",
request_body: "abc",
}, {
template: "location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates Location URL after a successful response from a DELETE',
requests: [
{
template: "location"
}, {
template: "lcl_response",
request_method: "DELETE",
request_body: "abc",
}, {
template: "location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates Location URL after a successful response from an unknown method',
requests: [
{
template: "location"
}, {
template: "lcl_response",
request_method: "FOO",
request_body: "abc",
}, {
template: "location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates Content-Location URL after a successful response from a POST',
requests: [
{
template: "content_location"
}, {
request_method: "POST",
request_body: "abc",
template: "lcl_response"
}, {
template: "content_location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache does not invalidate Content-Location URL after a failed response from an unsafe request',
requests: [
{
template: "content_location"
}, {
template: "lcl_response",
request_method: "POST",
request_body: "abc",
response_status: [500, "Internal Server Error"]
}, {
template: "content_location",
expected_type: "cached"
}
]
},
{
name: 'HTTP cache invalidates Content-Location URL after a successful response from a PUT',
requests: [
{
template: "content_location"
}, {
template: "lcl_response",
request_method: "PUT",
request_body: "abc",
}, {
template: "content_location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates Content-Location URL after a successful response from a DELETE',
requests: [
{
template: "content_location"
}, {
template: "lcl_response",
request_method: "DELETE",
request_body: "abc",
}, {
template: "content_location",
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache invalidates Content-Location URL after a successful response from an unknown method',
requests: [
{
template: "content_location"
}, {
template: "lcl_response",
request_method: "FOO",
request_body: "abc",
}, {
template: "content_location",
expected_type: "not_cached"
}
]
}
];
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,109 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Partial Content</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
{
name: 'HTTP cache stores partial content and reuses it.',
requests: [
{
request_headers: [
['Range', "bytes=-5"]
],
response_status: [206, "Partial Content"],
response_headers: [
['Cache-Control', 'max-age=3600'],
['Content-Range', 'bytes 0-4/10']
],
response_body: "01234",
expected_request_headers: [
['Range', "bytes=-5"]
],
},
{
request_headers: [
['Range', "bytes=-5"]
],
expected_type: "cached",
expected_status: 206
}
]
},
{
name: 'HTTP cache stores complete response and serves smaller ranges from it.',
requests: [
{
response_headers: [
['Cache-Control', 'max-age=3600'],
],
response_body: "01234567890",
},
{
request_headers: [
['Range', "bytes=-1"]
],
expected_type: "cached",
expected_response_text: "01"
}
]
},
{
name: 'HTTP cache stores partial response and serves smaller ranges from it.',
requests: [
{
request_headers: [
['Range', "bytes=-5"]
],
response_status: [206, "Partial Content"],
response_headers: [
['Cache-Control', 'max-age=3600'],
['Content-Range', 'bytes 0-4/10']
],
response_body: "01234",
},
{
request_headers: [
['Range', "bytes=-1"]
],
expected_type: "cached",
expected_response_text: "01"
}
]
},
{
name: 'HTTP cache stores partial content and completes it.',
requests: [
{
request_headers: [
['Range', "bytes=-5"]
],
response_status: [206, "Partial Content"],
response_headers: [
['Cache-Control', 'max-age=3600'],
['Content-Range', 'bytes 0-4/10']
],
response_body: "01234",
},
{
expected_request_headers: [
["range", "bytes=5-"]
]
}
]
},
];
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,51 @@
from json import JSONEncoder, JSONDecoder
from base64 import b64decode
def main(request, response):
uuid = request.GET.first("token", None)
if "querystate" in request.GET:
response.headers.set("Content-Type", "text/plain")
return JSONEncoder().encode(request.server.stash.take(uuid))
server_state = request.server.stash.take(uuid)
if not server_state:
server_state = []
requests = JSONDecoder().decode(b64decode(request.GET.first("info", "")))
config = requests[len(server_state)]
state = dict()
state["request_method"] = request.method
state["request_headers"] = dict([[h.lower(), request.headers[h]] for h in request.headers])
server_state.append(state)
request.server.stash.put(uuid, server_state)
note_headers = ['content-type', 'access-control-allow-origin', 'last-modified', 'etag']
noted_headers = {}
for header in config.get('response_headers', []):
if header[0].lower() in ["location", "content-location"]: # magic!
header[1] = "%s&target=%s" % (request.url, header[1])
response.headers.set(header[0], header[1])
if header[0].lower() in note_headers:
noted_headers[header[0].lower()] = header[1]
if "access-control-allow-origin" not in noted_headers:
response.headers.set("Access-Control-Allow-Origin", "*");
if "content-type" not in noted_headers:
response.headers.set("Content-Type", "text/plain")
response.headers.set("Server-Request-Count", len(server_state))
code, phrase = config.get("response_status", [200, "OK"])
if request.headers.get("If-Modified-Since", False) == noted_headers.get('last-modified', True):
code, phrase = [304, "Not Modified"]
if request.headers.get("If-None-Match", False) == noted_headers.get('etag', True):
code, phrase = [304, "Not Modified"]
response.status = (code, phrase)
content = config.get("response_body", uuid)
if code in [204, 304]:
return ""
else:
return content

View file

@ -0,0 +1,69 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Status Codes</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [];
function check_status(status) {
var code = status[0];
var phrase = status[1];
var body = status[2];
if (body === undefined) {
body = http_content(code);
}
tests.push({
name: 'HTTP cache goes to the network if it has a stale ' + code + ' response.',
requests: [
{
template: "stale",
response_status: [code, phrase],
response_body: body
}, {
expected_type: "not_cached",
response_body: body
}
]
})
tests.push({
name: 'HTTP cache avoids going to the network if it has a fresh ' + code + ' response.',
requests: [
{
template: "fresh",
response_status: [code, phrase],
response_body: body
}, {
expected_type: "cached",
response_status: [code, phrase],
response_body: body
}
]
})
}
[
[200, 'OK'],
[203, "Non-Authoritative Information"],
[204, "No Content", ""],
[299, "Whatever"],
[400, "Bad Request"],
[404, "Not Found"],
[410, "Gone"],
[499, "Whatever"],
[500, "Internal Server Error"],
[502, "Bad Gateway"],
[503, "Service Unavailable"],
[504, "Gateway Timeout"],
[599, "Whatever"]
].forEach(check_status);
run_tests(tests);
</script>
</body>
</html>

View file

@ -0,0 +1,270 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTTP Cache - Vary</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="http-cache.js"></script>
</head>
<body>
<script>
var tests = [
{
name: 'HTTP cache reuses Vary response when request matches.',
requests: [
{
request_headers: [
["Foo", "1"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo"]
]
},
{
request_headers: [
["Foo", "1"]
],
expected_type: "cached"
}
]
},
{
name: "HTTP cache doesn't use Vary response when request doesn't match.",
requests: [
{
request_headers: [
["Foo", "1"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo"]
]
},
{
request_headers: [
["Foo", "2"]
],
expected_type: "not_cached"
}
]
},
{
name: "HTTP cache doesn't use Vary response when request omits variant header.",
requests: [
{
request_headers: [
["Foo", "1"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo"]
]
},
{
expected_type: "not_cached"
}
]
},
{
name: "HTTP cache doesn't invalidate existing Vary response.",
requests: [
{
request_headers: [
["Foo", "1"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo"]
],
response_body: http_content('foo_1')
},
{
request_headers: [
["Foo", "2"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo"]
],
expected_type: "not_cached",
response_body: http_content('foo_2'),
},
{
request_headers: [
["Foo", "1"]
],
expected_type: "cached"
}
]
},
{
name: "HTTP cache doesn't pay attention to headers not listed in Vary.",
requests: [
{
request_headers: [
["Foo", "1"],
["Other", "2"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo"]
],
},
{
request_headers: [
["Foo", "1"],
["Other", "3"]
],
expected_type: "cached",
}
]
},
{
name: 'HTTP cache reuses two-way Vary response when request matches.',
requests: [
{
request_headers: [
["Foo", "1"],
["Bar", "abc"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo, Bar"]
]
},
{
request_headers: [
["Foo", "1"],
["Bar", "abc"]
],
expected_type: "cached"
}
]
},
{
name: "HTTP cache doesn't use two-way Vary response when request doesn't match.",
requests: [
{
request_headers: [
["Foo", "1"],
["Bar", "abc"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo, Bar"]
]
},
{
request_headers: [
["Foo", "2"],
["Bar", "abc"]
],
expected_type: "not_cached"
}
]
},
{
name: "HTTP cache doesn't use two-way Vary response when request omits variant header.",
requests: [
{
request_headers: [
["Foo", "1"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo, Bar"]
]
},
{
expected_type: "not_cached"
}
]
},
{
name: 'HTTP cache reuses three-way Vary response when request matches.',
requests: [
{
request_headers: [
["Foo", "1"],
["Bar", "abc"],
["Baz", "789"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo, Bar, Baz"]
]
},
{
request_headers: [
["Foo", "1"],
["Bar", "abc"],
["Baz", "789"]
],
expected_type: "cached"
}
]
},
{
name: "HTTP cache doesn't use three-way Vary response when request doesn't match.",
requests: [
{
request_headers: [
["Foo", "1"],
["Bar", "abc"],
["Baz", "789"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo, Bar, Baz"]
]
},
{
request_headers: [
["Foo", "2"],
["Bar", "abc"],
["Baz", "789"]
],
expected_type: "not_cached"
}
]
},
{
name: "HTTP cache doesn't use three-way Vary response when request omits variant header.",
requests: [
{
request_headers: [
["Foo", "1"],
["Baz", "789"]
],
response_headers: [
["Expires", http_date(5000)],
["Last-Modified", http_date(-3000)],
["Vary", "Foo, Bar, Baz"]
]
},
{
expected_type: "not_cached"
}
]
}
];
run_tests(tests);
</script>
</body>
</html>