|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 var httpserver = new HttpServer(); |
|
4 var currentTestIndex = 0; |
|
5 |
|
6 XPCOMUtils.defineLazyGetter(this, "port", function() { |
|
7 return httpserver.identity.primaryPort; |
|
8 }); |
|
9 |
|
10 XPCOMUtils.defineLazyGetter(this, "tests", function() { |
|
11 return [ |
|
12 // this is valid |
|
13 {url: "/assoc/assoctest?valid", |
|
14 responseheader: ["Assoc-Req: GET http://localhost:" + port + |
|
15 "/assoc/assoctest?valid", |
|
16 "Pragma: X-Verify-Assoc-Req"], |
|
17 flags: 0}, |
|
18 |
|
19 // this is invalid because the method is wrong |
|
20 {url: "/assoc/assoctest?invalid", |
|
21 responseheader: ["Assoc-Req: POST http://localhost:" + port + |
|
22 "/assoc/assoctest?invalid", |
|
23 "Pragma: X-Verify-Assoc-Req"], |
|
24 flags: CL_EXPECT_LATE_FAILURE}, |
|
25 |
|
26 // this is invalid because the url is wrong |
|
27 {url: "/assoc/assoctest?notvalid", |
|
28 responseheader: ["Assoc-Req: GET http://localhost:" + port + |
|
29 "/wrongpath/assoc/assoctest?notvalid", |
|
30 "Pragma: X-Verify-Assoc-Req"], |
|
31 flags: CL_EXPECT_LATE_FAILURE}, |
|
32 |
|
33 // this is invalid because the space between method and URL is missing |
|
34 {url: "/assoc/assoctest?invalid2", |
|
35 responseheader: ["Assoc-Req: GEThttp://localhost:" + port + |
|
36 "/assoc/assoctest?invalid2", |
|
37 "Pragma: X-Verify-Assoc-Req"], |
|
38 flags: CL_EXPECT_LATE_FAILURE}, |
|
39 ]; |
|
40 }); |
|
41 |
|
42 var oldPrefVal; |
|
43 var domBranch; |
|
44 |
|
45 function setupChannel(url) |
|
46 { |
|
47 var ios = Components.classes["@mozilla.org/network/io-service;1"]. |
|
48 getService(Ci.nsIIOService); |
|
49 var chan = ios.newChannel("http://localhost:" + port + url, "", null); |
|
50 return chan; |
|
51 } |
|
52 |
|
53 function startIter() |
|
54 { |
|
55 var channel = setupChannel(tests[currentTestIndex].url); |
|
56 channel.asyncOpen(new ChannelListener(completeIter, |
|
57 channel, tests[currentTestIndex].flags), null); |
|
58 } |
|
59 |
|
60 function completeIter(request, data, ctx) |
|
61 { |
|
62 if (++currentTestIndex < tests.length ) { |
|
63 startIter(); |
|
64 } else { |
|
65 domBranch.setBoolPref("enforce", oldPrefVal); |
|
66 httpserver.stop(do_test_finished); |
|
67 } |
|
68 } |
|
69 |
|
70 function run_test() |
|
71 { |
|
72 var prefService = |
|
73 Components.classes["@mozilla.org/preferences-service;1"] |
|
74 .getService(Components.interfaces.nsIPrefService); |
|
75 domBranch = prefService.getBranch("network.http.assoc-req."); |
|
76 oldPrefVal = domBranch.getBoolPref("enforce"); |
|
77 domBranch.setBoolPref("enforce", true); |
|
78 |
|
79 httpserver.registerPathHandler("/assoc/assoctest", handler); |
|
80 httpserver.start(-1); |
|
81 |
|
82 startIter(); |
|
83 do_test_pending(); |
|
84 } |
|
85 |
|
86 function handler(metadata, response) |
|
87 { |
|
88 var body = "thequickbrownfox"; |
|
89 response.setHeader("Content-Type", "text/plain", false); |
|
90 |
|
91 var header = tests[currentTestIndex].responseheader; |
|
92 if (header != undefined) { |
|
93 for (var i = 0; i < header.length; i++) { |
|
94 var splitHdr = header[i].split(": "); |
|
95 response.setHeader(splitHdr[0], splitHdr[1], false); |
|
96 } |
|
97 } |
|
98 |
|
99 response.setStatusLine(metadata.httpVersion, 200, "OK"); |
|
100 response.bodyOutputStream.write(body, body.length); |
|
101 } |