Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Unified Diff: test/authentication_challenge_test.dart

Issue 1307353003: Add an AuthenticationChallenge class. (Closed) Base URL: git@github.com:dart-lang/http_parser@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/src/authentication_challenge.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/authentication_challenge_test.dart
diff --git a/test/authentication_challenge_test.dart b/test/authentication_challenge_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ccb8d3230b0d98fc40aaa5569f7fba31f621df87
--- /dev/null
+++ b/test/authentication_challenge_test.dart
@@ -0,0 +1,144 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:http_parser/http_parser.dart';
+import 'package:test/test.dart';
+
+void main() {
+ group("parse", () {
+ _singleChallengeTests(
+ (challenge) => new AuthenticationChallenge.parse(challenge));
+ });
+
+ group("parseHeader", () {
+ group("with a single challenge", () {
+ _singleChallengeTests((challenge) {
+ var challenges = AuthenticationChallenge.parseHeader(challenge);
+ expect(challenges, hasLength(1));
+ return challenges.single;
+ });
+ });
+
+ test("parses multiple challenges", () {
+ var challenges = AuthenticationChallenge.parseHeader(
+ "scheme1 realm=fblthp, scheme2 realm=asdfg");
+ expect(challenges, hasLength(2));
+ expect(challenges.first.scheme, equals("scheme1"));
+ expect(challenges.first.parameters, equals({"realm": "fblthp"}));
+ expect(challenges.last.scheme, equals("scheme2"));
+ expect(challenges.last.parameters, equals({"realm": "asdfg"}));
+ });
+
+ test("parses multiple challenges with multiple parameters", () {
+ var challenges = AuthenticationChallenge.parseHeader(
+ "scheme1 realm=fblthp, foo=bar, scheme2 realm=asdfg, baz=bang");
+ expect(challenges, hasLength(2));
+
+ expect(challenges.first.scheme, equals("scheme1"));
+ expect(challenges.first.parameters, equals({
+ "realm": "fblthp",
+ "foo": "bar"
+ }));
+
+ expect(challenges.last.scheme, equals("scheme2"));
+ expect(challenges.last.parameters, equals({
+ "realm": "asdfg",
+ "baz": "bang"
+ }));
+ });
+ });
+}
+
+/// Tests to run for parsing a single challenge.
+///
+/// These are run on both [AuthenticationChallenge.parse] and
+/// [AuthenticationChallenge.parseHeader], since they use almost entirely
+/// separate code paths.
+void _singleChallengeTests(
+ AuthenticationChallenge parseChallenge(String challenge)) {
+ test("parses a simple challenge", () {
+ var challenge = parseChallenge("scheme realm=fblthp");
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, equals({"realm": "fblthp"}));
+ });
+
+ test("parses multiple parameters", () {
+ var challenge = parseChallenge("scheme realm=fblthp, foo=bar, baz=qux");
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, equals({
+ "realm": "fblthp",
+ "foo": "bar",
+ "baz": "qux"
+ }));
+ });
+
+ test("parses quoted string parameters", () {
+ var challenge = parseChallenge('scheme realm="fblthp, foo=bar", baz="qux"');
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, equals({
+ "realm": "fblthp, foo=bar",
+ "baz": "qux"
+ }));
+ });
+
+ test("normalizes the case of the scheme", () {
+ var challenge = parseChallenge("ScHeMe realm=fblthp");
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, equals({"realm": "fblthp"}));
+ });
+
+ test("normalizes the case of the parameter name", () {
+ var challenge = parseChallenge("scheme ReAlM=fblthp");
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, containsPair("realm", "fblthp"));
+ });
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.
+
+ test("allows extra whitespace", () {
+ var challenge = parseChallenge(
+ " scheme\t \trealm\t = \tfblthp\t, \tfoo\t\r\n =\tbar\t");
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, equals({
+ "realm": "fblthp",
+ "foo": "bar"
+ }));
+ });
+
+ test("allows an empty parameter", () {
+ var challenge = parseChallenge(
+ "scheme realm=fblthp, , foo=bar");
+ expect(challenge.scheme, equals("scheme"));
+ expect(challenge.parameters, equals({
+ "realm": "fblthp",
+ "foo": "bar"
+ }));
+ });
Bob Nystrom 2015/08/25 21:28:59 What about a trailing comma?
nweiz 2015/08/25 22:02:08 Done.
+
+ test("disallows only a scheme", () {
+ expect(() => parseChallenge("scheme"),
+ throwsFormatException);
+ });
+
+ test("disallows a valueless parameter", () {
+ expect(() => parseChallenge("scheme realm"),
+ throwsFormatException);
+ expect(() => parseChallenge("scheme realm="),
+ throwsFormatException);
+ expect(() => parseChallenge("scheme realm, foo=bar"),
+ throwsFormatException);
+ });
+
+ test("requires a space after the scheme", () {
+ expect(() => parseChallenge("scheme\trealm"),
+ throwsFormatException);
+ expect(() => parseChallenge("scheme\r\n\trealm="),
+ throwsFormatException);
+ });
+
+ test("disallows junk after the parameters", () {
+ expect(() => parseChallenge("scheme realm=fblthp foo"),
+ throwsFormatException);
+ expect(() => parseChallenge("scheme realm=fblthp, foo=bar baz"),
+ throwsFormatException);
+ });
+}
« lib/src/authentication_challenge.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698