Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // tests the registerPrefixHandler API |
michael@0 | 8 | |
michael@0 | 9 | XPCOMUtils.defineLazyGetter(this, "BASE", function() { |
michael@0 | 10 | return "http://localhost:" + srv.identity.primaryPort; |
michael@0 | 11 | }); |
michael@0 | 12 | |
michael@0 | 13 | function nocache(ch) |
michael@0 | 14 | { |
michael@0 | 15 | ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important! |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | function notFound(ch) |
michael@0 | 19 | { |
michael@0 | 20 | do_check_eq(ch.responseStatus, 404); |
michael@0 | 21 | do_check_false(ch.requestSucceeded); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function makeCheckOverride(magic) |
michael@0 | 25 | { |
michael@0 | 26 | return (function checkOverride(ch) |
michael@0 | 27 | { |
michael@0 | 28 | do_check_eq(ch.responseStatus, 200); |
michael@0 | 29 | do_check_eq(ch.responseStatusText, "OK"); |
michael@0 | 30 | do_check_true(ch.requestSucceeded); |
michael@0 | 31 | do_check_eq(ch.getResponseHeader("Override-Succeeded"), magic); |
michael@0 | 32 | }); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | XPCOMUtils.defineLazyGetter(this, "tests", function() { |
michael@0 | 36 | return [ |
michael@0 | 37 | new Test(BASE + "/prefix/dummy", prefixHandler, null, |
michael@0 | 38 | makeCheckOverride("prefix")), |
michael@0 | 39 | new Test(BASE + "/prefix/dummy", pathHandler, null, |
michael@0 | 40 | makeCheckOverride("path")), |
michael@0 | 41 | new Test(BASE + "/prefix/subpath/dummy", longerPrefixHandler, null, |
michael@0 | 42 | makeCheckOverride("subpath")), |
michael@0 | 43 | new Test(BASE + "/prefix/dummy", removeHandlers, null, notFound), |
michael@0 | 44 | new Test(BASE + "/prefix/subpath/dummy", newPrefixHandler, null, |
michael@0 | 45 | makeCheckOverride("subpath")) |
michael@0 | 46 | ]; |
michael@0 | 47 | }); |
michael@0 | 48 | |
michael@0 | 49 | /*************************** |
michael@0 | 50 | * registered prefix handler * |
michael@0 | 51 | ***************************/ |
michael@0 | 52 | |
michael@0 | 53 | function prefixHandler(channel) |
michael@0 | 54 | { |
michael@0 | 55 | nocache(channel); |
michael@0 | 56 | srv.registerPrefixHandler("/prefix/", makeOverride("prefix")); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /******************************** |
michael@0 | 60 | * registered path handler on top * |
michael@0 | 61 | ********************************/ |
michael@0 | 62 | |
michael@0 | 63 | function pathHandler(channel) |
michael@0 | 64 | { |
michael@0 | 65 | nocache(channel); |
michael@0 | 66 | srv.registerPathHandler("/prefix/dummy", makeOverride("path")); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | /********************************** |
michael@0 | 70 | * registered longer prefix handler * |
michael@0 | 71 | **********************************/ |
michael@0 | 72 | |
michael@0 | 73 | function longerPrefixHandler(channel) |
michael@0 | 74 | { |
michael@0 | 75 | nocache(channel); |
michael@0 | 76 | srv.registerPrefixHandler("/prefix/subpath/", makeOverride("subpath")); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | /************************ |
michael@0 | 80 | * removed prefix handler * |
michael@0 | 81 | ************************/ |
michael@0 | 82 | |
michael@0 | 83 | function removeHandlers(channel) |
michael@0 | 84 | { |
michael@0 | 85 | nocache(channel); |
michael@0 | 86 | srv.registerPrefixHandler("/prefix/", null); |
michael@0 | 87 | srv.registerPathHandler("/prefix/dummy", null); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /***************************** |
michael@0 | 91 | * re-register shorter handler * |
michael@0 | 92 | *****************************/ |
michael@0 | 93 | |
michael@0 | 94 | function newPrefixHandler(channel) |
michael@0 | 95 | { |
michael@0 | 96 | nocache(channel); |
michael@0 | 97 | srv.registerPrefixHandler("/prefix/", makeOverride("prefix")); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | var srv; |
michael@0 | 101 | var serverBasePath; |
michael@0 | 102 | var testsDirectory; |
michael@0 | 103 | |
michael@0 | 104 | function run_test() |
michael@0 | 105 | { |
michael@0 | 106 | testsDirectory = do_get_profile(); |
michael@0 | 107 | |
michael@0 | 108 | srv = createServer(); |
michael@0 | 109 | srv.start(-1); |
michael@0 | 110 | |
michael@0 | 111 | runHttpTests(tests, testComplete(srv)); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // PATH HANDLERS |
michael@0 | 115 | |
michael@0 | 116 | // generate an override |
michael@0 | 117 | function makeOverride(magic) |
michael@0 | 118 | { |
michael@0 | 119 | return (function override(metadata, response) |
michael@0 | 120 | { |
michael@0 | 121 | response.setStatusLine("1.1", 200, "OK"); |
michael@0 | 122 | response.setHeader("Override-Succeeded", magic, false); |
michael@0 | 123 | |
michael@0 | 124 | var body = "success!"; |
michael@0 | 125 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 126 | }); |
michael@0 | 127 | } |