Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ignore aborted response in http caching
- Loading branch information
Showing
8 changed files
with
186 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[http-cache-xhr.html] | ||
type: testharness |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="resources/http-cache.js"></script> | ||
<script> | ||
var test = async_test('The response from an aborted XHR request should not be cached'); | ||
test.step(function() { | ||
var client = new XMLHttpRequest(); | ||
var requests = [ | ||
{ | ||
response_status: [200, 'OK'], | ||
"response_headers": [ | ||
['Expires', http_date(100000)], | ||
['Last-Modified', http_date(0)] | ||
], | ||
response_body: '12' | ||
}, | ||
{ | ||
response_status: [200, 'OK'], | ||
"response_headers": [ | ||
['Expires', http_date(100000)], | ||
['Last-Modified', http_date(0)] | ||
], | ||
response_body: '12' | ||
}, | ||
{ | ||
response_status: [200, 'OK'], | ||
"response_headers": [ | ||
['Expires', http_date(100000)], | ||
['Last-Modified', http_date(0)] | ||
], | ||
response_body: '12' | ||
} | ||
]; | ||
var uuid = token(); | ||
var url = make_url(uuid, requests, 0); | ||
url += '&trickle=true'; | ||
client.onprogress = test.step_func(function() { | ||
var client2 = new XMLHttpRequest(); | ||
client2.onloadend = test.step_func(function(event) { | ||
server_state(uuid).then(test.step_func(function(state){ | ||
// Two server hits for two requests, no caching. | ||
assert_equals(state.length, 2); | ||
// The empty, aborted response. | ||
assert_equals(client.response, ""); | ||
assert_equals(client2.response, "12"); | ||
})); | ||
var client3 = new XMLHttpRequest(); | ||
client3.onloadend = test.step_func(function(event) { | ||
server_state(uuid).then(test.step_func(function(state) { | ||
// No server hits, the response from client2 was cached and re-used. | ||
assert_equals(state.length, 0) | ||
assert_equals(client3.response, '12'); | ||
test.done(); | ||
})); | ||
}); | ||
client3.open("GET", url); | ||
client3.send(); | ||
}); | ||
client.onabort = test.step_func(function() { | ||
client2.open("GET", url); | ||
client2.send(); | ||
}); | ||
client.abort(); | ||
}); | ||
client.open("GET", url); | ||
client.send(); | ||
}); | ||
</script> | ||
</head> | ||
</html> |
64 changes: 64 additions & 0 deletions
64
tests/wpt/mozilla/tests/mozilla/resources/http-cache-trickle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
|
||
import time | ||
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) | ||
|
||
if request.GET.first("trickle", None): | ||
response.write_status_headers() | ||
for byte in config.get("response_body", uuid): | ||
response.writer.write_content(byte) | ||
time.sleep(1) | ||
|
||
content = config.get("response_body", uuid) | ||
if code in [204, 304]: | ||
return "" | ||
else: | ||
return content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters