test_ChallengeHeaderParser.js (4259B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const { parseChallengeHeader } = ChromeUtils.importESModule( 6 "chrome://remote/content/shared/ChallengeHeaderParser.sys.mjs" 7 ); 8 9 add_task(async function test_single_scheme() { 10 const TEST_HEADERS = [ 11 { 12 // double quotes 13 header: 'Basic realm="test"', 14 params: [{ name: "realm", value: "test" }], 15 }, 16 { 17 // single quote 18 header: "Basic realm='test'", 19 params: [{ name: "realm", value: "test" }], 20 }, 21 { 22 // multiline 23 header: `Basic 24 realm='test'`, 25 params: [{ name: "realm", value: "test" }], 26 }, 27 { 28 // with additional parameter. 29 header: 'Basic realm="test", charset="UTF-8"', 30 params: [ 31 { name: "realm", value: "test" }, 32 { name: "charset", value: "UTF-8" }, 33 ], 34 }, 35 ]; 36 for (const { header, params } of TEST_HEADERS) { 37 const challenges = parseChallengeHeader(header); 38 equal(challenges.length, 1); 39 equal(challenges[0].scheme, "Basic"); 40 deepEqual(challenges[0].params, params); 41 } 42 }); 43 44 add_task(async function test_realmless_scheme() { 45 const TEST_HEADERS = [ 46 { 47 // no parameter 48 header: "Custom", 49 params: [], 50 }, 51 { 52 // one non-realm parameter 53 header: "Custom charset='UTF-8'", 54 params: [{ name: "charset", value: "UTF-8" }], 55 }, 56 ]; 57 58 for (const { header, params } of TEST_HEADERS) { 59 const challenges = parseChallengeHeader(header); 60 equal(challenges.length, 1); 61 equal(challenges[0].scheme, "Custom"); 62 deepEqual(challenges[0].params, params); 63 } 64 }); 65 66 add_task(async function test_multiple_schemes() { 67 const TEST_HEADERS = [ 68 { 69 header: 'Scheme1 realm="foo", Scheme2 realm="bar"', 70 params: [ 71 [{ name: "realm", value: "foo" }], 72 [{ name: "realm", value: "bar" }], 73 ], 74 }, 75 { 76 header: 'Scheme1 realm="foo", charset="UTF-8", Scheme2 realm="bar"', 77 params: [ 78 [ 79 { name: "realm", value: "foo" }, 80 { name: "charset", value: "UTF-8" }, 81 ], 82 [{ name: "realm", value: "bar" }], 83 ], 84 }, 85 { 86 header: `Scheme1 realm="foo", 87 charset="UTF-8", 88 Scheme2 realm="bar"`, 89 params: [ 90 [ 91 { name: "realm", value: "foo" }, 92 { name: "charset", value: "UTF-8" }, 93 ], 94 [{ name: "realm", value: "bar" }], 95 ], 96 }, 97 ]; 98 for (const { header, params } of TEST_HEADERS) { 99 const challenges = parseChallengeHeader(header); 100 equal(challenges.length, 2); 101 equal(challenges[0].scheme, "Scheme1"); 102 deepEqual(challenges[0].params, params[0]); 103 equal(challenges[1].scheme, "Scheme2"); 104 deepEqual(challenges[1].params, params[1]); 105 } 106 }); 107 108 add_task(async function test_digest_scheme() { 109 const header = `Digest 110 realm="http-auth@example.org", 111 qop="auth, auth-int", 112 algorithm=SHA-256, 113 nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v", 114 opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"`; 115 116 const challenges = parseChallengeHeader(header); 117 equal(challenges.length, 1); 118 equal(challenges[0].scheme, "Digest"); 119 120 // Note: we are not doing a deepEqual check here, because one of the params 121 // actually contains a `,` inside quotes for its value, which will not be 122 // handled properly by the current ChallengeHeaderParser. See Bug 1857847. 123 const realmParam = challenges[0].params.find(param => param.name === "realm"); 124 ok(realmParam); 125 equal(realmParam.value, "http-auth@example.org"); 126 127 // Once Bug 1857847 is addressed, this should start failing and can be 128 // switched to deepEqual. 129 notDeepEqual( 130 challenges[0].params, 131 [ 132 { name: "realm", value: "http-auth@example.org" }, 133 { name: "qop", value: "auth, auth-int" }, 134 { name: "algorithm", value: "SHA-256" }, 135 { name: "nonce", value: "7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v" }, 136 { name: "opaque", value: "FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS" }, 137 ], 138 "notDeepEqual should be changed to deepEqual when Bug 1857847 is fixed" 139 ); 140 });