OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 http_test; | 5 library http_test; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
(...skipping 10 matching lines...) Expand all Loading... |
21 expect(response.statusCode, equals(200)); | 21 expect(response.statusCode, equals(200)); |
22 expect(response.body, equals('')); | 22 expect(response.body, equals('')); |
23 }), completes); | 23 }), completes); |
24 }), completes); | 24 }), completes); |
25 }); | 25 }); |
26 | 26 |
27 test('get', () { | 27 test('get', () { |
28 expect(startServer().then((_) { | 28 expect(startServer().then((_) { |
29 expect(http.get(serverUrl, headers: { | 29 expect(http.get(serverUrl, headers: { |
30 'X-Random-Header': 'Value', | 30 'X-Random-Header': 'Value', |
31 'X-Other-Header': 'Other Value' | 31 'X-Other-Header': 'Other Value', |
| 32 'User-Agent': 'Dart' |
32 }).then((response) { | 33 }).then((response) { |
33 expect(response.statusCode, equals(200)); | 34 expect(response.statusCode, equals(200)); |
34 expect(response.body, parse(equals({ | 35 expect(response.body, parse(equals({ |
35 'method': 'GET', | 36 'method': 'GET', |
36 'path': '/', | 37 'path': '/', |
37 'headers': { | 38 'headers': { |
38 'content-length': ['0'], | 39 'content-length': ['0'], |
39 'accept-encoding': ['gzip'], | 40 'accept-encoding': ['gzip'], |
| 41 'user-agent': ['Dart'], |
40 'x-random-header': ['Value'], | 42 'x-random-header': ['Value'], |
41 'x-other-header': ['Other Value'] | 43 'x-other-header': ['Other Value'] |
42 }, | 44 }, |
43 }))); | 45 }))); |
44 }), completes); | 46 }), completes); |
45 }), completes); | 47 }), completes); |
46 }); | 48 }); |
47 | 49 |
48 test('post', () { | 50 test('post', () { |
49 expect(startServer().then((_) { | 51 expect(startServer().then((_) { |
50 expect(http.post(serverUrl, headers: { | 52 expect(http.post(serverUrl, headers: { |
51 'X-Random-Header': 'Value', | 53 'X-Random-Header': 'Value', |
52 'X-Other-Header': 'Other Value' | 54 'X-Other-Header': 'Other Value', |
| 55 'User-Agent': 'Dart' |
53 }, fields: { | 56 }, fields: { |
54 'some-field': 'value', | 57 'some-field': 'value', |
55 'other-field': 'other value' | 58 'other-field': 'other value' |
56 }).then((response) { | 59 }).then((response) { |
57 expect(response.statusCode, equals(200)); | 60 expect(response.statusCode, equals(200)); |
58 expect(response.body, parse(equals({ | 61 expect(response.body, parse(equals({ |
59 'method': 'POST', | 62 'method': 'POST', |
60 'path': '/', | 63 'path': '/', |
61 'headers': { | 64 'headers': { |
62 'content-type': [ | 65 'content-type': [ |
63 'application/x-www-form-urlencoded; charset=utf-8' | 66 'application/x-www-form-urlencoded; charset=utf-8' |
64 ], | 67 ], |
65 'content-length': ['40'], | 68 'content-length': ['40'], |
66 'accept-encoding': ['gzip'], | 69 'accept-encoding': ['gzip'], |
| 70 'user-agent': ['Dart'], |
67 'x-random-header': ['Value'], | 71 'x-random-header': ['Value'], |
68 'x-other-header': ['Other Value'] | 72 'x-other-header': ['Other Value'] |
69 }, | 73 }, |
70 'body': 'some-field=value&other-field=other+value' | 74 'body': 'some-field=value&other-field=other+value' |
71 }))); | 75 }))); |
72 }), completes); | 76 }), completes); |
73 }), completes); | 77 }), completes); |
74 }); | 78 }); |
75 | 79 |
76 test('post without fields', () { | 80 test('post without fields', () { |
77 expect(startServer().then((_) { | 81 expect(startServer().then((_) { |
78 expect(http.post(serverUrl, headers: { | 82 expect(http.post(serverUrl, headers: { |
79 'X-Random-Header': 'Value', | 83 'X-Random-Header': 'Value', |
80 'X-Other-Header': 'Other Value', | 84 'X-Other-Header': 'Other Value', |
81 'Content-Type': 'text/plain' | 85 'Content-Type': 'text/plain', |
| 86 'User-Agent': 'Dart' |
82 }).then((response) { | 87 }).then((response) { |
83 expect(response.statusCode, equals(200)); | 88 expect(response.statusCode, equals(200)); |
84 expect(response.body, parse(equals({ | 89 expect(response.body, parse(equals({ |
85 'method': 'POST', | 90 'method': 'POST', |
86 'path': '/', | 91 'path': '/', |
87 'headers': { | 92 'headers': { |
88 'accept-encoding': ['gzip'], | 93 'accept-encoding': ['gzip'], |
89 'content-length': ['0'], | 94 'content-length': ['0'], |
90 'content-type': ['text/plain'], | 95 'content-type': ['text/plain'], |
| 96 'user-agent': ['Dart'], |
91 'x-random-header': ['Value'], | 97 'x-random-header': ['Value'], |
92 'x-other-header': ['Other Value'] | 98 'x-other-header': ['Other Value'] |
93 } | 99 } |
94 }))); | 100 }))); |
95 }), completes); | 101 }), completes); |
96 }), completes); | 102 }), completes); |
97 }); | 103 }); |
98 | 104 |
99 test('put', () { | 105 test('put', () { |
100 expect(startServer().then((_) { | 106 expect(startServer().then((_) { |
101 expect(http.put(serverUrl, headers: { | 107 expect(http.put(serverUrl, headers: { |
102 'X-Random-Header': 'Value', | 108 'X-Random-Header': 'Value', |
103 'X-Other-Header': 'Other Value' | 109 'X-Other-Header': 'Other Value', |
| 110 'User-Agent': 'Dart' |
104 }, fields: { | 111 }, fields: { |
105 'some-field': 'value', | 112 'some-field': 'value', |
106 'other-field': 'other value' | 113 'other-field': 'other value' |
107 }).then((response) { | 114 }).then((response) { |
108 expect(response.statusCode, equals(200)); | 115 expect(response.statusCode, equals(200)); |
109 expect(response.body, parse(equals({ | 116 expect(response.body, parse(equals({ |
110 'method': 'PUT', | 117 'method': 'PUT', |
111 'path': '/', | 118 'path': '/', |
112 'headers': { | 119 'headers': { |
113 'content-type': [ | 120 'content-type': [ |
114 'application/x-www-form-urlencoded; charset=utf-8' | 121 'application/x-www-form-urlencoded; charset=utf-8' |
115 ], | 122 ], |
116 'accept-encoding': ['gzip'], | 123 'accept-encoding': ['gzip'], |
117 'content-length': ['40'], | 124 'content-length': ['40'], |
| 125 'user-agent': ['Dart'], |
118 'x-random-header': ['Value'], | 126 'x-random-header': ['Value'], |
119 'x-other-header': ['Other Value'] | 127 'x-other-header': ['Other Value'] |
120 }, | 128 }, |
121 'body': 'some-field=value&other-field=other+value' | 129 'body': 'some-field=value&other-field=other+value' |
122 }))); | 130 }))); |
123 }), completes); | 131 }), completes); |
124 }), completes); | 132 }), completes); |
125 }); | 133 }); |
126 | 134 |
127 test('put without fields', () { | 135 test('put without fields', () { |
128 expect(startServer().then((_) { | 136 expect(startServer().then((_) { |
129 expect(http.put(serverUrl, headers: { | 137 expect(http.put(serverUrl, headers: { |
130 'X-Random-Header': 'Value', | 138 'X-Random-Header': 'Value', |
131 'X-Other-Header': 'Other Value', | 139 'X-Other-Header': 'Other Value', |
132 'Content-Type': 'text/plain' | 140 'Content-Type': 'text/plain', |
| 141 'User-Agent': 'Dart' |
133 }).then((response) { | 142 }).then((response) { |
134 expect(response.statusCode, equals(200)); | 143 expect(response.statusCode, equals(200)); |
135 expect(response.body, parse(equals({ | 144 expect(response.body, parse(equals({ |
136 'method': 'PUT', | 145 'method': 'PUT', |
137 'path': '/', | 146 'path': '/', |
138 'headers': { | 147 'headers': { |
139 'content-length': ['0'], | 148 'content-length': ['0'], |
140 'accept-encoding': ['gzip'], | 149 'accept-encoding': ['gzip'], |
141 'content-type': ['text/plain'], | 150 'content-type': ['text/plain'], |
| 151 'user-agent': ['Dart'], |
142 'x-random-header': ['Value'], | 152 'x-random-header': ['Value'], |
143 'x-other-header': ['Other Value'] | 153 'x-other-header': ['Other Value'] |
144 } | 154 } |
145 }))); | 155 }))); |
146 }), completes); | 156 }), completes); |
147 }), completes); | 157 }), completes); |
148 }); | 158 }); |
149 | 159 |
150 test('delete', () { | 160 test('delete', () { |
151 expect(startServer().then((_) { | 161 expect(startServer().then((_) { |
152 expect(http.delete(serverUrl, headers: { | 162 expect(http.delete(serverUrl, headers: { |
153 'X-Random-Header': 'Value', | 163 'X-Random-Header': 'Value', |
154 'X-Other-Header': 'Other Value' | 164 'X-Other-Header': 'Other Value', |
| 165 'User-Agent': 'Dart' |
155 }).then((response) { | 166 }).then((response) { |
156 expect(response.statusCode, equals(200)); | 167 expect(response.statusCode, equals(200)); |
157 expect(response.body, parse(equals({ | 168 expect(response.body, parse(equals({ |
158 'method': 'DELETE', | 169 'method': 'DELETE', |
159 'path': '/', | 170 'path': '/', |
160 'headers': { | 171 'headers': { |
161 'content-length': ['0'], | 172 'content-length': ['0'], |
162 'accept-encoding': ['gzip'], | 173 'accept-encoding': ['gzip'], |
| 174 'user-agent': ['Dart'], |
163 'x-random-header': ['Value'], | 175 'x-random-header': ['Value'], |
164 'x-other-header': ['Other Value'] | 176 'x-other-header': ['Other Value'] |
165 } | 177 } |
166 }))); | 178 }))); |
167 }), completes); | 179 }), completes); |
168 }), completes); | 180 }), completes); |
169 }); | 181 }); |
170 | 182 |
171 test('read', () { | 183 test('read', () { |
172 expect(startServer().then((_) { | 184 expect(startServer().then((_) { |
173 expect(http.read(serverUrl, headers: { | 185 expect(http.read(serverUrl, headers: { |
174 'X-Random-Header': 'Value', | 186 'X-Random-Header': 'Value', |
175 'X-Other-Header': 'Other Value' | 187 'X-Other-Header': 'Other Value', |
| 188 'User-Agent': 'Dart' |
176 }).then((val) => val), completion(parse(equals({ | 189 }).then((val) => val), completion(parse(equals({ |
177 'method': 'GET', | 190 'method': 'GET', |
178 'path': '/', | 191 'path': '/', |
179 'headers': { | 192 'headers': { |
180 'content-length': ['0'], | 193 'content-length': ['0'], |
181 'accept-encoding': ['gzip'], | 194 'accept-encoding': ['gzip'], |
| 195 'user-agent': ['Dart'], |
182 'x-random-header': ['Value'], | 196 'x-random-header': ['Value'], |
183 'x-other-header': ['Other Value'] | 197 'x-other-header': ['Other Value'] |
184 }, | 198 }, |
185 })))); | 199 })))); |
186 }), completes); | 200 }), completes); |
187 }); | 201 }); |
188 | 202 |
189 test('read throws an error for a 4** status code', () { | 203 test('read throws an error for a 4** status code', () { |
190 expect(startServer().then((_) { | 204 expect(startServer().then((_) { |
191 expect(http.read(serverUrl.resolve('/error')), throwsHttpException); | 205 expect(http.read(serverUrl.resolve('/error')), throwsHttpException); |
192 }), completes); | 206 }), completes); |
193 }); | 207 }); |
194 | 208 |
195 test('readBytes', () { | 209 test('readBytes', () { |
196 expect(startServer().then((_) { | 210 expect(startServer().then((_) { |
197 var future = http.readBytes(serverUrl, headers: { | 211 var future = http.readBytes(serverUrl, headers: { |
198 'X-Random-Header': 'Value', | 212 'X-Random-Header': 'Value', |
199 'X-Other-Header': 'Other Value' | 213 'X-Other-Header': 'Other Value', |
| 214 'User-Agent': 'Dart' |
200 }).then((bytes) => new String.fromCharCodes(bytes)); | 215 }).then((bytes) => new String.fromCharCodes(bytes)); |
201 | 216 |
202 expect(future, completion(parse(equals({ | 217 expect(future, completion(parse(equals({ |
203 'method': 'GET', | 218 'method': 'GET', |
204 'path': '/', | 219 'path': '/', |
205 'headers': { | 220 'headers': { |
206 'content-length': ['0'], | 221 'content-length': ['0'], |
207 'accept-encoding': ['gzip'], | 222 'accept-encoding': ['gzip'], |
| 223 'user-agent': ['Dart'], |
208 'x-random-header': ['Value'], | 224 'x-random-header': ['Value'], |
209 'x-other-header': ['Other Value'] | 225 'x-other-header': ['Other Value'] |
210 }, | 226 }, |
211 })))); | 227 })))); |
212 }), completes); | 228 }), completes); |
213 }); | 229 }); |
214 | 230 |
215 test('readBytes throws an error for a 4** status code', () { | 231 test('readBytes throws an error for a 4** status code', () { |
216 expect(startServer().then((_) { | 232 expect(startServer().then((_) { |
217 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException)
; | 233 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException)
; |
218 }), completes); | 234 }), completes); |
219 }); | 235 }); |
220 }); | 236 }); |
221 } | 237 } |
OLD | NEW |