| 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 http_test; | 5 library http_test; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| 11 import 'utils.dart'; | 11 import 'utils.dart'; |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 group('http.', () { | 14 group('http.', () { |
| 15 setUp(startServer); | 15 setUp(startServer); |
| 16 tearDown(stopServer); | 16 tearDown(stopServer); |
| 17 | 17 |
| 18 test('head', () { | 18 test('head', () { |
| 19 expect(http.head(serverUrl).transform((response) { | 19 expect(http.head(serverUrl).then(expectAsync1((response) { |
| 20 expect(response.statusCode, equals(200)); | 20 expect(response.statusCode, equals(200)); |
| 21 expect(response.body, equals('')); | 21 expect(response.body, equals('')); |
| 22 }), completes); | 22 })), completes); |
| 23 }); | 23 }); |
| 24 | 24 |
| 25 test('get', () { | 25 test('get', () { |
| 26 expect(http.get(serverUrl, headers: { | 26 expect(http.get(serverUrl, headers: { |
| 27 'X-Random-Header': 'Value', | 27 'X-Random-Header': 'Value', |
| 28 'X-Other-Header': 'Other Value' | 28 'X-Other-Header': 'Other Value' |
| 29 }).transform((response) { | 29 }).then(expectAsync1((response) { |
| 30 expect(response.statusCode, equals(200)); | 30 expect(response.statusCode, equals(200)); |
| 31 expect(response.body, parse(equals({ | 31 expect(response.body, parse(equals({ |
| 32 'method': 'GET', | 32 'method': 'GET', |
| 33 'path': '/', | 33 'path': '/', |
| 34 'headers': { | 34 'headers': { |
| 35 'content-length': ['0'], | 35 'content-length': ['0'], |
| 36 'x-random-header': ['Value'], | 36 'x-random-header': ['Value'], |
| 37 'x-other-header': ['Other Value'] | 37 'x-other-header': ['Other Value'] |
| 38 }, | 38 }, |
| 39 }))); | 39 }))); |
| 40 }), completes); | 40 })), completes); |
| 41 }); | 41 }); |
| 42 | 42 |
| 43 test('post', () { | 43 test('post', () { |
| 44 expect(http.post(serverUrl, headers: { | 44 expect(http.post(serverUrl, headers: { |
| 45 'X-Random-Header': 'Value', | 45 'X-Random-Header': 'Value', |
| 46 'X-Other-Header': 'Other Value' | 46 'X-Other-Header': 'Other Value' |
| 47 }, fields: { | 47 }, fields: { |
| 48 'some-field': 'value', | 48 'some-field': 'value', |
| 49 'other-field': 'other value' | 49 'other-field': 'other value' |
| 50 }).transform((response) { | 50 }).then(expectAsync1((response) { |
| 51 expect(response.statusCode, equals(200)); | 51 expect(response.statusCode, equals(200)); |
| 52 expect(response.body, parse(equals({ | 52 expect(response.body, parse(equals({ |
| 53 'method': 'POST', | 53 'method': 'POST', |
| 54 'path': '/', | 54 'path': '/', |
| 55 'headers': { | 55 'headers': { |
| 56 'content-type': [ | 56 'content-type': [ |
| 57 'application/x-www-form-urlencoded; charset=UTF-8' | 57 'application/x-www-form-urlencoded; charset=UTF-8' |
| 58 ], | 58 ], |
| 59 'content-length': ['40'], | 59 'content-length': ['40'], |
| 60 'x-random-header': ['Value'], | 60 'x-random-header': ['Value'], |
| 61 'x-other-header': ['Other Value'] | 61 'x-other-header': ['Other Value'] |
| 62 }, | 62 }, |
| 63 'body': 'some-field=value&other-field=other+value' | 63 'body': 'some-field=value&other-field=other+value' |
| 64 }))); | 64 }))); |
| 65 }), completes); | 65 })), completes); |
| 66 }); | 66 }); |
| 67 | 67 |
| 68 test('post without fields', () { | 68 test('post without fields', () { |
| 69 expect(http.post(serverUrl, headers: { | 69 expect(http.post(serverUrl, headers: { |
| 70 'X-Random-Header': 'Value', | 70 'X-Random-Header': 'Value', |
| 71 'X-Other-Header': 'Other Value', | 71 'X-Other-Header': 'Other Value', |
| 72 'Content-Type': 'text/plain' | 72 'Content-Type': 'text/plain' |
| 73 }).transform((response) { | 73 }).then(expectAsync1((response) { |
| 74 expect(response.statusCode, equals(200)); | 74 expect(response.statusCode, equals(200)); |
| 75 expect(response.body, parse(equals({ | 75 expect(response.body, parse(equals({ |
| 76 'method': 'POST', | 76 'method': 'POST', |
| 77 'path': '/', | 77 'path': '/', |
| 78 'headers': { | 78 'headers': { |
| 79 'content-length': ['0'], | 79 'content-length': ['0'], |
| 80 'content-type': ['text/plain'], | 80 'content-type': ['text/plain'], |
| 81 'x-random-header': ['Value'], | 81 'x-random-header': ['Value'], |
| 82 'x-other-header': ['Other Value'] | 82 'x-other-header': ['Other Value'] |
| 83 } | 83 } |
| 84 }))); | 84 }))); |
| 85 }), completes); | 85 })), completes); |
| 86 }); | 86 }); |
| 87 | 87 |
| 88 test('put', () { | 88 test('put', () { |
| 89 expect(http.put(serverUrl, headers: { | 89 expect(http.put(serverUrl, headers: { |
| 90 'X-Random-Header': 'Value', | 90 'X-Random-Header': 'Value', |
| 91 'X-Other-Header': 'Other Value' | 91 'X-Other-Header': 'Other Value' |
| 92 }, fields: { | 92 }, fields: { |
| 93 'some-field': 'value', | 93 'some-field': 'value', |
| 94 'other-field': 'other value' | 94 'other-field': 'other value' |
| 95 }).transform((response) { | 95 }).then(expectAsync1((response) { |
| 96 expect(response.statusCode, equals(200)); | 96 expect(response.statusCode, equals(200)); |
| 97 expect(response.body, parse(equals({ | 97 expect(response.body, parse(equals({ |
| 98 'method': 'PUT', | 98 'method': 'PUT', |
| 99 'path': '/', | 99 'path': '/', |
| 100 'headers': { | 100 'headers': { |
| 101 'content-type': [ | 101 'content-type': [ |
| 102 'application/x-www-form-urlencoded; charset=UTF-8' | 102 'application/x-www-form-urlencoded; charset=UTF-8' |
| 103 ], | 103 ], |
| 104 'content-length': ['40'], | 104 'content-length': ['40'], |
| 105 'x-random-header': ['Value'], | 105 'x-random-header': ['Value'], |
| 106 'x-other-header': ['Other Value'] | 106 'x-other-header': ['Other Value'] |
| 107 }, | 107 }, |
| 108 'body': 'some-field=value&other-field=other+value' | 108 'body': 'some-field=value&other-field=other+value' |
| 109 }))); | 109 }))); |
| 110 }), completes); | 110 })), completes); |
| 111 }); | 111 }); |
| 112 | 112 |
| 113 test('put without fields', () { | 113 test('put without fields', () { |
| 114 expect(http.put(serverUrl, headers: { | 114 expect(http.put(serverUrl, headers: { |
| 115 'X-Random-Header': 'Value', | 115 'X-Random-Header': 'Value', |
| 116 'X-Other-Header': 'Other Value', | 116 'X-Other-Header': 'Other Value', |
| 117 'Content-Type': 'text/plain' | 117 'Content-Type': 'text/plain' |
| 118 }).transform((response) { | 118 }).then(expectAsync1((response) { |
| 119 expect(response.statusCode, equals(200)); | 119 expect(response.statusCode, equals(200)); |
| 120 expect(response.body, parse(equals({ | 120 expect(response.body, parse(equals({ |
| 121 'method': 'PUT', | 121 'method': 'PUT', |
| 122 'path': '/', | 122 'path': '/', |
| 123 'headers': { | 123 'headers': { |
| 124 'content-length': ['0'], | 124 'content-length': ['0'], |
| 125 'content-type': ['text/plain'], | 125 'content-type': ['text/plain'], |
| 126 'x-random-header': ['Value'], | 126 'x-random-header': ['Value'], |
| 127 'x-other-header': ['Other Value'] | 127 'x-other-header': ['Other Value'] |
| 128 } | 128 } |
| 129 }))); | 129 }))); |
| 130 }), completes); | 130 })), completes); |
| 131 }); | 131 }); |
| 132 | 132 |
| 133 test('delete', () { | 133 test('delete', () { |
| 134 expect(http.delete(serverUrl, headers: { | 134 expect(http.delete(serverUrl, headers: { |
| 135 'X-Random-Header': 'Value', | 135 'X-Random-Header': 'Value', |
| 136 'X-Other-Header': 'Other Value' | 136 'X-Other-Header': 'Other Value' |
| 137 }).transform((response) { | 137 }).then(expectAsync1((response) { |
| 138 expect(response.statusCode, equals(200)); | 138 expect(response.statusCode, equals(200)); |
| 139 expect(response.body, parse(equals({ | 139 expect(response.body, parse(equals({ |
| 140 'method': 'DELETE', | 140 'method': 'DELETE', |
| 141 'path': '/', | 141 'path': '/', |
| 142 'headers': { | 142 'headers': { |
| 143 'content-length': ['0'], | 143 'content-length': ['0'], |
| 144 'x-random-header': ['Value'], | 144 'x-random-header': ['Value'], |
| 145 'x-other-header': ['Other Value'] | 145 'x-other-header': ['Other Value'] |
| 146 } | 146 } |
| 147 }))); | 147 }))); |
| 148 }), completes); | 148 })), completes); |
| 149 }); | 149 }); |
| 150 | 150 |
| 151 test('read', () { | 151 test('read', () { |
| 152 expect(http.read(serverUrl, headers: { | 152 expect(http.read(serverUrl, headers: { |
| 153 'X-Random-Header': 'Value', | 153 'X-Random-Header': 'Value', |
| 154 'X-Other-Header': 'Other Value' | 154 'X-Other-Header': 'Other Value' |
| 155 }), completion(parse(equals({ | 155 }).then(expectAsync1((val) => val)), completion(parse(equals({ |
| 156 'method': 'GET', | 156 'method': 'GET', |
| 157 'path': '/', | 157 'path': '/', |
| 158 'headers': { | 158 'headers': { |
| 159 'content-length': ['0'], | 159 'content-length': ['0'], |
| 160 'x-random-header': ['Value'], | 160 'x-random-header': ['Value'], |
| 161 'x-other-header': ['Other Value'] | 161 'x-other-header': ['Other Value'] |
| 162 }, | 162 }, |
| 163 })))); | 163 })))); |
| 164 }); | 164 }); |
| 165 | 165 |
| 166 test('read throws an error for a 4** status code', () { | 166 test('read throws an error for a 4** status code', () { |
| 167 expect(http.read(serverUrl.resolve('/error')), throwsHttpException); | 167 expect(http.read(serverUrl.resolve('/error')).then((expectAsync1(x) => x))
, |
| 168 throwsHttpException); |
| 168 }); | 169 }); |
| 169 | 170 |
| 170 test('readBytes', () { | 171 test('readBytes', () { |
| 171 var future = http.readBytes(serverUrl, headers: { | 172 var future = http.readBytes(serverUrl, headers: { |
| 172 'X-Random-Header': 'Value', | 173 'X-Random-Header': 'Value', |
| 173 'X-Other-Header': 'Other Value' | 174 'X-Other-Header': 'Other Value' |
| 174 }).transform((bytes) => new String.fromCharCodes(bytes)); | 175 }).then(expectAsync1((bytes) => new String.fromCharCodes(bytes))); |
| 175 | 176 |
| 176 expect(future, completion(parse(equals({ | 177 expect(future, completion(parse(equals({ |
| 177 'method': 'GET', | 178 'method': 'GET', |
| 178 'path': '/', | 179 'path': '/', |
| 179 'headers': { | 180 'headers': { |
| 180 'content-length': ['0'], | 181 'content-length': ['0'], |
| 181 'x-random-header': ['Value'], | 182 'x-random-header': ['Value'], |
| 182 'x-other-header': ['Other Value'] | 183 'x-other-header': ['Other Value'] |
| 183 }, | 184 }, |
| 184 })))); | 185 })))); |
| 185 }); | 186 }); |
| 186 | 187 |
| 187 test('readBytes throws an error for a 4** status code', () { | 188 test('readBytes throws an error for a 4** status code', () { |
| 188 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException); | 189 expect(http.readBytes(serverUrl.resolve('/error')).then((expectAsync1(x) =
> x)), |
| 190 throwsHttpException); |
| 189 }); | 191 }); |
| 190 }); | 192 }); |
| 191 } | 193 } |
| OLD | NEW |