@@ -24,14 +24,23 @@ void testResponseHeaders(Client client) async {
24
24
});
25
25
26
26
test ('single header' , () async {
27
- httpServerChannel.sink.add ({ 'foo' : ' bar' } );
27
+ httpServerChannel.sink.add ('foo: bar\r\n ' );
28
28
29
29
final response = await client.get (Uri .http (host, '' ));
30
30
expect (response.headers['foo' ], 'bar' );
31
31
});
32
32
33
- test ('UPPERCASE header' , () async {
34
- httpServerChannel.sink.add ({'foo' : 'BAR' });
33
+ test ('UPPERCASE header value' , () async {
34
+ httpServerChannel.sink.add ('foo: BAR\r\n ' );
35
+
36
+ final response = await client.get (Uri .http (host, '' ));
37
+ // RFC 2616 14.44 states that header field names are case-insensitive.
38
+ // http.Client canonicalizes field names into lower case.
39
+ expect (response.headers['foo' ], 'BAR' );
40
+ });
41
+
42
+ test ('space surrounding header value' , () async {
43
+ httpServerChannel.sink.add ('foo: \t BAR \t \r\n ' );
35
44
36
45
final response = await client.get (Uri .http (host, '' ));
37
46
// RFC 2616 14.44 states that header field names are case-insensitive.
@@ -41,7 +50,7 @@ void testResponseHeaders(Client client) async {
41
50
42
51
test ('multiple headers' , () async {
43
52
httpServerChannel.sink
44
- .add ({ 'field1' : ' value1' , 'field2' : ' value2' , 'field3' : ' value3' } );
53
+ .add ('field1: value1\r\n ' 'field2: value2\r\n ' 'field3: value3\r\n ' );
45
54
46
55
final response = await client.get (Uri .http (host, '' ));
47
56
expect (response.headers['field1' ], 'value1' );
@@ -50,10 +59,76 @@ void testResponseHeaders(Client client) async {
50
59
});
51
60
52
61
test ('multiple values per header' , () async {
53
- httpServerChannel.sink.add ({'list' : 'apple, orange, banana' });
62
+ // RFC-2616 4.2 says:
63
+ // "The field value MAY be preceded by any amount of LWS, though a single
64
+ // SP is preferred." and
65
+ // "The field-content does not include any leading or trailing LWS ..."
66
+ httpServerChannel.sink.add ('list: apple, orange, banana\r\n ' );
54
67
55
68
final response = await client.get (Uri .http (host, '' ));
56
- expect (response.headers['list' ], 'apple, orange, banana' );
69
+ expect (response.headers['list' ],
70
+ matches (r'apple[ \t]*,[ \t]*orange[ \t]*,[ \t]*banana' ));
71
+ });
72
+
73
+ test ('multiple values per header surrounded with spaces' , () async {
74
+ httpServerChannel.sink
75
+ .add ('list: \t apple \t , \t orange \t , \t banana\t \t \r\n ' );
76
+
77
+ final response = await client.get (Uri .http (host, '' ));
78
+ expect (response.headers['list' ],
79
+ matches (r'apple[ \t]*,[ \t]*orange[ \t]*,[ \t]*banana' ));
80
+ });
81
+
82
+ test ('multiple headers with the same name' , () async {
83
+ httpServerChannel.sink.add ('list: apple\r\n '
84
+ 'list: orange\r\n '
85
+ 'list: banana\r\n ' );
86
+
87
+ final response = await client.get (Uri .http (host, '' ));
88
+ expect (response.headers['list' ],
89
+ matches (r'apple[ \t]*,[ \t]*orange[ \t]*,[ \t]*banana' ));
90
+ });
91
+
92
+ test ('multiple headers with the same name but different cases' , () async {
93
+ httpServerChannel.sink.add ('list: apple\r\n '
94
+ 'LIST: orange\r\n '
95
+ 'List: banana\r\n ' );
96
+
97
+ final response = await client.get (Uri .http (host, '' ));
98
+ expect (response.headers['list' ],
99
+ matches (r'apple[ \t]*,[ \t]*orange[ \t]*,[ \t]*banana' ));
100
+ });
101
+
102
+ group ('content length' , () {
103
+ test ('surrounded in spaces' , () async {
104
+ // RFC-2616 4.2 says:
105
+ // "The field value MAY be preceded by any amount of LWS, though a
106
+ // single SP is preferred." and
107
+ // "The field-content does not include any leading or trailing LWS ..."
108
+ httpServerChannel.sink.add ('content-length: \t 0 \t \r\n ' );
109
+ final response = await client.get (Uri .http (host, '' ));
110
+ expect (response.contentLength, 0 );
111
+ },
112
+ skip: 'Enable after https://github.com/dart-lang/sdk/issues/51532 '
113
+ 'is fixed' );
114
+
115
+ test ('non-integer' , () async {
116
+ httpServerChannel.sink.add ('content-length: cat\r\n ' );
117
+ await expectLater (
118
+ client.get (Uri .http (host, '' )), throwsA (isA <ClientException >()));
119
+ });
120
+
121
+ test ('negative' , () async {
122
+ httpServerChannel.sink.add ('content-length: -5\r\n ' );
123
+ await expectLater (
124
+ client.get (Uri .http (host, '' )), throwsA (isA <ClientException >()));
125
+ });
126
+
127
+ test ('bigger than actual body' , () async {
128
+ httpServerChannel.sink.add ('content-length: 100\r\n ' );
129
+ await expectLater (
130
+ client.get (Uri .http (host, '' )), throwsA (isA <ClientException >()));
131
+ });
57
132
});
58
133
});
59
134
}
0 commit comments