2009-01-13 22:53:43 -08:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=464848
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>XMLHttpRequest send data and headers</title>
|
2009-05-06 13:46:04 -07:00
|
|
|
<script type="text/javascript" src="/MochiKit/packed.js"></script>
|
2009-01-13 22:53:43 -08:00
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a target="_blank"
|
|
|
|
href="https://bugzilla.mozilla.org/show_bug.cgi?id=464848">Mozilla Bug 464848</a>
|
|
|
|
<p id="display">
|
2009-08-31 14:07:16 -07:00
|
|
|
<input id="fileList" type="file"></input>
|
2009-01-13 22:53:43 -08:00
|
|
|
</p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="application/javascript;version=1.8">
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", "file_XHRSendData_doc.xml", false);
|
|
|
|
xhr.send();
|
|
|
|
testDoc1 = xhr.responseXML;
|
|
|
|
is(testDoc1.inputEncoding, "ISO-8859-1", "wrong encoding");
|
|
|
|
|
|
|
|
testDoc2 = document.implementation.createDocument("", "", null);
|
|
|
|
testDoc2.appendChild(testDoc2.createComment(" doc 2 "));
|
|
|
|
testDoc2.appendChild(testDoc2.createElement("res"));
|
|
|
|
testDoc2.documentElement.appendChild(testDoc2.createTextNode("text"));
|
|
|
|
is(testDoc2.inputEncoding, null, "wrong encoding");
|
|
|
|
|
2009-08-31 14:07:16 -07:00
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
|
|
|
var testData = "blahblahblahblahblahblahblaaaaaaaah. blah.";
|
|
|
|
testFileTxt = createFileWithDataExt(testData, ".txt");
|
|
|
|
testFilePng = createFileWithDataExt(testData, ".png");
|
|
|
|
testFileJpg = createFileWithDataExt(testData, ".jpg");
|
|
|
|
testFileGif = createFileWithDataExt(testData, ".gif");
|
|
|
|
testFilePdf = createFileWithDataExt(testData, ".pdf");
|
|
|
|
testFileXml = createFileWithDataExt(testData, ".xml");
|
|
|
|
testFileUnknown = createFileWithDataExt(testData, "noext");
|
|
|
|
|
|
|
|
function createFileWithDataExt(fileData, extension) {
|
|
|
|
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
|
|
|
|
var testFile = dirSvc.get("ProfD", Components.interfaces.nsIFile);
|
|
|
|
testFile.append("testfile" + extension);
|
|
|
|
var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
|
|
|
|
outStream.init(testFile, 0x02 | 0x08 | 0x20, 0666, 0);
|
|
|
|
outStream.write(fileData, fileData.length);
|
|
|
|
outStream.close();
|
|
|
|
|
|
|
|
var fileList = document.getElementById('fileList');
|
|
|
|
fileList.value = testFile.path;
|
|
|
|
|
|
|
|
return fileList.files[0];
|
|
|
|
}
|
|
|
|
|
2009-01-13 22:53:43 -08:00
|
|
|
tests = [{ body: null,
|
|
|
|
resBody: "",
|
|
|
|
},
|
|
|
|
{ body: undefined,
|
|
|
|
resBody: "",
|
|
|
|
},
|
|
|
|
{ body: "hi",
|
|
|
|
resBody: "hi",
|
|
|
|
resContentType: "text/plain; charset=UTF-8",
|
|
|
|
},
|
|
|
|
{ body: "r\xe4ksm\xf6rg\xe5s",
|
|
|
|
resBody: "r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s",
|
|
|
|
resContentType: "text/plain; charset=UTF-8",
|
|
|
|
},
|
|
|
|
{ body: "hi",
|
|
|
|
contentType: "",
|
|
|
|
resBody: "hi",
|
|
|
|
resContentType: "text/plain; charset=UTF-8",
|
|
|
|
},
|
|
|
|
{ body: "hi",
|
|
|
|
contentType: "foo/bar",
|
|
|
|
resBody: "hi",
|
|
|
|
resContentType: "foo/bar; charset=UTF-8",
|
|
|
|
},
|
|
|
|
{ body: "hi",
|
|
|
|
contentType: "foo/bar; baz=bin",
|
|
|
|
resBody: "hi",
|
|
|
|
resContentType: "foo/bar; charset=UTF-8; baz=bin",
|
|
|
|
},
|
|
|
|
{ body: "hi",
|
|
|
|
contentType: "foo/bar; charset=ascii; baz=bin",
|
|
|
|
resBody: "hi",
|
|
|
|
resContentType: "foo/bar; charset=UTF-8; baz=bin",
|
|
|
|
},
|
|
|
|
{ body: "hi",
|
|
|
|
contentType: "foo/bar; charset=uTf-8",
|
|
|
|
resBody: "hi",
|
|
|
|
resContentType: "foo/bar; charset=uTf-8",
|
|
|
|
},
|
|
|
|
{ body: testDoc1,
|
|
|
|
resBody: "<!-- comment -->\n<out>hi</out>",
|
|
|
|
resContentType: "application/xml; charset=ISO-8859-1",
|
|
|
|
},
|
|
|
|
{ body: testDoc1,
|
|
|
|
contentType: "foo/bar",
|
|
|
|
resBody: "<!-- comment -->\n<out>hi</out>",
|
|
|
|
resContentType: "foo/bar; charset=ISO-8859-1",
|
|
|
|
},
|
|
|
|
{ body: testDoc1,
|
|
|
|
contentType: "foo/bar; charset=ascii; baz=bin",
|
|
|
|
resBody: "<!-- comment -->\n<out>hi</out>",
|
|
|
|
resContentType: "foo/bar; charset=ISO-8859-1; baz=bin",
|
|
|
|
},
|
|
|
|
{ body: testDoc1,
|
|
|
|
contentType: "foo/bar; charset=IsO-8859-1",
|
|
|
|
resBody: "<!-- comment -->\n<out>hi</out>",
|
|
|
|
resContentType: "foo/bar; charset=IsO-8859-1",
|
|
|
|
},
|
|
|
|
{ body: testDoc2,
|
|
|
|
resBody: "<!-- doc 2 -->\n<res>text</res>",
|
|
|
|
resContentType: "application/xml; charset=UTF-8",
|
|
|
|
},
|
|
|
|
{ body: testDoc2,
|
|
|
|
contentType: "foo/bar",
|
|
|
|
resBody: "<!-- doc 2 -->\n<res>text</res>",
|
|
|
|
resContentType: "foo/bar; charset=UTF-8",
|
|
|
|
},
|
|
|
|
{ body: testDoc2,
|
|
|
|
contentType: "foo/bar; charset=ascii; baz=bin",
|
|
|
|
resBody: "<!-- doc 2 -->\n<res>text</res>",
|
|
|
|
resContentType: "foo/bar; charset=UTF-8; baz=bin",
|
|
|
|
},
|
|
|
|
{ body: testDoc2,
|
|
|
|
contentType: "foo/bar; charset=uTf-8",
|
|
|
|
resBody: "<!-- doc 2 -->\n<res>text</res>",
|
|
|
|
resContentType: "foo/bar; charset=uTf-8",
|
|
|
|
},
|
2009-08-31 14:07:16 -07:00
|
|
|
{ body: testFileTxt,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: testFileTxt.mediaType,
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ body: testFilePng,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: testFilePng.mediaType,
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ body: testFileJpg,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: testFileJpg.mediaType,
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ body: testFileGif,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: testFileGif.mediaType,
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ body: testFilePdf,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: testFilePdf.mediaType,
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ body: testFileXml,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: testFileXml.mediaType,
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ body: testFileUnknown,
|
|
|
|
resBody: testData,
|
|
|
|
resContentType: "",
|
|
|
|
resContentLength: testData.length,
|
|
|
|
},
|
|
|
|
{ //will trigger a redirect test server-side
|
|
|
|
body: ("TEST_REDIRECT_STR&url=" + window.location.host + window.location.pathname),
|
|
|
|
redirect: true,
|
|
|
|
},
|
2009-01-13 22:53:43 -08:00
|
|
|
];
|
|
|
|
|
2009-08-31 14:07:16 -07:00
|
|
|
try {
|
|
|
|
for each(test in tests) {
|
|
|
|
xhr = new XMLHttpRequest;
|
|
|
|
xhr.open("POST", "file_XHRSendData.sjs", false);
|
|
|
|
if (test.contentType)
|
|
|
|
xhr.setRequestHeader("Content-Type", test.contentType);
|
|
|
|
xhr.send(test.body);
|
2009-01-13 22:53:43 -08:00
|
|
|
|
2009-08-31 14:07:16 -07:00
|
|
|
if (test.resContentType) {
|
|
|
|
is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
|
|
|
|
"Wrong Content-Type sent");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
is(xhr.getResponseHeader("Result-Content-Type"), null);
|
|
|
|
}
|
2009-01-13 22:53:43 -08:00
|
|
|
|
2009-08-31 14:07:16 -07:00
|
|
|
if (test.resContentLength) {
|
|
|
|
is(xhr.getResponseHeader("Result-Content-Length"), test.resContentLength, "Wrong Content-Length sent");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test.body instanceof Document) {
|
|
|
|
is(xhr.responseText.replace("\r\n", "\n"), test.resBody, "Wrong body");
|
|
|
|
}
|
|
|
|
else if (!test.redirect) {
|
|
|
|
is(xhr.responseText, test.resBody, "Wrong body");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If we're testing redirect, determine whether the body is
|
|
|
|
// this document by looking for the relevant bug url
|
|
|
|
is(xhr.responseText.indexOf("https://bugzilla.mozilla.org/show_bug.cgi?id=464848") >= 0, true,
|
|
|
|
"Wrong page for redirect");
|
|
|
|
}
|
2009-01-14 00:19:16 -08:00
|
|
|
}
|
2009-01-13 22:53:43 -08:00
|
|
|
}
|
2009-08-31 14:07:16 -07:00
|
|
|
finally {
|
|
|
|
cleanUpData();
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanUpData() {
|
|
|
|
removeFileWithExt(".txt");
|
|
|
|
removeFileWithExt(".png");
|
|
|
|
removeFileWithExt(".jpg");
|
|
|
|
removeFileWithExt(".gif");
|
|
|
|
removeFileWithExt(".pdf");
|
|
|
|
removeFileWithExt(".xml");
|
|
|
|
removeFileWithExt("noext");
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFileWithExt(extension)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
|
|
|
|
var testFile = dirSvc.get("ProfD", Components.interfaces.nsIFile);
|
|
|
|
|
|
|
|
testFile.append("testfile" + extension);
|
|
|
|
testFile.remove(false);
|
|
|
|
}
|
|
|
|
|
2009-01-13 22:53:43 -08:00
|
|
|
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|