Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Components.utils.import("resource://gre/modules/Http.jsm"); |
michael@0 | 5 | Components.utils.import("resource://testing-common/httpd.js"); |
michael@0 | 6 | |
michael@0 | 7 | const BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1", |
michael@0 | 8 | "nsIBinaryInputStream", "setInputStream"); |
michael@0 | 9 | |
michael@0 | 10 | var server; |
michael@0 | 11 | |
michael@0 | 12 | const kDefaultServerPort = 9000; |
michael@0 | 13 | const kSuccessPath = "/success"; |
michael@0 | 14 | const kBaseUrl = "http://localhost:" + kDefaultServerPort; |
michael@0 | 15 | const kSuccessUrl = kBaseUrl + kSuccessPath; |
michael@0 | 16 | |
michael@0 | 17 | const kPostPath = "/post"; |
michael@0 | 18 | const kPostUrl = kBaseUrl + kPostPath; |
michael@0 | 19 | const kPostDataSent = [["foo", "bar"], ["complex", "!*()@"]]; |
michael@0 | 20 | const kPostDataReceived = "foo=bar&complex=%21%2A%28%29%40"; |
michael@0 | 21 | |
michael@0 | 22 | const kPutPath = "/put"; |
michael@0 | 23 | const kPutUrl = kBaseUrl + kPutPath; |
michael@0 | 24 | const kPutDataSent = [["P", "NP"]]; |
michael@0 | 25 | const kPutDataReceived = "P=NP"; |
michael@0 | 26 | |
michael@0 | 27 | const kGetPath = "/get"; |
michael@0 | 28 | const kGetUrl = kBaseUrl + kGetPath; |
michael@0 | 29 | |
michael@0 | 30 | function successResult(aRequest, aResponse) { |
michael@0 | 31 | aResponse.setStatusLine(null, 200, "OK"); |
michael@0 | 32 | aResponse.setHeader("Content-Type", "application/json"); |
michael@0 | 33 | aResponse.write("Success!"); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function getDataChecker(aExpectedMethod, aExpectedData) { |
michael@0 | 37 | return function(aRequest, aResponse) { |
michael@0 | 38 | let body = new BinaryInputStream(aRequest.bodyInputStream); |
michael@0 | 39 | let bytes = []; |
michael@0 | 40 | let avail; |
michael@0 | 41 | while ((avail = body.available()) > 0) |
michael@0 | 42 | Array.prototype.push.apply(bytes, body.readByteArray(avail)); |
michael@0 | 43 | |
michael@0 | 44 | do_check_eq(aRequest.method, aExpectedMethod); |
michael@0 | 45 | |
michael@0 | 46 | var data = String.fromCharCode.apply(null, bytes); |
michael@0 | 47 | |
michael@0 | 48 | do_check_eq(data, aExpectedData); |
michael@0 | 49 | |
michael@0 | 50 | aResponse.setStatusLine(null, 200, "OK"); |
michael@0 | 51 | aResponse.setHeader("Content-Type", "application/json"); |
michael@0 | 52 | aResponse.write("Success!"); |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | add_test(function test_successCallback() { |
michael@0 | 57 | do_test_pending(); |
michael@0 | 58 | let options = { |
michael@0 | 59 | onLoad: function(aResponse) { |
michael@0 | 60 | do_check_eq(aResponse, "Success!"); |
michael@0 | 61 | do_test_finished(); |
michael@0 | 62 | run_next_test(); |
michael@0 | 63 | }, |
michael@0 | 64 | onError: function(e) { |
michael@0 | 65 | do_check_true(false); |
michael@0 | 66 | do_test_finished(); |
michael@0 | 67 | run_next_test(); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | httpRequest(kSuccessUrl, options); |
michael@0 | 71 | }); |
michael@0 | 72 | |
michael@0 | 73 | add_test(function test_errorCallback() { |
michael@0 | 74 | do_test_pending(); |
michael@0 | 75 | let options = { |
michael@0 | 76 | onSuccess: function(aResponse) { |
michael@0 | 77 | do_check_true(false); |
michael@0 | 78 | do_test_finished(); |
michael@0 | 79 | run_next_test(); |
michael@0 | 80 | }, |
michael@0 | 81 | onError: function(e, aResponse) { |
michael@0 | 82 | do_check_eq(e, "404 - Not Found"); |
michael@0 | 83 | do_test_finished(); |
michael@0 | 84 | run_next_test(); |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | httpRequest(kBaseUrl + "/failure", options); |
michael@0 | 88 | }); |
michael@0 | 89 | |
michael@0 | 90 | add_test(function test_PostData() { |
michael@0 | 91 | do_test_pending(); |
michael@0 | 92 | let options = { |
michael@0 | 93 | onLoad: function(aResponse) { |
michael@0 | 94 | do_check_eq(aResponse, "Success!"); |
michael@0 | 95 | do_test_finished(); |
michael@0 | 96 | run_next_test(); |
michael@0 | 97 | }, |
michael@0 | 98 | onError: function(e) { |
michael@0 | 99 | do_check_true(false); |
michael@0 | 100 | do_test_finished(); |
michael@0 | 101 | run_next_test(); |
michael@0 | 102 | }, |
michael@0 | 103 | postData: kPostDataSent |
michael@0 | 104 | } |
michael@0 | 105 | httpRequest(kPostUrl, options); |
michael@0 | 106 | }); |
michael@0 | 107 | |
michael@0 | 108 | add_test(function test_PutData() { |
michael@0 | 109 | do_test_pending(); |
michael@0 | 110 | let options = { |
michael@0 | 111 | method: "PUT", |
michael@0 | 112 | onLoad: function(aResponse) { |
michael@0 | 113 | do_check_eq(aResponse, "Success!"); |
michael@0 | 114 | do_test_finished(); |
michael@0 | 115 | run_next_test(); |
michael@0 | 116 | }, |
michael@0 | 117 | onError: function(e) { |
michael@0 | 118 | do_check_true(false); |
michael@0 | 119 | do_test_finished(); |
michael@0 | 120 | run_next_test(); |
michael@0 | 121 | }, |
michael@0 | 122 | postData: kPutDataSent |
michael@0 | 123 | } |
michael@0 | 124 | httpRequest(kPutUrl, options); |
michael@0 | 125 | }); |
michael@0 | 126 | |
michael@0 | 127 | add_test(function test_GetData() { |
michael@0 | 128 | do_test_pending(); |
michael@0 | 129 | let options = { |
michael@0 | 130 | onLoad: function(aResponse) { |
michael@0 | 131 | do_check_eq(aResponse, "Success!"); |
michael@0 | 132 | do_test_finished(); |
michael@0 | 133 | run_next_test(); |
michael@0 | 134 | }, |
michael@0 | 135 | onError: function(e) { |
michael@0 | 136 | do_check_true(false); |
michael@0 | 137 | do_test_finished(); |
michael@0 | 138 | run_next_test(); |
michael@0 | 139 | }, |
michael@0 | 140 | postData: null |
michael@0 | 141 | } |
michael@0 | 142 | httpRequest(kGetUrl, options); |
michael@0 | 143 | }); |
michael@0 | 144 | |
michael@0 | 145 | add_test(function test_OptionalParameters() { |
michael@0 | 146 | let options = { |
michael@0 | 147 | onLoad: null, |
michael@0 | 148 | onError: null, |
michael@0 | 149 | logger: null |
michael@0 | 150 | }; |
michael@0 | 151 | // Just make sure that nothing throws when doing this (i.e. httpRequest |
michael@0 | 152 | // doesn't try to access null options). |
michael@0 | 153 | httpRequest(kGetUrl, options); |
michael@0 | 154 | run_next_test(); |
michael@0 | 155 | }); |
michael@0 | 156 | |
michael@0 | 157 | function run_test() { |
michael@0 | 158 | // Set up a mock HTTP server to serve a success page. |
michael@0 | 159 | server = new HttpServer(); |
michael@0 | 160 | server.registerPathHandler(kSuccessPath, successResult); |
michael@0 | 161 | server.registerPathHandler(kPostPath, |
michael@0 | 162 | getDataChecker("POST", kPostDataReceived)); |
michael@0 | 163 | server.registerPathHandler(kPutPath, |
michael@0 | 164 | getDataChecker("PUT", kPutDataReceived)); |
michael@0 | 165 | server.registerPathHandler(kGetPath, getDataChecker("GET", "")); |
michael@0 | 166 | server.start(kDefaultServerPort); |
michael@0 | 167 | |
michael@0 | 168 | run_next_test(); |
michael@0 | 169 | |
michael@0 | 170 | // Teardown. |
michael@0 | 171 | do_register_cleanup(function() { |
michael@0 | 172 | server.stop(function() { }); |
michael@0 | 173 | }); |
michael@0 | 174 | } |
michael@0 | 175 |