netwerk/test/unit/test_bug856978.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // This test makes sure that the authorization header can get deleted e.g. by
michael@0 6 // extensions if they are observing "http-on-modify-request". In a first step
michael@0 7 // the auth cache is filled with credentials which then get added to the
michael@0 8 // following request. On "http-on-modify-request" it is tested whether the
michael@0 9 // authorization header got added at all and if so it gets removed. This test
michael@0 10 // passes iff both succeeds.
michael@0 11
michael@0 12 Components.utils.import("resource://testing-common/httpd.js");
michael@0 13
michael@0 14 var notification = "http-on-modify-request";
michael@0 15
michael@0 16 var httpServer = null;
michael@0 17
michael@0 18 var authCredentials = "guest:guest";
michael@0 19 var authPath = "/authTest";
michael@0 20 var authCredsURL = "http://" + authCredentials + "@localhost:8888" + authPath;
michael@0 21 var authURL = "http://localhost:8888" + authPath;
michael@0 22
michael@0 23 function authHandler(metadata, response) {
michael@0 24 if (metadata.hasHeader("Test")) {
michael@0 25 // Lets see if the auth header got deleted.
michael@0 26 var noAuthHeader = false;
michael@0 27 if (!metadata.hasHeader("Authorization")) {
michael@0 28 noAuthHeader = true;
michael@0 29 }
michael@0 30 do_check_true(noAuthHeader);
michael@0 31 } else {
michael@0 32 // Not our test request yet.
michael@0 33 if (!metadata.hasHeader("Authorization")) {
michael@0 34 response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
michael@0 35 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
michael@0 36 }
michael@0 37 }
michael@0 38 }
michael@0 39
michael@0 40 function RequestObserver() {
michael@0 41 this.register();
michael@0 42 }
michael@0 43
michael@0 44 RequestObserver.prototype = {
michael@0 45 register: function() {
michael@0 46 do_print("Registering " + notification);
michael@0 47 Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService).
michael@0 48 addObserver(this, notification, true);
michael@0 49 },
michael@0 50
michael@0 51 QueryInterface: function(iid) {
michael@0 52 if (iid.equals(Ci.nsIObserver) || iid.equals(Ci.nsISupportsWeakReference) ||
michael@0 53 iid.equals(Ci.nsISupports)) {
michael@0 54 return this;
michael@0 55 }
michael@0 56 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 57 },
michael@0 58
michael@0 59 observe: function(subject, topic, data) {
michael@0 60 if (topic == notification) {
michael@0 61 if (!(subject instanceof Ci.nsIHttpChannel)) {
michael@0 62 do_throw(notification + " observed a non-HTTP channel.");
michael@0 63 }
michael@0 64 try {
michael@0 65 let authHeader = subject.getRequestHeader("Authorization");
michael@0 66 } catch (e) {
michael@0 67 // Throw if there is no header to delete. We should get one iff caching
michael@0 68 // the auth credentials is working and the header gets added _before_
michael@0 69 // "http-on-modify-request" gets called.
michael@0 70 httpServer.stop(do_test_finished);
michael@0 71 do_throw("No authorization header found, aborting!");
michael@0 72 }
michael@0 73 // We are still here. Let's remove the authorization header now.
michael@0 74 subject.setRequestHeader("Authorization", null, false);
michael@0 75 }
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 var listener = {
michael@0 80 onStartRequest: function test_onStartR(request, ctx) {},
michael@0 81
michael@0 82 onDataAvailable: function test_ODA() {
michael@0 83 do_throw("Should not get any data!");
michael@0 84 },
michael@0 85
michael@0 86 onStopRequest: function test_onStopR(request, ctx, status) {
michael@0 87 if (current_test < (tests.length - 1)) {
michael@0 88 current_test++;
michael@0 89 tests[current_test]();
michael@0 90 } else {
michael@0 91 do_test_pending();
michael@0 92 httpServer.stop(do_test_finished);
michael@0 93 }
michael@0 94 do_test_finished();
michael@0 95 }
michael@0 96 };
michael@0 97
michael@0 98 function makeChan(url) {
michael@0 99 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 100 var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
michael@0 101 return chan;
michael@0 102 }
michael@0 103
michael@0 104 var tests = [startAuthHeaderTest, removeAuthHeaderTest];
michael@0 105
michael@0 106 var current_test = 0;
michael@0 107
michael@0 108 var requestObserver = null;
michael@0 109
michael@0 110 function run_test() {
michael@0 111 httpServer = new HttpServer();
michael@0 112 httpServer.registerPathHandler(authPath, authHandler);
michael@0 113 httpServer.start(8888);
michael@0 114
michael@0 115 tests[0]();
michael@0 116 }
michael@0 117
michael@0 118 function startAuthHeaderTest() {
michael@0 119 var chan = makeChan(authCredsURL);
michael@0 120 chan.asyncOpen(listener, null);
michael@0 121
michael@0 122 do_test_pending();
michael@0 123 }
michael@0 124
michael@0 125 function removeAuthHeaderTest() {
michael@0 126 // After caching the auth credentials in the first test, lets try to remove
michael@0 127 // the authorization header now...
michael@0 128 requestObserver = new RequestObserver();
michael@0 129 var chan = makeChan(authURL);
michael@0 130 // Indicating that the request is coming from the second test.
michael@0 131 chan.setRequestHeader("Test", "1", false);
michael@0 132 chan.asyncOpen(listener, null);
michael@0 133
michael@0 134 do_test_pending();
michael@0 135 }

mercurial