| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 curl_client_test; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:isolate'; | |
| 9 import 'dart:uri'; | |
| 10 | |
| 11 import '../../unittest/lib/unittest.dart'; | |
| 12 import '../lib/http.dart' as http; | |
| 13 import '../lib/src/utils.dart'; | |
| 14 import 'utils.dart'; | |
| 15 | |
| 16 void main() { | |
| 17 setUp(startServer); | |
| 18 tearDown(stopServer); | |
| 19 | |
| 20 test('head', () { | |
| 21 expect(new http.CurlClient().head(serverUrl).transform((response) { | |
| 22 expect(response.statusCode, equals(200)); | |
| 23 expect(response.body, equals('')); | |
| 24 }), completes); | |
| 25 }); | |
| 26 | |
| 27 test('get', () { | |
| 28 expect(new http.CurlClient().get(serverUrl, headers: { | |
| 29 'X-Random-Header': 'Value', | |
| 30 'X-Other-Header': 'Other Value' | |
| 31 }).transform((response) { | |
| 32 expect(response.statusCode, equals(200)); | |
| 33 expect(response.body, parse(equals({ | |
| 34 'method': 'GET', | |
| 35 'path': '/', | |
| 36 'headers': { | |
| 37 'x-random-header': ['Value'], | |
| 38 'x-other-header': ['Other Value'] | |
| 39 }, | |
| 40 }))); | |
| 41 }), completes); | |
| 42 }); | |
| 43 | |
| 44 test('post', () { | |
| 45 expect(new http.CurlClient().post(serverUrl, headers: { | |
| 46 'X-Random-Header': 'Value', | |
| 47 'X-Other-Header': 'Other Value' | |
| 48 }, fields: { | |
| 49 'some-field': 'value', | |
| 50 'other-field': 'other value' | |
| 51 }).transform((response) { | |
| 52 expect(response.statusCode, equals(200)); | |
| 53 expect(response.body, parse(equals({ | |
| 54 'method': 'POST', | |
| 55 'path': '/', | |
| 56 'headers': { | |
| 57 'content-type': [ | |
| 58 'application/x-www-form-urlencoded; charset=UTF-8' | |
| 59 ], | |
| 60 'content-length': ['42'], | |
| 61 'x-random-header': ['Value'], | |
| 62 'x-other-header': ['Other Value'] | |
| 63 }, | |
| 64 'body': 'some-field=value&other-field=other%20value' | |
| 65 }))); | |
| 66 }), completes); | |
| 67 }); | |
| 68 | |
| 69 test('post without fields', () { | |
| 70 expect(new http.CurlClient().post(serverUrl, headers: { | |
| 71 'X-Random-Header': 'Value', | |
| 72 'X-Other-Header': 'Other Value', | |
| 73 'Content-Type': 'text/plain' | |
| 74 }).transform((response) { | |
| 75 expect(response.statusCode, equals(200)); | |
| 76 expect(response.body, parse(equals({ | |
| 77 'method': 'POST', | |
| 78 'path': '/', | |
| 79 'headers': { | |
| 80 'content-type': ['text/plain'], | |
| 81 'x-random-header': ['Value'], | |
| 82 'x-other-header': ['Other Value'] | |
| 83 } | |
| 84 }))); | |
| 85 }), completes); | |
| 86 }); | |
| 87 | |
| 88 test('put', () { | |
| 89 expect(new http.CurlClient().put(serverUrl, headers: { | |
| 90 'X-Random-Header': 'Value', | |
| 91 'X-Other-Header': 'Other Value' | |
| 92 }, fields: { | |
| 93 'some-field': 'value', | |
| 94 'other-field': 'other value' | |
| 95 }).transform((response) { | |
| 96 expect(response.statusCode, equals(200)); | |
| 97 expect(response.body, parse(equals({ | |
| 98 'method': 'PUT', | |
| 99 'path': '/', | |
| 100 'headers': { | |
| 101 'content-type': [ | |
| 102 'application/x-www-form-urlencoded; charset=UTF-8' | |
| 103 ], | |
| 104 'content-length': ['42'], | |
| 105 'x-random-header': ['Value'], | |
| 106 'x-other-header': ['Other Value'] | |
| 107 }, | |
| 108 'body': 'some-field=value&other-field=other%20value' | |
| 109 }))); | |
| 110 }), completes); | |
| 111 }); | |
| 112 | |
| 113 test('put without fields', () { | |
| 114 expect(new http.CurlClient().put(serverUrl, headers: { | |
| 115 'X-Random-Header': 'Value', | |
| 116 'X-Other-Header': 'Other Value', | |
| 117 'Content-Type': 'text/plain' | |
| 118 }).transform((response) { | |
| 119 expect(response.statusCode, equals(200)); | |
| 120 expect(response.body, parse(equals({ | |
| 121 'method': 'PUT', | |
| 122 'path': '/', | |
| 123 'headers': { | |
| 124 'content-type': ['text/plain'], | |
| 125 'x-random-header': ['Value'], | |
| 126 'x-other-header': ['Other Value'] | |
| 127 } | |
| 128 }))); | |
| 129 }), completes); | |
| 130 }); | |
| 131 | |
| 132 test('delete', () { | |
| 133 expect(new http.CurlClient().delete(serverUrl, headers: { | |
| 134 'X-Random-Header': 'Value', | |
| 135 'X-Other-Header': 'Other Value' | |
| 136 }).transform((response) { | |
| 137 expect(response.statusCode, equals(200)); | |
| 138 expect(response.body, parse(equals({ | |
| 139 'method': 'DELETE', | |
| 140 'path': '/', | |
| 141 'headers': { | |
| 142 'x-random-header': ['Value'], | |
| 143 'x-other-header': ['Other Value'] | |
| 144 } | |
| 145 }))); | |
| 146 }), completes); | |
| 147 }); | |
| 148 | |
| 149 test('read', () { | |
| 150 expect(new http.CurlClient().read(serverUrl, headers: { | |
| 151 'X-Random-Header': 'Value', | |
| 152 'X-Other-Header': 'Other Value' | |
| 153 }), completion(parse(equals({ | |
| 154 'method': 'GET', | |
| 155 'path': '/', | |
| 156 'headers': { | |
| 157 'x-random-header': ['Value'], | |
| 158 'x-other-header': ['Other Value'] | |
| 159 }, | |
| 160 })))); | |
| 161 }); | |
| 162 | |
| 163 test('read throws an error for a 4** status code', () { | |
| 164 expect(new http.CurlClient().read(serverUrl.resolve('/error')), | |
| 165 throwsHttpException); | |
| 166 }); | |
| 167 | |
| 168 test('readBytes', () { | |
| 169 var future = new http.CurlClient().readBytes(serverUrl, headers: { | |
| 170 'X-Random-Header': 'Value', | |
| 171 'X-Other-Header': 'Other Value' | |
| 172 }).transform((bytes) => new String.fromCharCodes(bytes)); | |
| 173 | |
| 174 expect(future, completion(parse(equals({ | |
| 175 'method': 'GET', | |
| 176 'path': '/', | |
| 177 'headers': { | |
| 178 'x-random-header': ['Value'], | |
| 179 'x-other-header': ['Other Value'] | |
| 180 }, | |
| 181 })))); | |
| 182 }); | |
| 183 | |
| 184 test('readBytes throws an error for a 4** status code', () { | |
| 185 expect(new http.CurlClient().readBytes(serverUrl.resolve('/error')), | |
| 186 throwsHttpException); | |
| 187 }); | |
| 188 | |
| 189 test('#send a StreamedRequest', () { | |
| 190 var client = new http.CurlClient(); | |
| 191 var request = new http.StreamedRequest("POST", serverUrl); | |
| 192 request.headers[HttpHeaders.CONTENT_TYPE] = | |
| 193 'application/json; charset=utf-8'; | |
| 194 | |
| 195 var future = client.send(request).chain((response) { | |
| 196 expect(response.statusCode, equals(200)); | |
| 197 return consumeInputStream(response.stream); | |
| 198 }).transform((bytes) => new String.fromCharCodes(bytes)); | |
| 199 future.onComplete((_) => client.close()); | |
| 200 | |
| 201 expect(future, completion(parse(equals({ | |
| 202 'method': 'POST', | |
| 203 'path': '/', | |
| 204 'headers': { | |
| 205 'content-type': ['application/json; charset=utf-8'], | |
| 206 'transfer-encoding': ['chunked'] | |
| 207 }, | |
| 208 'body': '{"hello": "world"}' | |
| 209 })))); | |
| 210 | |
| 211 request.stream.writeString('{"hello": "world"}'); | |
| 212 request.stream.close(); | |
| 213 }); | |
| 214 | |
| 215 test('with one redirect', () { | |
| 216 var url = serverUrl.resolve('/redirect'); | |
| 217 expect(new http.CurlClient().get(url).transform((response) { | |
| 218 expect(response.statusCode, equals(200)); | |
| 219 expect(response.body, parse(equals({ | |
| 220 'method': 'GET', | |
| 221 'path': '/', | |
| 222 'headers': {} | |
| 223 }))); | |
| 224 }), completes); | |
| 225 }); | |
| 226 | |
| 227 test('with too many redirects', () { | |
| 228 expect(new http.CurlClient().get(serverUrl.resolve('/loop?1')), | |
| 229 throwsRedirectLimitExceededException); | |
| 230 }); | |
| 231 | |
| 232 test('with a generic failure', () { | |
| 233 expect(new http.CurlClient().get('url fail'), | |
| 234 throwsHttpException); | |
| 235 }); | |
| 236 | |
| 237 test('with one redirect via HEAD', () { | |
| 238 var url = serverUrl.resolve('/redirect'); | |
| 239 expect(new http.CurlClient().head(url).transform((response) { | |
| 240 expect(response.statusCode, equals(200)); | |
| 241 }), completes); | |
| 242 }); | |
| 243 | |
| 244 test('with too many redirects via HEAD', () { | |
| 245 expect(new http.CurlClient().head(serverUrl.resolve('/loop?1')), | |
| 246 throwsRedirectLimitExceededException); | |
| 247 }); | |
| 248 | |
| 249 test('with a generic failure via HEAD', () { | |
| 250 expect(new http.CurlClient().head('url fail'), | |
| 251 throwsHttpException); | |
| 252 }); | |
| 253 | |
| 254 test('without following redirects', () { | |
| 255 var request = new http.Request('GET', serverUrl.resolve('/redirect')); | |
| 256 request.followRedirects = false; | |
| 257 expect(new http.CurlClient().send(request).chain(http.Response.fromStream) | |
| 258 .transform((response) { | |
| 259 expect(response.statusCode, equals(302)); | |
| 260 expect(response.isRedirect, true); | |
| 261 }), completes); | |
| 262 }); | |
| 263 } | |
| OLD | NEW |