Skip to content
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

Add more XMLHttpRequest overrideMimeType() tests #8449

Merged
merged 4 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mimesniff/mime-types/resources/mime-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@
"input": "\t",
"output": null
},
{
"input": "/",
"output": null
},
{
"input": "bogus",
"output": null
Expand All @@ -295,6 +299,10 @@
"input": "(/)",
"output": null
},
{
"input": "ÿ/ÿ",
"output": null
},
{
"input": "text/html(;doesnot=matter",
"output": null
Expand Down
89 changes: 40 additions & 49 deletions xhr/overridemimetype-blob.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,51 @@
<div id="log"></div>
<script>
async_test(t => {
const client = new XMLHttpRequest()
const client = new XMLHttpRequest();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is extraordinarily hard to review because almost every line has changed, but I guess only because of semicolons? Please split up the commits at least next time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I figured it wouldn't be much of an issue given how GitHub highlights stuff.

client.onload = t.step_func_done(() => {
assert_equals(client.getResponseHeader("Content-Type"), "")
assert_equals(client.response.type, "text/xml")
})
client.open("GET", "resources/status.py")
client.responseType = "blob"
client.send()
}, "Use text/xml as fallback MIME type")
assert_equals(client.getResponseHeader("Content-Type"), "");
assert_equals(client.response.type, "text/xml");
});
client.open("GET", "resources/status.py");
client.responseType = "blob";
client.send();
}, "Use text/xml as fallback MIME type");

async_test(t => {
const client = new XMLHttpRequest()
const client = new XMLHttpRequest();
client.onload = t.step_func_done(() => {
assert_equals(client.getResponseHeader("Content-Type"), "")
assert_equals(client.response.type, "text/xml")
assert_equals(client.getResponseHeader("Content-Type"), "");
assert_equals(client.response.type, "text/xml");
})
client.open("GET", "resources/status.py?content=thisshouldnotmakeadifferencebutdoes")
client.responseType = "blob"
client.send()
}, "Use text/xml as fallback MIME type, 2")
client.open("GET", "resources/status.py?content=thisshouldnotmakeadifferencebutdoes");
client.responseType = "blob";
client.send();
}, "Use text/xml as fallback MIME type, 2");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the above two tests are in a file related to overrideMimeType()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No good reason.


async_test(t => {
const client = new XMLHttpRequest()
client.onload = t.step_func_done(() => {
assert_equals(client.getResponseHeader("Content-Type"), "")
assert_equals(client.response.type, "application/octet-stream")
})
client.open("GET", "resources/status.py")
client.responseType = "blob"
client.overrideMimeType("bogus")
client.send()
}, "Bogus MIME type should end up as application/octet-stream")
promise_test(() => {
// Don't load generated-mime-types.json as sending them all over the network would be prohibitive
return fetch("../mimesniff/mime-types/resources/mime-types.json").then(res => res.json()).then(runTests);
}, "Loading data…");

async_test(t => {
const client = new XMLHttpRequest()
client.onload = t.step_func_done(() => {
assert_equals(client.getResponseHeader("Content-Type"), "")
assert_equals(client.response.type, "application/octet-stream")
})
client.open("GET", "resources/status.py")
client.responseType = "blob"
client.overrideMimeType("text/xml;charset=†")
client.send()
}, "Bogus MIME type should end up as application/octet-stream, 2")

async_test(t => {
const client = new XMLHttpRequest()
client.onload = t.step_func_done(() => {
assert_equals(client.getResponseHeader("Content-Type"), "")
assert_equals(client.response.type, "hi/x")
})
client.open("GET", "resources/status.py")
client.responseType = "blob"
client.overrideMimeType("HI/x;test=test")
client.send()
}, "Valid MIME types need to be normalized")
function runTests(tests) {
let index = 0;
tests.forEach((val) => {
if(typeof val === "string") {
return;
}
index++;
async_test(t => {
const client = new XMLHttpRequest(),
expectedOutput = val.output !== null ? val.output : "application/octet-stream";
client.onload = t.step_func_done(() => {
assert_equals(client.getResponseHeader("Content-Type"), "");
assert_equals(client.response.type, expectedOutput);
});
client.open("GET", "resources/status.py");
client.responseType = "blob";
client.overrideMimeType(val.input);
client.send();
}, index + ") MIME types need to be parsed and serialized: " + val.input);
});
}
</script>
40 changes: 40 additions & 0 deletions xhr/overridemimetype-edge-cases.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const testURL = "resources/status.py?type=" + encodeURIComponent("text/plain;charset=windows-1252") + "&content=%C2%F0";

async_test(t => {
const client = new XMLHttpRequest();
let secondTime = false;
client.onload = t.step_func(() => {
if(!secondTime) {
assert_equals(client.responseText, "\uFFFD");
secondTime = true;
client.open("GET", testURL);
client.send();
} else {
assert_equals(client.responseText, "Âð");
t.done();
}
});
client.open("GET", testURL);
client.overrideMimeType("text/plain;charset=UTF-8")
client.send();
}, "overrideMimeType() state needs to be reset across requests");

async_test(t => {
const client = new XMLHttpRequest();
client.onload = t.step_func_done(() => {
assert_equals(client.responseText, "Âð")
});
client.open("GET", testURL);
client.overrideMimeType("text/xml");
client.send();
}, "If charset is not overridden by overrideMimeType() the original continues to be used");

async_test(t => {
const client = new XMLHttpRequest();
client.onload = t.step_func_done(() => {
assert_equals(client.responseText, "\uFFFD")
});
client.open("GET", testURL);
client.overrideMimeType("text/plain;charset=342");
client.send();
}, "Charset can be overridden by overrideMimeType() with a bogus charset");
1 change: 1 addition & 0 deletions xhr/responsetext-decoding.htm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
request("text/plain", "%EF%BB%BF", "", "text");
request("text/plain", "%EF%BB%BF%EF%BB%BF", "\uFEFF", "text");
request("text/plain", "%C2", "\uFFFD", "text");
request("text/plain;charset=bogus", "%C2", "\uFFFD", "text");
request("text/xml", "%FE%FF", "", "text");
request("text/xml", "%FE%FF%FE%FF", "\uFEFF", "text");
request("text/xml", "%EF%BB%BF", "", "text");
Expand Down