|
| 1 | +// Copyright (c) 2017, 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 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'package:async/async.dart'; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +import 'package:http/http.dart' as http; |
| 11 | + |
| 12 | +import 'utils.dart'; |
| 13 | + |
| 14 | +void main() { |
| 15 | + group('#contentLength', () { |
| 16 | + test('is computed from bodyBytes', () { |
| 17 | + var request = new http.Request('POST', dummyUrl, body: [1, 2, 3, 4, 5]); |
| 18 | + expect(request.contentLength, equals(5)); |
| 19 | + request = new http.Request('POST', dummyUrl, |
| 20 | + body: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 21 | + expect(request.contentLength, equals(10)); |
| 22 | + }); |
| 23 | + |
| 24 | + test('is computed from body', () { |
| 25 | + var request = new http.Request('POST', dummyUrl, body: 'hello'); |
| 26 | + expect(request.contentLength, equals(5)); |
| 27 | + request = new http.Request('POST', dummyUrl, body: 'hello, world'); |
| 28 | + expect(request.contentLength, equals(12)); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + group('#encoding', () { |
| 33 | + test('defaults to utf-8', () { |
| 34 | + var request = new http.Request('POST', dummyUrl); |
| 35 | + expect(request.encoding.name, equals(UTF8.name)); |
| 36 | + }); |
| 37 | + |
| 38 | + test('can be set', () { |
| 39 | + var request = new http.Request('POST', dummyUrl, encoding: LATIN1); |
| 40 | + expect(request.encoding.name, equals(LATIN1.name)); |
| 41 | + }); |
| 42 | + |
| 43 | + test('is based on the content-type charset if it exists', () { |
| 44 | + var request = new http.Request('POST', dummyUrl, |
| 45 | + headers: {'Content-Type': 'text/plain; charset=iso-8859-1'}); |
| 46 | + expect(request.encoding.name, equals(LATIN1.name)); |
| 47 | + }); |
| 48 | + |
| 49 | + test('throws an error if the content-type charset is unknown', () { |
| 50 | + var request = new http.Request('POST', dummyUrl, |
| 51 | + headers: {'Content-Type': 'text/plain; charset=not-a-real-charset'}); |
| 52 | + expect(() => request.encoding, throwsFormatException); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + group('#bodyBytes', () { |
| 57 | + test('defaults to empty', () { |
| 58 | + var request = new http.Request('POST', dummyUrl); |
| 59 | + expect(collectBytes(request.read()), completion(isEmpty)); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + group('#body', () { |
| 64 | + test('defaults to empty', () { |
| 65 | + var request = new http.Request('POST', dummyUrl); |
| 66 | + expect(request.readAsString(), completion(isEmpty)); |
| 67 | + }); |
| 68 | + |
| 69 | + test('is encoded according to the given encoding', () { |
| 70 | + var request = |
| 71 | + new http.Request('POST', dummyUrl, encoding: LATIN1, body: "föøbãr"); |
| 72 | + expect(collectBytes(request.read()), |
| 73 | + completion(equals([102, 246, 248, 98, 227, 114]))); |
| 74 | + }); |
| 75 | + |
| 76 | + test('is decoded according to the given encoding', () { |
| 77 | + var request = new http.Request('POST', dummyUrl, |
| 78 | + encoding: LATIN1, body: [102, 246, 248, 98, 227, 114]); |
| 79 | + expect(request.readAsString(), completion(equals("föøbãr"))); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + group('#bodyFields', () { |
| 84 | + test('is encoded according to the given encoding', () { |
| 85 | + var request = new http.Request('POST', dummyUrl, |
| 86 | + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, |
| 87 | + encoding: LATIN1, |
| 88 | + body: {"föø": "bãr"}); |
| 89 | + expect(request.readAsString(), completion(equals('f%F6%F8=b%E3r'))); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + group('content-type header', () { |
| 94 | + test('defaults to empty', () { |
| 95 | + var request = new http.Request('POST', dummyUrl); |
| 96 | + expect(request.headers['Content-Type'], isNull); |
| 97 | + }); |
| 98 | + |
| 99 | + test('defaults to empty if only encoding is set', () { |
| 100 | + var request = new http.Request('POST', dummyUrl, encoding: LATIN1); |
| 101 | + expect(request.headers['Content-Type'], isNull); |
| 102 | + }); |
| 103 | + |
| 104 | + test('name is case insensitive', () { |
| 105 | + var request = new http.Request('POST', dummyUrl, |
| 106 | + headers: {'CoNtEnT-tYpE': 'application/json'}); |
| 107 | + expect(request.headers, containsPair('content-type', 'application/json')); |
| 108 | + }); |
| 109 | + |
| 110 | + test( |
| 111 | + 'is set to application/x-www-form-urlencoded with charset utf-8 if ' |
| 112 | + 'bodyFields is set', () { |
| 113 | + var request = |
| 114 | + new http.Request('POST', dummyUrl, body: {'hello': 'world'}); |
| 115 | + expect(request.headers['Content-Type'], |
| 116 | + equals('application/x-www-form-urlencoded; charset=utf-8')); |
| 117 | + }); |
| 118 | + |
| 119 | + test( |
| 120 | + 'is set to application/x-www-form-urlencoded with the given charset ' |
| 121 | + 'if bodyFields and encoding are set', () { |
| 122 | + var request = new http.Request('POST', dummyUrl, |
| 123 | + encoding: LATIN1, body: {'hello': 'world'}); |
| 124 | + expect(request.headers['Content-Type'], |
| 125 | + equals('application/x-www-form-urlencoded; charset=iso-8859-1')); |
| 126 | + }); |
| 127 | + |
| 128 | + test( |
| 129 | + 'is set to text/plain and the given encoding if body and encoding are ' |
| 130 | + 'both set', () { |
| 131 | + var request = new http.Request('POST', dummyUrl, |
| 132 | + encoding: LATIN1, body: 'hello, world'); |
| 133 | + expect(request.headers['Content-Type'], |
| 134 | + equals('text/plain; charset=iso-8859-1')); |
| 135 | + }); |
| 136 | + |
| 137 | + test('is modified to include utf-8 if body is set', () { |
| 138 | + var request = new http.Request('POST', dummyUrl, |
| 139 | + headers: {'Content-Type': 'application/json'}, |
| 140 | + body: '{"hello": "world"}'); |
| 141 | + expect(request.headers['Content-Type'], |
| 142 | + equals('application/json; charset=utf-8')); |
| 143 | + }); |
| 144 | + |
| 145 | + test('is modified to include the given encoding if encoding is set', () { |
| 146 | + var request = new http.Request('POST', dummyUrl, |
| 147 | + headers: {'Content-Type': 'application/json'}, encoding: LATIN1); |
| 148 | + expect(request.headers['Content-Type'], |
| 149 | + equals('application/json; charset=iso-8859-1')); |
| 150 | + }); |
| 151 | + |
| 152 | + test('has its charset overridden by an explicit encoding', () { |
| 153 | + var request = new http.Request('POST', dummyUrl, |
| 154 | + headers: {'Content-Type': 'application/json; charset=utf-8'}, |
| 155 | + encoding: LATIN1); |
| 156 | + expect(request.headers['Content-Type'], |
| 157 | + equals('application/json; charset=iso-8859-1')); |
| 158 | + }); |
| 159 | + |
| 160 | + test("doen't have its charset overridden by setting bodyFields", () { |
| 161 | + var request = new http.Request('POST', dummyUrl, headers: { |
| 162 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=iso-8859-1' |
| 163 | + }, body: { |
| 164 | + 'hello': 'world' |
| 165 | + }); |
| 166 | + expect(request.headers['Content-Type'], |
| 167 | + equals('application/x-www-form-urlencoded; charset=iso-8859-1')); |
| 168 | + }); |
| 169 | + |
| 170 | + test("doen't have its charset overridden by setting body", () { |
| 171 | + var request = new http.Request('POST', dummyUrl, |
| 172 | + headers: {'Content-Type': 'application/json; charset=iso-8859-1'}, |
| 173 | + body: '{"hello": "world"}'); |
| 174 | + expect(request.headers['Content-Type'], |
| 175 | + equals('application/json; charset=iso-8859-1')); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + group('change', () { |
| 180 | + test('with no arguments returns instance with equal values', () { |
| 181 | + var request = new http.Request('GET', dummyUrl, |
| 182 | + headers: {'header1': 'header value 1'}, |
| 183 | + body: 'hello, world', |
| 184 | + context: {'context1': 'context value 1'}); |
| 185 | + |
| 186 | + var copy = request.change(); |
| 187 | + |
| 188 | + expect(copy.method, request.method); |
| 189 | + expect(copy.headers, same(request.headers)); |
| 190 | + expect(copy.url, request.url); |
| 191 | + expect(copy.context, same(request.context)); |
| 192 | + expect(copy.readAsString(), completion('hello, world')); |
| 193 | + }); |
| 194 | + |
| 195 | + test("allows the original request to be read", () { |
| 196 | + var request = new http.Request('GET', dummyUrl); |
| 197 | + var changed = request.change(); |
| 198 | + |
| 199 | + expect(request.read().toList(), completion(isEmpty)); |
| 200 | + expect(changed.read, throwsStateError); |
| 201 | + }); |
| 202 | + |
| 203 | + test("allows the changed request to be read", () { |
| 204 | + var request = new http.Request('GET', dummyUrl); |
| 205 | + var changed = request.change(); |
| 206 | + |
| 207 | + expect(changed.read().toList(), completion(isEmpty)); |
| 208 | + expect(request.read, throwsStateError); |
| 209 | + }); |
| 210 | + |
| 211 | + test("allows another changed request to be read", () { |
| 212 | + var request = new http.Request('GET', dummyUrl); |
| 213 | + var changed1 = request.change(); |
| 214 | + var changed2 = request.change(); |
| 215 | + |
| 216 | + expect(changed2.read().toList(), completion(isEmpty)); |
| 217 | + expect(changed1.read, throwsStateError); |
| 218 | + expect(request.read, throwsStateError); |
| 219 | + }); |
| 220 | + }); |
| 221 | +} |
0 commit comments