| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library http.test.io.http_test; | |
| 6 | |
| 7 import 'package:http/http.dart' as http; | |
| 8 import 'package:unittest/unittest.dart'; | |
| 9 | |
| 10 import 'utils.dart'; | |
| 11 | |
| 12 main() { | |
| 13 group('http.', () { | |
| 14 tearDown(stopServer); | |
| 15 | |
| 16 test('head', () { | |
| 17 expect(startServer().then((_) { | |
| 18 expect(http.head(serverUrl).then((response) { | |
| 19 expect(response.statusCode, equals(200)); | |
| 20 expect(response.body, equals('')); | |
| 21 }), completes); | |
| 22 }), completes); | |
| 23 }); | |
| 24 | |
| 25 test('get', () { | |
| 26 expect(startServer().then((_) { | |
| 27 expect(http.get(serverUrl, headers: { | |
| 28 'X-Random-Header': 'Value', | |
| 29 'X-Other-Header': 'Other Value', | |
| 30 'User-Agent': 'Dart' | |
| 31 }).then((response) { | |
| 32 expect(response.statusCode, equals(200)); | |
| 33 expect(response.body, parse(equals({ | |
| 34 'method': 'GET', | |
| 35 'path': '/', | |
| 36 'headers': { | |
| 37 'content-length': ['0'], | |
| 38 'accept-encoding': ['gzip'], | |
| 39 'user-agent': ['Dart'], | |
| 40 'x-random-header': ['Value'], | |
| 41 'x-other-header': ['Other Value'] | |
| 42 }, | |
| 43 }))); | |
| 44 }), completes); | |
| 45 }), completes); | |
| 46 }); | |
| 47 | |
| 48 test('post', () { | |
| 49 expect(startServer().then((_) { | |
| 50 expect(http.post(serverUrl, headers: { | |
| 51 'X-Random-Header': 'Value', | |
| 52 'X-Other-Header': 'Other Value', | |
| 53 'Content-Type': 'text/plain', | |
| 54 'User-Agent': 'Dart' | |
| 55 }).then((response) { | |
| 56 expect(response.statusCode, equals(200)); | |
| 57 expect(response.body, parse(equals({ | |
| 58 'method': 'POST', | |
| 59 'path': '/', | |
| 60 'headers': { | |
| 61 'accept-encoding': ['gzip'], | |
| 62 'content-length': ['0'], | |
| 63 'content-type': ['text/plain'], | |
| 64 'user-agent': ['Dart'], | |
| 65 'x-random-header': ['Value'], | |
| 66 'x-other-header': ['Other Value'] | |
| 67 } | |
| 68 }))); | |
| 69 }), completes); | |
| 70 }), completes); | |
| 71 }); | |
| 72 | |
| 73 test('post with string', () { | |
| 74 expect(startServer().then((_) { | |
| 75 expect(http.post(serverUrl, headers: { | |
| 76 'X-Random-Header': 'Value', | |
| 77 'X-Other-Header': 'Other Value', | |
| 78 'User-Agent': 'Dart' | |
| 79 }, body: 'request body').then((response) { | |
| 80 expect(response.statusCode, equals(200)); | |
| 81 expect(response.body, parse(equals({ | |
| 82 'method': 'POST', | |
| 83 'path': '/', | |
| 84 'headers': { | |
| 85 'content-type': ['text/plain; charset=utf-8'], | |
| 86 'content-length': ['12'], | |
| 87 'accept-encoding': ['gzip'], | |
| 88 'user-agent': ['Dart'], | |
| 89 'x-random-header': ['Value'], | |
| 90 'x-other-header': ['Other Value'] | |
| 91 }, | |
| 92 'body': 'request body' | |
| 93 }))); | |
| 94 }), completes); | |
| 95 }), completes); | |
| 96 }); | |
| 97 | |
| 98 test('post with bytes', () { | |
| 99 expect(startServer().then((_) { | |
| 100 expect(http.post(serverUrl, headers: { | |
| 101 'X-Random-Header': 'Value', | |
| 102 'X-Other-Header': 'Other Value', | |
| 103 'User-Agent': 'Dart' | |
| 104 }, body: [104, 101, 108, 108, 111]).then((response) { | |
| 105 expect(response.statusCode, equals(200)); | |
| 106 expect(response.body, parse(equals({ | |
| 107 'method': 'POST', | |
| 108 'path': '/', | |
| 109 'headers': { | |
| 110 'content-length': ['5'], | |
| 111 'accept-encoding': ['gzip'], | |
| 112 'user-agent': ['Dart'], | |
| 113 'x-random-header': ['Value'], | |
| 114 'x-other-header': ['Other Value'] | |
| 115 }, | |
| 116 'body': [104, 101, 108, 108, 111] | |
| 117 }))); | |
| 118 }), completes); | |
| 119 }), completes); | |
| 120 }); | |
| 121 | |
| 122 test('post with fields', () { | |
| 123 expect(startServer().then((_) { | |
| 124 expect(http.post(serverUrl, headers: { | |
| 125 'X-Random-Header': 'Value', | |
| 126 'X-Other-Header': 'Other Value', | |
| 127 'User-Agent': 'Dart' | |
| 128 }, body: { | |
| 129 'some-field': 'value', | |
| 130 'other-field': 'other value' | |
| 131 }).then((response) { | |
| 132 expect(response.statusCode, equals(200)); | |
| 133 expect(response.body, parse(equals({ | |
| 134 'method': 'POST', | |
| 135 'path': '/', | |
| 136 'headers': { | |
| 137 'content-type': [ | |
| 138 'application/x-www-form-urlencoded; charset=utf-8' | |
| 139 ], | |
| 140 'content-length': ['40'], | |
| 141 'accept-encoding': ['gzip'], | |
| 142 'user-agent': ['Dart'], | |
| 143 'x-random-header': ['Value'], | |
| 144 'x-other-header': ['Other Value'] | |
| 145 }, | |
| 146 'body': 'some-field=value&other-field=other+value' | |
| 147 }))); | |
| 148 }), completes); | |
| 149 }), completes); | |
| 150 }); | |
| 151 | |
| 152 test('put', () { | |
| 153 expect(startServer().then((_) { | |
| 154 expect(http.put(serverUrl, headers: { | |
| 155 'X-Random-Header': 'Value', | |
| 156 'X-Other-Header': 'Other Value', | |
| 157 'Content-Type': 'text/plain', | |
| 158 'User-Agent': 'Dart' | |
| 159 }).then((response) { | |
| 160 expect(response.statusCode, equals(200)); | |
| 161 expect(response.body, parse(equals({ | |
| 162 'method': 'PUT', | |
| 163 'path': '/', | |
| 164 'headers': { | |
| 165 'accept-encoding': ['gzip'], | |
| 166 'content-length': ['0'], | |
| 167 'content-type': ['text/plain'], | |
| 168 'user-agent': ['Dart'], | |
| 169 'x-random-header': ['Value'], | |
| 170 'x-other-header': ['Other Value'] | |
| 171 } | |
| 172 }))); | |
| 173 }), completes); | |
| 174 }), completes); | |
| 175 }); | |
| 176 | |
| 177 test('put with string', () { | |
| 178 expect(startServer().then((_) { | |
| 179 expect(http.put(serverUrl, headers: { | |
| 180 'X-Random-Header': 'Value', | |
| 181 'X-Other-Header': 'Other Value', | |
| 182 'User-Agent': 'Dart' | |
| 183 }, body: 'request body').then((response) { | |
| 184 expect(response.statusCode, equals(200)); | |
| 185 expect(response.body, parse(equals({ | |
| 186 'method': 'PUT', | |
| 187 'path': '/', | |
| 188 'headers': { | |
| 189 'content-type': ['text/plain; charset=utf-8'], | |
| 190 'content-length': ['12'], | |
| 191 'accept-encoding': ['gzip'], | |
| 192 'user-agent': ['Dart'], | |
| 193 'x-random-header': ['Value'], | |
| 194 'x-other-header': ['Other Value'] | |
| 195 }, | |
| 196 'body': 'request body' | |
| 197 }))); | |
| 198 }), completes); | |
| 199 }), completes); | |
| 200 }); | |
| 201 | |
| 202 test('put with bytes', () { | |
| 203 expect(startServer().then((_) { | |
| 204 expect(http.put(serverUrl, headers: { | |
| 205 'X-Random-Header': 'Value', | |
| 206 'X-Other-Header': 'Other Value', | |
| 207 'User-Agent': 'Dart' | |
| 208 }, body: [104, 101, 108, 108, 111]).then((response) { | |
| 209 expect(response.statusCode, equals(200)); | |
| 210 expect(response.body, parse(equals({ | |
| 211 'method': 'PUT', | |
| 212 'path': '/', | |
| 213 'headers': { | |
| 214 'content-length': ['5'], | |
| 215 'accept-encoding': ['gzip'], | |
| 216 'user-agent': ['Dart'], | |
| 217 'x-random-header': ['Value'], | |
| 218 'x-other-header': ['Other Value'] | |
| 219 }, | |
| 220 'body': [104, 101, 108, 108, 111] | |
| 221 }))); | |
| 222 }), completes); | |
| 223 }), completes); | |
| 224 }); | |
| 225 | |
| 226 test('put with fields', () { | |
| 227 expect(startServer().then((_) { | |
| 228 expect(http.put(serverUrl, headers: { | |
| 229 'X-Random-Header': 'Value', | |
| 230 'X-Other-Header': 'Other Value', | |
| 231 'User-Agent': 'Dart' | |
| 232 }, body: { | |
| 233 'some-field': 'value', | |
| 234 'other-field': 'other value' | |
| 235 }).then((response) { | |
| 236 expect(response.statusCode, equals(200)); | |
| 237 expect(response.body, parse(equals({ | |
| 238 'method': 'PUT', | |
| 239 'path': '/', | |
| 240 'headers': { | |
| 241 'content-type': [ | |
| 242 'application/x-www-form-urlencoded; charset=utf-8' | |
| 243 ], | |
| 244 'content-length': ['40'], | |
| 245 'accept-encoding': ['gzip'], | |
| 246 'user-agent': ['Dart'], | |
| 247 'x-random-header': ['Value'], | |
| 248 'x-other-header': ['Other Value'] | |
| 249 }, | |
| 250 'body': 'some-field=value&other-field=other+value' | |
| 251 }))); | |
| 252 }), completes); | |
| 253 }), completes); | |
| 254 }); | |
| 255 | |
| 256 test('delete', () { | |
| 257 expect(startServer().then((_) { | |
| 258 expect(http.delete(serverUrl, headers: { | |
| 259 'X-Random-Header': 'Value', | |
| 260 'X-Other-Header': 'Other Value', | |
| 261 'User-Agent': 'Dart' | |
| 262 }).then((response) { | |
| 263 expect(response.statusCode, equals(200)); | |
| 264 expect(response.body, parse(equals({ | |
| 265 'method': 'DELETE', | |
| 266 'path': '/', | |
| 267 'headers': { | |
| 268 'content-length': ['0'], | |
| 269 'accept-encoding': ['gzip'], | |
| 270 'user-agent': ['Dart'], | |
| 271 'x-random-header': ['Value'], | |
| 272 'x-other-header': ['Other Value'] | |
| 273 } | |
| 274 }))); | |
| 275 }), completes); | |
| 276 }), completes); | |
| 277 }); | |
| 278 | |
| 279 test('read', () { | |
| 280 expect(startServer().then((_) { | |
| 281 expect(http.read(serverUrl, headers: { | |
| 282 'X-Random-Header': 'Value', | |
| 283 'X-Other-Header': 'Other Value', | |
| 284 'User-Agent': 'Dart' | |
| 285 }).then((val) => val), completion(parse(equals({ | |
| 286 'method': 'GET', | |
| 287 'path': '/', | |
| 288 'headers': { | |
| 289 'content-length': ['0'], | |
| 290 'accept-encoding': ['gzip'], | |
| 291 'user-agent': ['Dart'], | |
| 292 'x-random-header': ['Value'], | |
| 293 'x-other-header': ['Other Value'] | |
| 294 }, | |
| 295 })))); | |
| 296 }), completes); | |
| 297 }); | |
| 298 | |
| 299 test('read throws an error for a 4** status code', () { | |
| 300 expect(startServer().then((_) { | |
| 301 expect(http.read(serverUrl.resolve('/error')), throwsClientException); | |
| 302 }), completes); | |
| 303 }); | |
| 304 | |
| 305 test('readBytes', () { | |
| 306 expect(startServer().then((_) { | |
| 307 var future = http.readBytes(serverUrl, headers: { | |
| 308 'X-Random-Header': 'Value', | |
| 309 'X-Other-Header': 'Other Value', | |
| 310 'User-Agent': 'Dart' | |
| 311 }).then((bytes) => new String.fromCharCodes(bytes)); | |
| 312 | |
| 313 expect(future, completion(parse(equals({ | |
| 314 'method': 'GET', | |
| 315 'path': '/', | |
| 316 'headers': { | |
| 317 'content-length': ['0'], | |
| 318 'accept-encoding': ['gzip'], | |
| 319 'user-agent': ['Dart'], | |
| 320 'x-random-header': ['Value'], | |
| 321 'x-other-header': ['Other Value'] | |
| 322 }, | |
| 323 })))); | |
| 324 }), completes); | |
| 325 }); | |
| 326 | |
| 327 test('readBytes throws an error for a 4** status code', () { | |
| 328 expect(startServer().then((_) { | |
| 329 expect(http.readBytes(serverUrl.resolve('/error')), | |
| 330 throwsClientException); | |
| 331 }), completes); | |
| 332 }); | |
| 333 }); | |
| 334 } | |
| OLD | NEW |