netwerk/test/unit/test_headers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_headers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +//
     1.5 +//  cleaner HTTP header test infrastructure
     1.6 +//
     1.7 +//  tests bugs:  589292, [add more here: see hg log for definitive list]
     1.8 +//
     1.9 +//  TO ADD NEW TESTS:
    1.10 +//  1) Increment up 'lastTest' to new number (say, "99")
    1.11 +//  2) Add new test 'handler99' and 'completeTest99' functions.
    1.12 +//  3) If your test should fail the necko channel, set 
    1.13 +//     test_flags[99] = CL_EXPECT_FAILURE.   
    1.14 +//
    1.15 +// TO DEBUG JUST ONE TEST: temporarily change firstTest and lastTest to equal
    1.16 +//                         the test # you're interested in.
    1.17 +//
    1.18 +//  For tests that need duplicate copies of headers to be sent, see
    1.19 +//  test_duplicate_headers.js
    1.20 +
    1.21 +var firstTest = 1;   // set to test of interest when debugging
    1.22 +var lastTest = 4;    // set to test of interest when debugging
    1.23 +////////////////////////////////////////////////////////////////////////////////
    1.24 +
    1.25 +// Note: sets Cc and Ci variables
    1.26 +
    1.27 +Cu.import("resource://testing-common/httpd.js");
    1.28 +
    1.29 +XPCOMUtils.defineLazyGetter(this, "URL", function() {
    1.30 +  return "http://localhost:" + httpserver.identity.primaryPort;
    1.31 +});
    1.32 +
    1.33 +var httpserver = new HttpServer();
    1.34 +var index = 0;
    1.35 +var nextTest = firstTest;
    1.36 +var test_flags = new Array();
    1.37 +var testPathBase = "/test_headers";
    1.38 +
    1.39 +function run_test()
    1.40 +{
    1.41 +  httpserver.start(-1);
    1.42 +
    1.43 +  do_test_pending();
    1.44 +  run_test_number(nextTest);
    1.45 +}
    1.46 +
    1.47 +function runNextTest()
    1.48 +{
    1.49 +  if (nextTest == lastTest) {
    1.50 +    endTests();
    1.51 +    return;
    1.52 +  }
    1.53 +  nextTest++;
    1.54 +  // Make sure test functions exist
    1.55 +  if (eval("handler" + nextTest) == undefined)
    1.56 +    do_throw("handler" + nextTest + " undefined!");
    1.57 +  if (eval("completeTest" + nextTest) == undefined)
    1.58 +    do_throw("completeTest" + nextTest + " undefined!");
    1.59 +  
    1.60 +  run_test_number(nextTest);
    1.61 +}
    1.62 +
    1.63 +function run_test_number(num)
    1.64 +{
    1.65 +  testPath = testPathBase + num;
    1.66 +  httpserver.registerPathHandler(testPath, eval("handler" + num));
    1.67 +
    1.68 +  var channel = setupChannel(testPath);
    1.69 +  flags = test_flags[num];   // OK if flags undefined for test
    1.70 +  channel.asyncOpen(new ChannelListener(eval("completeTest" + num),
    1.71 +                                        channel, flags), null);
    1.72 +}
    1.73 +
    1.74 +function setupChannel(url)
    1.75 +{
    1.76 +  var ios = Components.classes["@mozilla.org/network/io-service;1"].
    1.77 +                       getService(Ci.nsIIOService);
    1.78 +  var chan = ios.newChannel(URL + url, "", null);
    1.79 +  var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    1.80 +  return httpChan;
    1.81 +}
    1.82 +
    1.83 +function endTests()
    1.84 +{
    1.85 +  httpserver.stop(do_test_finished);
    1.86 +}
    1.87 +
    1.88 +////////////////////////////////////////////////////////////////////////////////
    1.89 +// Test 1: test Content-Disposition channel attributes
    1.90 +function handler1(metadata, response)
    1.91 +{
    1.92 +  response.setStatusLine(metadata.httpVersion, 200, "OK");
    1.93 +  response.setHeader("Content-Disposition", "attachment; filename=foo");
    1.94 +  response.setHeader("Content-Type", "text/plain", false);
    1.95 +  var body = "foo";
    1.96 +}
    1.97 +
    1.98 +function completeTest1(request, data, ctx)
    1.99 +{
   1.100 +  try {
   1.101 +    var chan = request.QueryInterface(Ci.nsIChannel);
   1.102 +    do_check_eq(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
   1.103 +    do_check_eq(chan.contentDispositionFilename, "foo");
   1.104 +    do_check_eq(chan.contentDispositionHeader, "attachment; filename=foo");
   1.105 +  } catch (ex) {
   1.106 +    do_throw("error parsing Content-Disposition: " + ex);
   1.107 +  }
   1.108 +  runNextTest();  
   1.109 +}
   1.110 +
   1.111 +////////////////////////////////////////////////////////////////////////////////
   1.112 +// Test 2: no filename 
   1.113 +function handler2(metadata, response)
   1.114 +{
   1.115 +  response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.116 +  response.setHeader("Content-Type", "text/plain", false);
   1.117 +  response.setHeader("Content-Disposition", "attachment");
   1.118 +  var body = "foo";
   1.119 +  response.bodyOutputStream.write(body, body.length);
   1.120 +}
   1.121 +
   1.122 +function completeTest2(request, data, ctx)
   1.123 +{
   1.124 +  try {
   1.125 +    var chan = request.QueryInterface(Ci.nsIChannel);
   1.126 +    do_check_eq(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
   1.127 +    do_check_eq(chan.contentDispositionHeader, "attachment");
   1.128 +
   1.129 +    filename = chan.contentDispositionFilename;  // should barf
   1.130 +    do_throw("Should have failed getting Content-Disposition filename");
   1.131 +  } catch (ex) {
   1.132 +    do_print("correctly ate exception");    
   1.133 +  }
   1.134 +  runNextTest();  
   1.135 +}
   1.136 +
   1.137 +////////////////////////////////////////////////////////////////////////////////
   1.138 +// Test 3: filename missing
   1.139 +function handler3(metadata, response)
   1.140 +{
   1.141 +  response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.142 +  response.setHeader("Content-Type", "text/plain", false);
   1.143 +  response.setHeader("Content-Disposition", "attachment; filename=");
   1.144 +  var body = "foo";
   1.145 +  response.bodyOutputStream.write(body, body.length);
   1.146 +}
   1.147 +
   1.148 +function completeTest3(request, data, ctx)
   1.149 +{
   1.150 +  try {
   1.151 +    var chan = request.QueryInterface(Ci.nsIChannel);
   1.152 +    do_check_eq(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
   1.153 +    do_check_eq(chan.contentDispositionHeader, "attachment; filename=");
   1.154 +
   1.155 +    filename = chan.contentDispositionFilename;  // should barf
   1.156 +    do_throw("Should have failed getting Content-Disposition filename");
   1.157 +  } catch (ex) {
   1.158 +    do_print("correctly ate exception");    
   1.159 +  }
   1.160 +  runNextTest();  
   1.161 +}
   1.162 +
   1.163 +////////////////////////////////////////////////////////////////////////////////
   1.164 +// Test 4: inline
   1.165 +function handler4(metadata, response)
   1.166 +{
   1.167 +  response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.168 +  response.setHeader("Content-Type", "text/plain", false);
   1.169 +  response.setHeader("Content-Disposition", "inline");
   1.170 +  var body = "foo";
   1.171 +  response.bodyOutputStream.write(body, body.length);
   1.172 +}
   1.173 +
   1.174 +function completeTest4(request, data, ctx)
   1.175 +{
   1.176 +  try {
   1.177 +    var chan = request.QueryInterface(Ci.nsIChannel);
   1.178 +    do_check_eq(chan.contentDisposition, chan.DISPOSITION_INLINE);
   1.179 +    do_check_eq(chan.contentDispositionHeader, "inline");
   1.180 +
   1.181 +    filename = chan.contentDispositionFilename;  // should barf
   1.182 +    do_throw("Should have failed getting Content-Disposition filename");
   1.183 +  } catch (ex) {
   1.184 +    do_print("correctly ate exception");    
   1.185 +  }
   1.186 +  runNextTest();
   1.187 +}

mercurial