OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library shelf.request_test; | 5 library shelf.request_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | |
9 | 8 |
10 import 'package:shelf/shelf.dart'; | 9 import 'package:shelf/shelf.dart'; |
11 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
12 | 11 |
12 final _localhostUri = Uri.parse('http://localhost/'); | |
13 | |
13 Request _request([Map<String, String> headers, Stream<List<int>> body]) { | 14 Request _request([Map<String, String> headers, Stream<List<int>> body]) { |
14 if (headers == null) headers = {}; | 15 if (headers == null) headers = {}; |
15 return new Request("/", "", "GET", "", "1.1", Uri.parse('http://localhost/'), | 16 return new Request("1.1", "GET", headers, _localhostUri, |
16 headers, body: body); | 17 body: body); |
17 } | 18 } |
18 | 19 |
19 void main() { | 20 void main() { |
20 group("contentLength", () { | 21 group('constructor', () { |
21 test("is null without a content-length header", () { | 22 test('requestedUri must be absolute', () { |
22 var request = _request(); | 23 expect(() => new Request("1.1", 'GET', {}, Uri.parse('/path')), |
23 expect(request.contentLength, isNull); | 24 throwsArgumentError); |
24 }); | 25 }); |
25 | 26 |
26 test("comes from the content-length header", () { | 27 test('if uri is null, scriptName must be null', () { |
27 var request = _request({ | 28 expect(() => new Request("1.1", 'GET', {}, Uri.parse('/path'), |
28 'content-length': '42' | 29 scriptName: '/script/name'), throwsArgumentError); |
29 }); | 30 }); |
30 expect(request.contentLength, 42); | 31 |
32 test('if scriptName is null, uri must be null', () { | |
33 var relativeUri = new Uri(path: '/cool/beans.html'); | |
34 expect(() => new Request("1.1", 'GET', {}, Uri.parse('/path'), | |
35 uri: relativeUri), throwsArgumentError); | |
36 }); | |
37 | |
38 test('uri must be relative', () { | |
39 var relativeUri = Uri.parse('http://localhost/test'); | |
40 | |
41 expect(() => new Request("1.1", 'GET', {}, _localhostUri, | |
42 uri: relativeUri, scriptName: '/news'), | |
43 throwsArgumentError); | |
44 | |
45 // NOTE: explicitly testing fragments due to Issue 18053 | |
46 relativeUri = Uri.parse('http://localhost/test#fragment'); | |
47 | |
48 expect(() => new Request("1.1", 'GET', {}, _localhostUri, | |
49 uri: relativeUri, scriptName: '/news'), | |
50 throwsArgumentError); | |
51 }); | |
52 | |
53 test('uri and scriptName', () { | |
54 var pathInfo = '/pages/page.html?utm_source=ABC123'; | |
55 var scriptName = '/assets/static'; | |
56 var fullUrl = 'http://localhost$scriptName$pathInfo'; | |
57 var request = new Request('1.1', 'GET', {}, Uri.parse(fullUrl), | |
58 uri: Uri.parse(pathInfo), scriptName: scriptName); | |
nweiz
2014/04/08 20:23:09
Testing this with the same data in [requestedUrl]
kevmoo
2014/04/08 21:41:13
Done.
| |
59 | |
60 expect(request.scriptName, scriptName); | |
61 expect(request.pathInfo, '/pages/page.html'); | |
62 expect(request.queryString, 'utm_source=ABC123'); | |
63 }); | |
64 | |
65 test('minimal uri', () { | |
66 var pathInfo = '/'; | |
67 var scriptName = '/assets/static'; | |
68 var fullUrl = 'http://localhost$scriptName$pathInfo'; | |
69 var request = new Request('1.1', 'GET', {}, Uri.parse(fullUrl), | |
70 uri: Uri.parse(pathInfo), scriptName: scriptName); | |
71 | |
72 expect(request.scriptName, scriptName); | |
73 expect(request.pathInfo, '/'); | |
74 expect(request.queryString, ''); | |
75 }); | |
76 | |
77 test('bad uri', () { | |
nweiz
2014/04/08 20:23:09
"bad" isn't very descriptive. What's bad about it?
kevmoo
2014/04/08 21:41:13
Done.
| |
78 var pathInfo = 'page'; | |
79 var scriptName = '/assets/static'; | |
80 var fullUrl = 'http://localhost$scriptName$pathInfo'; | |
81 | |
82 expect(() => new Request('1.1', 'GET', {}, Uri.parse(fullUrl), | |
83 uri: Uri.parse(pathInfo), scriptName: scriptName), | |
84 throwsArgumentError); | |
85 }); | |
86 | |
87 test('bad scriptName', () { | |
88 var pathInfo = '/page'; | |
89 var scriptName = 'assets/static'; | |
90 var fullUrl = 'http://localhost/assets/static/pages'; | |
91 | |
92 expect(() => new Request('1.1', 'GET', {}, Uri.parse(fullUrl), | |
93 uri: Uri.parse(pathInfo), scriptName: scriptName), | |
94 throwsArgumentError); | |
95 | |
96 pathInfo = '/assets/static/page'; | |
nweiz
2014/04/08 20:23:09
If you're testing multiple possible errors, put th
kevmoo
2014/04/08 21:41:13
Done.
| |
97 scriptName = '/'; | |
98 fullUrl = 'http://localhost/assets/static/pages'; | |
99 | |
100 expect(() => new Request('1.1', 'GET', {}, Uri.parse(fullUrl), | |
101 uri: Uri.parse(pathInfo), scriptName: scriptName), | |
102 throwsArgumentError); | |
31 }); | 103 }); |
32 }); | 104 }); |
33 | 105 |
34 group("ifModifiedSince", () { | 106 group("ifModifiedSince", () { |
35 test("is null without an If-Modified-Since header", () { | 107 test("is null without an If-Modified-Since header", () { |
36 var request = _request(); | 108 var request = _request(); |
37 expect(request.ifModifiedSince, isNull); | 109 expect(request.ifModifiedSince, isNull); |
38 }); | 110 }); |
39 | 111 |
40 test("comes from the Last-Modified header", () { | 112 test("comes from the Last-Modified header", () { |
41 var request = _request({ | 113 var request = _request({ |
42 'if-modified-since': 'Sun, 06 Nov 1994 08:49:37 GMT' | 114 'if-modified-since': 'Sun, 06 Nov 1994 08:49:37 GMT' |
43 }); | 115 }); |
44 expect(request.ifModifiedSince, | 116 expect(request.ifModifiedSince, |
45 equals(DateTime.parse("1994-11-06 08:49:37z"))); | 117 equals(DateTime.parse("1994-11-06 08:49:37z"))); |
46 }); | 118 }); |
47 }); | 119 }); |
48 | 120 |
49 group("readAsString", () { | 121 group("readAsString", () { |
nweiz
2014/04/08 20:23:09
I thought the reason you added message_test was so
kevmoo
2014/04/08 21:41:13
Request and Response have different logic for bodi
| |
50 test("supports a null body", () { | 122 test("supports a null body", () { |
51 var request = _request(); | 123 var request = _request(); |
52 expect(request.readAsString(), completion(equals(""))); | 124 expect(request.readAsString(), completion(equals(""))); |
53 }); | 125 }); |
54 | 126 |
55 test("supports a Stream<List<int>> body", () { | 127 test("supports a Stream<List<int>> body", () { |
56 var controller = new StreamController(); | 128 var controller = new StreamController(); |
57 var request = _request({}, controller.stream); | 129 var request = _request({}, controller.stream); |
58 expect(request.readAsString(), completion(equals("hello, world"))); | 130 expect(request.readAsString(), completion(equals("hello, world"))); |
59 | 131 |
60 controller.add([104, 101, 108, 108, 111, 44]); | 132 controller.add([104, 101, 108, 108, 111, 44]); |
61 return new Future(() { | 133 return new Future(() { |
62 controller | 134 controller |
63 ..add([32, 119, 111, 114, 108, 100]) | 135 ..add([32, 119, 111, 114, 108, 100]) |
64 ..close(); | 136 ..close(); |
65 }); | 137 }); |
66 }); | 138 }); |
67 | |
68 test("defaults to UTF-8", () { | |
69 var request = _request({}, new Stream.fromIterable([[195, 168]])); | |
70 expect(request.readAsString(), completion(equals("è"))); | |
71 }); | |
72 | |
73 test("the content-type header overrides the default", () { | |
74 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'}, | |
75 new Stream.fromIterable([[195, 168]])); | |
76 expect(request.readAsString(), completion(equals("è"))); | |
77 }); | |
78 | |
79 test("an explicit encoding overrides the content-type header", () { | |
80 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'}, | |
81 new Stream.fromIterable([[195, 168]])); | |
82 expect(request.readAsString(LATIN1), completion(equals("è"))); | |
83 }); | |
84 }); | 139 }); |
85 | 140 |
86 group("read", () { | 141 group("read", () { |
87 test("supports a null body", () { | 142 test("supports a null body", () { |
88 var request = _request(); | 143 var request = _request(); |
89 expect(request.read().toList(), completion(isEmpty)); | 144 expect(request.read().toList(), completion(isEmpty)); |
90 }); | 145 }); |
91 | 146 |
92 test("supports a Stream<List<int>> body", () { | 147 test("supports a Stream<List<int>> body", () { |
93 var controller = new StreamController(); | 148 var controller = new StreamController(); |
94 var request = _request({}, controller.stream); | 149 var request = _request({}, controller.stream); |
95 expect(request.read().toList(), completion(equals([ | 150 expect(request.read().toList(), completion(equals([ |
96 [104, 101, 108, 108, 111, 44], | 151 [104, 101, 108, 108, 111, 44], |
97 [32, 119, 111, 114, 108, 100] | 152 [32, 119, 111, 114, 108, 100] |
98 ]))); | 153 ]))); |
99 | 154 |
100 controller.add([104, 101, 108, 108, 111, 44]); | 155 controller.add([104, 101, 108, 108, 111, 44]); |
101 return new Future(() { | 156 return new Future(() { |
102 controller | 157 controller |
103 ..add([32, 119, 111, 114, 108, 100]) | 158 ..add([32, 119, 111, 114, 108, 100]) |
104 ..close(); | 159 ..close(); |
105 }); | 160 }); |
106 }); | 161 }); |
107 }); | 162 }); |
108 | |
109 group("mimeType", () { | |
110 test("is null without a content-type header", () { | |
111 expect(_request().mimeType, isNull); | |
112 }); | |
113 | |
114 test("comes from the content-type header", () { | |
115 expect(_request({ | |
116 'content-type': 'text/plain' | |
117 }).mimeType, equals('text/plain')); | |
118 }); | |
119 | |
120 test("doesn't include parameters", () { | |
121 expect(_request({ | |
122 'content-type': 'text/plain; foo=bar; bar=baz' | |
123 }).mimeType, equals('text/plain')); | |
124 }); | |
125 }); | |
126 | |
127 group("encoding", () { | |
128 test("is null without a content-type header", () { | |
129 expect(_request().encoding, isNull); | |
130 }); | |
131 | |
132 test("is null without a charset parameter", () { | |
133 expect(_request({ | |
134 'content-type': 'text/plain' | |
135 }).encoding, isNull); | |
136 }); | |
137 | |
138 test("is null with an unrecognized charset parameter", () { | |
139 expect(_request({ | |
140 'content-type': 'text/plain; charset=fblthp' | |
141 }).encoding, isNull); | |
142 }); | |
143 | |
144 test("comes from the content-type charset parameter", () { | |
145 expect(_request({ | |
146 'content-type': 'text/plain; charset=iso-8859-1' | |
147 }).encoding, equals(LATIN1)); | |
148 }); | |
149 }); | |
150 } | 163 } |
OLD | NEW |