forked from felangel/mason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.dart
200 lines (169 loc) · 6.44 KB
/
response_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import 'dart:convert';
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
import 'package:dart_frog/src/body_parsers/form_data.dart';
import 'package:test/test.dart';
void main() {
group('Response', () {
group('statusCode', () {
test('defaults to 200.', () {
final response = Response();
expect(response.statusCode, equals(HttpStatus.ok));
});
test('can be overridden.', () {
const expected = HttpStatus.notFound;
final response = Response(statusCode: expected);
expect(response.statusCode, equals(expected));
});
});
test('has correct body (string)', () {
const body = 'test-body';
final response = Response(body: body);
expect(response.body(), completion(equals(body)));
});
test('has correct body (empty)', () {
final response = Response();
expect(response.body(), completion(isEmpty));
});
test('throws exception when unable to read body', () async {
final request = Response.bytes(body: base64Decode('1234'));
expect(request.body, throwsException);
});
test('throws exception when unable to read body multiple times.', () async {
final request = Response.bytes(body: base64Decode('1234'));
expect(request.body, throwsException);
expect(request.body, throwsException);
});
test('has correct headers', () {
const headers = <String, String>{'foo': 'bar'};
final response = Response(headers: headers);
expect(response.headers['foo'], equals('bar'));
});
test('has correct headers when a key has multiple values', () {
const headers = <String, Object>{
'foo': ['bar', 'baz'],
};
final response = Response(headers: headers);
expect(response.headers['foo'], equals('bar,baz'));
});
test('body can be read multiple times (sync)', () {
final body = json.encode({'test': 'body'});
final response = Response(body: body);
expect(response.body(), completion(equals(body)));
expect(response.body(), completion(equals(body)));
expect(response.json(), completion(equals(json.decode(body))));
expect(response.json(), completion(equals(json.decode(body))));
});
test('body can be read multiple times (async)', () async {
final body = json.encode({'test': 'body'});
final response = Response(body: body);
await expectLater(response.body(), completion(equals(body)));
await expectLater(response.body(), completion(equals(body)));
await expectLater(response.json(), completion(equals(json.decode(body))));
await expectLater(response.json(), completion(equals(json.decode(body))));
});
group('copyWith', () {
test('returns a copy with overridden properties', () {
const headers = <String, String>{'foo': 'bar'};
const body = 'test-body';
final original = Response();
final copy = original.copyWith(
headers: headers,
body: body,
);
expect(copy.headers['foo'], equals(headers['foo']));
expect(copy.body(), completion(equals(body)));
});
});
group('bytes', () {
test('has correct body', () {
final bytes = utf8.encode('hello');
final response = Response.bytes(body: bytes);
expect(response.bytes(), emits(equals(bytes)));
});
});
group('stream', () {
test('has correct body', () {
final bytes = utf8.encode('hello');
final stream = Stream.value(bytes);
final response = Response.stream(body: stream);
expect(response.bytes(), emits(equals(bytes)));
});
});
group('formData', () {
final contentTypeFormUrlEncoded = {
HttpHeaders.contentTypeHeader: formUrlEncodedContentType.mimeType
};
test('throws StateError on invalid content-type', () async {
final response = Response();
expect(response.formData(), throwsStateError);
});
test('has correct data (no body)', () async {
final response = Response(headers: contentTypeFormUrlEncoded);
expect(response.formData(), completion(equals({})));
});
test('has correct data (empty body)', () async {
final response = Response(headers: contentTypeFormUrlEncoded, body: '');
expect(response.formData(), completion(equals({})));
});
test('has correct data (single key/value pair)', () async {
final response = Response(
headers: contentTypeFormUrlEncoded,
body: 'foo=bar',
);
expect(response.formData(), completion(equals({'foo': 'bar'})));
});
test('has correct data (multiple key/value pairs)', () async {
final response = Response(
headers: contentTypeFormUrlEncoded,
body: 'foo=bar&bar=baz',
);
expect(
response.formData(),
completion(equals({'foo': 'bar', 'bar': 'baz'})),
);
});
});
group('json', () {
test('has correct body (map)', () {
final body = <String, dynamic>{'foo': 'bar'};
final response = Response.json(body: body);
expect(response.json(), completion(equals(body)));
});
test('has correct body (list)', () {
final body = <String>['foo', 'bar'];
final response = Response.json(body: body);
expect(response.json(), completion(equals(body)));
});
test('has correct body (string)', () {
const body = 'foo';
final response = Response.json(body: body);
expect(response.json(), completion(equals(body)));
});
test('has correct body (number)', () {
const body = 42.0;
final response = Response.json(body: body);
expect(response.json(), completion(equals(body)));
});
test('has correct body (bool)', () {
const body = false;
final response = Response.json(body: body);
expect(response.json(), completion(equals(body)));
});
test('has correct body (empty)', () {
final response = Response.json();
expect(response.json(), completion(isEmpty));
});
test('has correct content-type when overriden in headers', () {
final headers = <String, String>{
HttpHeaders.contentTypeHeader: ContentType.html.value,
};
final response = Response.json(headers: headers);
expect(
response.headers[HttpHeaders.contentTypeHeader],
equals(ContentType.html.value),
);
});
});
});
}