Chromium Code Reviews| 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 }); | |
|
Bob Nystrom
2015/08/25 21:28:59
Test that it doesn't normalize the parameter value
nweiz
2015/08/25 22:02:08
Done.
| |
| 96 | |
| 97 test("allows extra whitespace", () { | |
| 98 var challenge = parseChallenge( | |
| 99 " scheme\t \trealm\t = \tfblthp\t, \tfoo\t\r\n =\tbar\t"); | |
| 100 expect(challenge.scheme, equals("scheme")); | |
| 101 expect(challenge.parameters, equals({ | |
| 102 "realm": "fblthp", | |
| 103 "foo": "bar" | |
| 104 })); | |
| 105 }); | |
| 106 | |
| 107 test("allows an empty parameter", () { | |
| 108 var challenge = parseChallenge( | |
| 109 "scheme realm=fblthp, , foo=bar"); | |
| 110 expect(challenge.scheme, equals("scheme")); | |
| 111 expect(challenge.parameters, equals({ | |
| 112 "realm": "fblthp", | |
| 113 "foo": "bar" | |
| 114 })); | |
| 115 }); | |
|
Bob Nystrom
2015/08/25 21:28:59
What about a trailing comma?
nweiz
2015/08/25 22:02:08
Done.
| |
| 116 | |
| 117 test("disallows only a scheme", () { | |
| 118 expect(() => parseChallenge("scheme"), | |
| 119 throwsFormatException); | |
| 120 }); | |
| 121 | |
| 122 test("disallows a valueless parameter", () { | |
| 123 expect(() => parseChallenge("scheme realm"), | |
| 124 throwsFormatException); | |
| 125 expect(() => parseChallenge("scheme realm="), | |
| 126 throwsFormatException); | |
| 127 expect(() => parseChallenge("scheme realm, foo=bar"), | |
| 128 throwsFormatException); | |
| 129 }); | |
| 130 | |
| 131 test("requires a space after the scheme", () { | |
| 132 expect(() => parseChallenge("scheme\trealm"), | |
| 133 throwsFormatException); | |
| 134 expect(() => parseChallenge("scheme\r\n\trealm="), | |
| 135 throwsFormatException); | |
| 136 }); | |
| 137 | |
| 138 test("disallows junk after the parameters", () { | |
| 139 expect(() => parseChallenge("scheme realm=fblthp foo"), | |
| 140 throwsFormatException); | |
| 141 expect(() => parseChallenge("scheme realm=fblthp, foo=bar baz"), | |
| 142 throwsFormatException); | |
| 143 }); | |
| 144 } | |
| OLD | NEW |