netwerk/test/unit/test_headers.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 //
michael@0 2 // cleaner HTTP header test infrastructure
michael@0 3 //
michael@0 4 // tests bugs: 589292, [add more here: see hg log for definitive list]
michael@0 5 //
michael@0 6 // TO ADD NEW TESTS:
michael@0 7 // 1) Increment up 'lastTest' to new number (say, "99")
michael@0 8 // 2) Add new test 'handler99' and 'completeTest99' functions.
michael@0 9 // 3) If your test should fail the necko channel, set
michael@0 10 // test_flags[99] = CL_EXPECT_FAILURE.
michael@0 11 //
michael@0 12 // TO DEBUG JUST ONE TEST: temporarily change firstTest and lastTest to equal
michael@0 13 // the test # you're interested in.
michael@0 14 //
michael@0 15 // For tests that need duplicate copies of headers to be sent, see
michael@0 16 // test_duplicate_headers.js
michael@0 17
michael@0 18 var firstTest = 1; // set to test of interest when debugging
michael@0 19 var lastTest = 4; // set to test of interest when debugging
michael@0 20 ////////////////////////////////////////////////////////////////////////////////
michael@0 21
michael@0 22 // Note: sets Cc and Ci variables
michael@0 23
michael@0 24 Cu.import("resource://testing-common/httpd.js");
michael@0 25
michael@0 26 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 27 return "http://localhost:" + httpserver.identity.primaryPort;
michael@0 28 });
michael@0 29
michael@0 30 var httpserver = new HttpServer();
michael@0 31 var index = 0;
michael@0 32 var nextTest = firstTest;
michael@0 33 var test_flags = new Array();
michael@0 34 var testPathBase = "/test_headers";
michael@0 35
michael@0 36 function run_test()
michael@0 37 {
michael@0 38 httpserver.start(-1);
michael@0 39
michael@0 40 do_test_pending();
michael@0 41 run_test_number(nextTest);
michael@0 42 }
michael@0 43
michael@0 44 function runNextTest()
michael@0 45 {
michael@0 46 if (nextTest == lastTest) {
michael@0 47 endTests();
michael@0 48 return;
michael@0 49 }
michael@0 50 nextTest++;
michael@0 51 // Make sure test functions exist
michael@0 52 if (eval("handler" + nextTest) == undefined)
michael@0 53 do_throw("handler" + nextTest + " undefined!");
michael@0 54 if (eval("completeTest" + nextTest) == undefined)
michael@0 55 do_throw("completeTest" + nextTest + " undefined!");
michael@0 56
michael@0 57 run_test_number(nextTest);
michael@0 58 }
michael@0 59
michael@0 60 function run_test_number(num)
michael@0 61 {
michael@0 62 testPath = testPathBase + num;
michael@0 63 httpserver.registerPathHandler(testPath, eval("handler" + num));
michael@0 64
michael@0 65 var channel = setupChannel(testPath);
michael@0 66 flags = test_flags[num]; // OK if flags undefined for test
michael@0 67 channel.asyncOpen(new ChannelListener(eval("completeTest" + num),
michael@0 68 channel, flags), null);
michael@0 69 }
michael@0 70
michael@0 71 function setupChannel(url)
michael@0 72 {
michael@0 73 var ios = Components.classes["@mozilla.org/network/io-service;1"].
michael@0 74 getService(Ci.nsIIOService);
michael@0 75 var chan = ios.newChannel(URL + url, "", null);
michael@0 76 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 77 return httpChan;
michael@0 78 }
michael@0 79
michael@0 80 function endTests()
michael@0 81 {
michael@0 82 httpserver.stop(do_test_finished);
michael@0 83 }
michael@0 84
michael@0 85 ////////////////////////////////////////////////////////////////////////////////
michael@0 86 // Test 1: test Content-Disposition channel attributes
michael@0 87 function handler1(metadata, response)
michael@0 88 {
michael@0 89 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 90 response.setHeader("Content-Disposition", "attachment; filename=foo");
michael@0 91 response.setHeader("Content-Type", "text/plain", false);
michael@0 92 var body = "foo";
michael@0 93 }
michael@0 94
michael@0 95 function completeTest1(request, data, ctx)
michael@0 96 {
michael@0 97 try {
michael@0 98 var chan = request.QueryInterface(Ci.nsIChannel);
michael@0 99 do_check_eq(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
michael@0 100 do_check_eq(chan.contentDispositionFilename, "foo");
michael@0 101 do_check_eq(chan.contentDispositionHeader, "attachment; filename=foo");
michael@0 102 } catch (ex) {
michael@0 103 do_throw("error parsing Content-Disposition: " + ex);
michael@0 104 }
michael@0 105 runNextTest();
michael@0 106 }
michael@0 107
michael@0 108 ////////////////////////////////////////////////////////////////////////////////
michael@0 109 // Test 2: no filename
michael@0 110 function handler2(metadata, response)
michael@0 111 {
michael@0 112 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 113 response.setHeader("Content-Type", "text/plain", false);
michael@0 114 response.setHeader("Content-Disposition", "attachment");
michael@0 115 var body = "foo";
michael@0 116 response.bodyOutputStream.write(body, body.length);
michael@0 117 }
michael@0 118
michael@0 119 function completeTest2(request, data, ctx)
michael@0 120 {
michael@0 121 try {
michael@0 122 var chan = request.QueryInterface(Ci.nsIChannel);
michael@0 123 do_check_eq(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
michael@0 124 do_check_eq(chan.contentDispositionHeader, "attachment");
michael@0 125
michael@0 126 filename = chan.contentDispositionFilename; // should barf
michael@0 127 do_throw("Should have failed getting Content-Disposition filename");
michael@0 128 } catch (ex) {
michael@0 129 do_print("correctly ate exception");
michael@0 130 }
michael@0 131 runNextTest();
michael@0 132 }
michael@0 133
michael@0 134 ////////////////////////////////////////////////////////////////////////////////
michael@0 135 // Test 3: filename missing
michael@0 136 function handler3(metadata, response)
michael@0 137 {
michael@0 138 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 139 response.setHeader("Content-Type", "text/plain", false);
michael@0 140 response.setHeader("Content-Disposition", "attachment; filename=");
michael@0 141 var body = "foo";
michael@0 142 response.bodyOutputStream.write(body, body.length);
michael@0 143 }
michael@0 144
michael@0 145 function completeTest3(request, data, ctx)
michael@0 146 {
michael@0 147 try {
michael@0 148 var chan = request.QueryInterface(Ci.nsIChannel);
michael@0 149 do_check_eq(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
michael@0 150 do_check_eq(chan.contentDispositionHeader, "attachment; filename=");
michael@0 151
michael@0 152 filename = chan.contentDispositionFilename; // should barf
michael@0 153 do_throw("Should have failed getting Content-Disposition filename");
michael@0 154 } catch (ex) {
michael@0 155 do_print("correctly ate exception");
michael@0 156 }
michael@0 157 runNextTest();
michael@0 158 }
michael@0 159
michael@0 160 ////////////////////////////////////////////////////////////////////////////////
michael@0 161 // Test 4: inline
michael@0 162 function handler4(metadata, response)
michael@0 163 {
michael@0 164 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 165 response.setHeader("Content-Type", "text/plain", false);
michael@0 166 response.setHeader("Content-Disposition", "inline");
michael@0 167 var body = "foo";
michael@0 168 response.bodyOutputStream.write(body, body.length);
michael@0 169 }
michael@0 170
michael@0 171 function completeTest4(request, data, ctx)
michael@0 172 {
michael@0 173 try {
michael@0 174 var chan = request.QueryInterface(Ci.nsIChannel);
michael@0 175 do_check_eq(chan.contentDisposition, chan.DISPOSITION_INLINE);
michael@0 176 do_check_eq(chan.contentDispositionHeader, "inline");
michael@0 177
michael@0 178 filename = chan.contentDispositionFilename; // should barf
michael@0 179 do_throw("Should have failed getting Content-Disposition filename");
michael@0 180 } catch (ex) {
michael@0 181 do_print("correctly ate exception");
michael@0 182 }
michael@0 183 runNextTest();
michael@0 184 }

mercurial