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