OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:http_parser/http_parser.dart'; |
| 6 import 'package:test/test.dart'; |
| 7 |
| 8 void main() { |
| 9 group("parse", () { |
| 10 _singleChallengeTests( |
| 11 (challenge) => new AuthenticationChallenge.parse(challenge)); |
| 12 }); |
| 13 |
| 14 group("parseHeader", () { |
| 15 group("with a single challenge", () { |
| 16 _singleChallengeTests((challenge) { |
| 17 var challenges = AuthenticationChallenge.parseHeader(challenge); |
| 18 expect(challenges, hasLength(1)); |
| 19 return challenges.single; |
| 20 }); |
| 21 }); |
| 22 |
| 23 test("parses multiple challenges", () { |
| 24 var challenges = AuthenticationChallenge.parseHeader( |
| 25 "scheme1 realm=fblthp, scheme2 realm=asdfg"); |
| 26 expect(challenges, hasLength(2)); |
| 27 expect(challenges.first.scheme, equals("scheme1")); |
| 28 expect(challenges.first.parameters, equals({"realm": "fblthp"})); |
| 29 expect(challenges.last.scheme, equals("scheme2")); |
| 30 expect(challenges.last.parameters, equals({"realm": "asdfg"})); |
| 31 }); |
| 32 |
| 33 test("parses multiple challenges with multiple parameters", () { |
| 34 var challenges = AuthenticationChallenge.parseHeader( |
| 35 "scheme1 realm=fblthp, foo=bar, scheme2 realm=asdfg, baz=bang"); |
| 36 expect(challenges, hasLength(2)); |
| 37 |
| 38 expect(challenges.first.scheme, equals("scheme1")); |
| 39 expect(challenges.first.parameters, equals({ |
| 40 "realm": "fblthp", |
| 41 "foo": "bar" |
| 42 })); |
| 43 |
| 44 expect(challenges.last.scheme, equals("scheme2")); |
| 45 expect(challenges.last.parameters, equals({ |
| 46 "realm": "asdfg", |
| 47 "baz": "bang" |
| 48 })); |
| 49 }); |
| 50 }); |
| 51 } |
| 52 |
| 53 /// Tests to run for parsing a single challenge. |
| 54 /// |
| 55 /// These are run on both [AuthenticationChallenge.parse] and |
| 56 /// [AuthenticationChallenge.parseHeader], since they use almost entirely |
| 57 /// separate code paths. |
| 58 void _singleChallengeTests( |
| 59 AuthenticationChallenge parseChallenge(String challenge)) { |
| 60 test("parses a simple challenge", () { |
| 61 var challenge = parseChallenge("scheme realm=fblthp"); |
| 62 expect(challenge.scheme, equals("scheme")); |
| 63 expect(challenge.parameters, equals({"realm": "fblthp"})); |
| 64 }); |
| 65 |
| 66 test("parses multiple parameters", () { |
| 67 var challenge = parseChallenge("scheme realm=fblthp, foo=bar, baz=qux"); |
| 68 expect(challenge.scheme, equals("scheme")); |
| 69 expect(challenge.parameters, equals({ |
| 70 "realm": "fblthp", |
| 71 "foo": "bar", |
| 72 "baz": "qux" |
| 73 })); |
| 74 }); |
| 75 |
| 76 test("parses quoted string parameters", () { |
| 77 var challenge = parseChallenge('scheme realm="fblthp, foo=bar", baz="qux"'); |
| 78 expect(challenge.scheme, equals("scheme")); |
| 79 expect(challenge.parameters, equals({ |
| 80 "realm": "fblthp, foo=bar", |
| 81 "baz": "qux" |
| 82 })); |
| 83 }); |
| 84 |
| 85 test("normalizes the case of the scheme", () { |
| 86 var challenge = parseChallenge("ScHeMe realm=fblthp"); |
| 87 expect(challenge.scheme, equals("scheme")); |
| 88 expect(challenge.parameters, equals({"realm": "fblthp"})); |
| 89 }); |
| 90 |
| 91 test("normalizes the case of the parameter name", () { |
| 92 var challenge = parseChallenge("scheme ReAlM=fblthp"); |
| 93 expect(challenge.scheme, equals("scheme")); |
| 94 expect(challenge.parameters, containsPair("realm", "fblthp")); |
| 95 }); |
| 96 |
| 97 test("doesn't normalize the case of the parameter value", () { |
| 98 var challenge = parseChallenge("scheme realm=FbLtHp"); |
| 99 expect(challenge.scheme, equals("scheme")); |
| 100 expect(challenge.parameters, containsPair("realm", "FbLtHp")); |
| 101 expect(challenge.parameters, isNot(containsPair("realm", "fblthp"))); |
| 102 }); |
| 103 |
| 104 test("allows extra whitespace", () { |
| 105 var challenge = parseChallenge( |
| 106 " scheme\t \trealm\t = \tfblthp\t, \tfoo\t\r\n =\tbar\t"); |
| 107 expect(challenge.scheme, equals("scheme")); |
| 108 expect(challenge.parameters, equals({ |
| 109 "realm": "fblthp", |
| 110 "foo": "bar" |
| 111 })); |
| 112 }); |
| 113 |
| 114 test("allows an empty parameter", () { |
| 115 var challenge = parseChallenge( |
| 116 "scheme realm=fblthp, , foo=bar"); |
| 117 expect(challenge.scheme, equals("scheme")); |
| 118 expect(challenge.parameters, equals({ |
| 119 "realm": "fblthp", |
| 120 "foo": "bar" |
| 121 })); |
| 122 }); |
| 123 |
| 124 test("allows a leading comma", () { |
| 125 var challenge = parseChallenge( |
| 126 "scheme , realm=fblthp, foo=bar,"); |
| 127 expect(challenge.scheme, equals("scheme")); |
| 128 expect(challenge.parameters, equals({ |
| 129 "realm": "fblthp", |
| 130 "foo": "bar" |
| 131 })); |
| 132 }); |
| 133 |
| 134 test("allows a trailing comma", () { |
| 135 var challenge = parseChallenge( |
| 136 "scheme realm=fblthp, foo=bar, ,"); |
| 137 expect(challenge.scheme, equals("scheme")); |
| 138 expect(challenge.parameters, equals({ |
| 139 "realm": "fblthp", |
| 140 "foo": "bar" |
| 141 })); |
| 142 }); |
| 143 |
| 144 test("disallows only a scheme", () { |
| 145 expect(() => parseChallenge("scheme"), |
| 146 throwsFormatException); |
| 147 }); |
| 148 |
| 149 test("disallows a valueless parameter", () { |
| 150 expect(() => parseChallenge("scheme realm"), |
| 151 throwsFormatException); |
| 152 expect(() => parseChallenge("scheme realm="), |
| 153 throwsFormatException); |
| 154 expect(() => parseChallenge("scheme realm, foo=bar"), |
| 155 throwsFormatException); |
| 156 }); |
| 157 |
| 158 test("requires a space after the scheme", () { |
| 159 expect(() => parseChallenge("scheme\trealm"), |
| 160 throwsFormatException); |
| 161 expect(() => parseChallenge("scheme\r\n\trealm="), |
| 162 throwsFormatException); |
| 163 }); |
| 164 |
| 165 test("disallows junk after the parameters", () { |
| 166 expect(() => parseChallenge("scheme realm=fblthp foo"), |
| 167 throwsFormatException); |
| 168 expect(() => parseChallenge("scheme realm=fblthp, foo=bar baz"), |
| 169 throwsFormatException); |
| 170 }); |
| 171 } |
OLD | NEW |