Skip to content

Commit 3cbf6ea

Browse files
committed
use vecs of lines for indentation
1 parent d836b68 commit 3cbf6ea

File tree

4 files changed

+192
-176
lines changed

4 files changed

+192
-176
lines changed

tests/client_decode.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ mod client_decode {
55
use http_types::Result;
66
use pretty_assertions::assert_eq;
77

8-
async fn decode_str(s: &'static str) -> http_types::Result<http_types::Response> {
9-
client::decode(Cursor::new(s.replace("\n", "\r\n"))).await
8+
async fn decode_lines(s: Vec<&str>) -> http_types::Result<http_types::Response> {
9+
client::decode(Cursor::new(dbg!(s.join("\r\n")))).await
1010
}
1111

1212
#[async_std::test]
1313
async fn response_no_date() -> Result<()> {
14-
let res = decode_str(
15-
r#"HTTP/1.1 200 OK
16-
transfer-encoding: chunked
17-
content-type: text/plain
18-
19-
"#,
20-
)
14+
let res = decode_lines(vec![
15+
"HTTP/1.1 200 OK",
16+
"transfer-encoding: chunked",
17+
"content-type: text/plain",
18+
"",
19+
"",
20+
])
2121
.await?;
2222

2323
assert_eq!(res.header(&headers::DATE).is_some(), true);
@@ -26,15 +26,15 @@ content-type: text/plain
2626

2727
#[async_std::test]
2828
async fn multiple_header_values_for_same_header_name() -> Result<()> {
29-
let res = decode_str(
30-
r#"HTTP/1.1 200 OK
31-
host: example.com
32-
content-length: 0
33-
set-cookie: sessionId=e8bb43229de9
34-
set-cookie: qwerty=219ffwef9w0f
35-
36-
"#,
37-
)
29+
let res = decode_lines(vec![
30+
"HTTP/1.1 200 OK",
31+
"host: example.com",
32+
"content-length: 0",
33+
"set-cookie: sessionId=e8bb43229de9",
34+
"set-cookie: qwerty=219ffwef9w0f",
35+
"",
36+
"",
37+
])
3838
.await?;
3939
assert_eq!(res.header(&headers::SET_COOKIE).unwrap().iter().count(), 2);
4040

@@ -43,16 +43,15 @@ set-cookie: qwerty=219ffwef9w0f
4343

4444
#[async_std::test]
4545
async fn response_newlines() -> Result<()> {
46-
let res = decode_str(
47-
r#"HTTP/1.1 200 OK
48-
content-length: 78
49-
date: {DATE}
50-
content-type: text/plain; charset=utf-8
51-
52-
http specifies headers are separated with \r\n but many servers don't do that
53-
54-
"#,
55-
)
46+
let res = decode_lines(vec![
47+
"HTTP/1.1 200 OK",
48+
"content-length: 78",
49+
"date: {DATE}",
50+
"content-type: text/plain; charset=utf-8",
51+
"",
52+
"http specifies headers are separated with \r\n but many servers don't do that",
53+
"",
54+
])
5655
.await?;
5756

5857
assert_eq!(

tests/client_encode.rs

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ mod client_encode {
3030
assert_encoded(
3131
100,
3232
req,
33-
r#"POST / HTTP/1.1
34-
host: localhost:8080
35-
content-length: 5
36-
content-type: text/plain;charset=utf-8
37-
38-
hello"#,
33+
vec![
34+
"POST / HTTP/1.1",
35+
"host: localhost:8080",
36+
"content-length: 5",
37+
"content-type: text/plain;charset=utf-8",
38+
"",
39+
"hello",
40+
],
3941
)
4042
.await;
4143
Ok(())
@@ -49,12 +51,14 @@ hello"#,
4951
assert_encoded(
5052
100,
5153
req,
52-
r#"CONNECT example.com:443 HTTP/1.1
53-
host: example.com
54-
proxy-connection: keep-alive
55-
content-length: 0
56-
57-
"#,
54+
vec![
55+
"CONNECT example.com:443 HTTP/1.1",
56+
"host: example.com",
57+
"proxy-connection: keep-alive",
58+
"content-length: 0",
59+
"",
60+
"",
61+
],
5862
)
5963
.await;
6064

@@ -70,21 +74,23 @@ content-length: 0
7074
assert_encoded(
7175
10,
7276
req,
73-
r#"GET /path?query HTTP/1.1
74-
host: example.com
75-
content-length: 0
76-
77-
"#,
77+
vec![
78+
"GET /path?query HTTP/1.1",
79+
"host: example.com",
80+
"content-length: 0",
81+
"",
82+
"",
83+
],
7884
)
7985
.await;
8086

8187
Ok(())
8288
}
8389

84-
async fn assert_encoded(len: usize, req: Request, s: &str) {
90+
async fn assert_encoded(len: usize, req: Request, lines: Vec<&str>) {
8591
assert_eq!(
8692
encode_to_string(req, len).await.unwrap(),
87-
s.replace('\n', "\r\n"),
93+
lines.join("\r\n"),
8894
)
8995
}
9096

@@ -98,20 +104,21 @@ content-length: 0
98104
assert_encoded(
99105
10,
100106
req,
101-
r#"GET /path?query HTTP/1.1
102-
host: example.com
103-
content-type: application/octet-stream
104-
transfer-encoding: chunked
105-
106-
5
107-
hello
108-
5
109-
worl
110-
1
111-
d
112-
0
113-
114-
"#,
107+
vec![
108+
"GET /path?query HTTP/1.1",
109+
"host: example.com",
110+
"content-type: application/octet-stream",
111+
"transfer-encoding: chunked",
112+
"",
113+
"5",
114+
"hello",
115+
"5",
116+
" worl",
117+
"1",
118+
"d",
119+
"0",
120+
"",
121+
],
115122
)
116123
.await;
117124

@@ -121,16 +128,17 @@ d
121128
assert_encoded(
122129
16,
123130
req,
124-
r#"GET /path?query HTTP/1.1
125-
host: example.com
126-
content-type: application/octet-stream
127-
transfer-encoding: chunked
128-
129-
B
130-
hello world
131-
0
132-
133-
"#,
131+
vec![
132+
"GET /path?query HTTP/1.1",
133+
"host: example.com",
134+
"content-type: application/octet-stream",
135+
"transfer-encoding: chunked",
136+
"",
137+
"B",
138+
"hello world",
139+
"0",
140+
"",
141+
],
134142
)
135143
.await;
136144

@@ -145,22 +153,23 @@ hello world
145153
assert_encoded(
146154
32,
147155
req,
148-
r#"GET /path?query HTTP/1.1
149-
host: example.com
150-
content-type: application/octet-stream
151-
transfer-encoding: chunked
152-
153-
1A
154-
this response is more than
155-
1A
156-
32 bytes long in order to
157-
1A
158-
require a second hex digi
159-
1
160-
t
161-
0
162-
163-
"#,
156+
vec![
157+
"GET /path?query HTTP/1.1",
158+
"host: example.com",
159+
"content-type: application/octet-stream",
160+
"transfer-encoding: chunked",
161+
"",
162+
"1A",
163+
"this response is more than",
164+
"1A",
165+
" 32 bytes long in order to",
166+
"1A",
167+
" require a second hex digi",
168+
"1",
169+
"t",
170+
"0",
171+
"",
172+
],
164173
)
165174
.await;
166175

tests/server_decode.rs

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ mod server_decode {
88
use http_types::Url;
99
use pretty_assertions::assert_eq;
1010

11-
async fn decode_str(s: &'static str) -> Result<Option<Request>> {
11+
async fn decode_lines(lines: Vec<&str>) -> Result<Option<Request>> {
12+
let s = lines.join("\r\n");
1213
async_h1::server::decode(Duplex::new(
13-
Arc::new(Mutex::new(Cursor::new(s.replace("\n", "\r\n")))),
14+
Arc::new(Mutex::new(Cursor::new(s))),
1415
Arc::new(Mutex::new(Cursor::new(vec![]))),
1516
))
1617
.await
1718
}
1819

1920
#[async_std::test]
2021
async fn post_with_body() -> Result<()> {
21-
let mut request = decode_str(
22-
r#"POST / HTTP/1.1
23-
host: localhost:8080
24-
content-length: 5
25-
content-type: text/plain;charset=utf-8
26-
another-header: header value
27-
another-header: other header value
28-
29-
hello
30-
31-
"#,
32-
)
22+
let mut request = decode_lines(vec![
23+
"POST / HTTP/1.1",
24+
"host: localhost:8080",
25+
"content-length: 5",
26+
"content-type: text/plain;charset=utf-8",
27+
"another-header: header value",
28+
"another-header: other header value",
29+
"",
30+
"hello",
31+
"",
32+
])
3333
.await?
3434
.unwrap();
3535

@@ -52,22 +52,21 @@ hello
5252

5353
#[async_std::test]
5454
async fn chunked() -> Result<()> {
55-
let mut request = decode_str(
56-
r#"POST / HTTP/1.1
57-
host: localhost:8080
58-
transfer-encoding: chunked
59-
content-type: text/plain;charset=utf-8
60-
61-
1
62-
h
63-
1
64-
e
65-
3
66-
llo
67-
0
68-
69-
"#,
70-
)
55+
let mut request = decode_lines(vec![
56+
"POST / HTTP/1.1",
57+
"host: localhost:8080",
58+
"transfer-encoding: chunked",
59+
"content-type: text/plain;charset=utf-8",
60+
"",
61+
"1",
62+
"h",
63+
"1",
64+
"e",
65+
"3",
66+
"llo",
67+
"0",
68+
"",
69+
])
7170
.await?
7271
.unwrap();
7372

@@ -83,18 +82,17 @@ llo
8382
"#]
8483
#[async_std::test]
8584
async fn invalid_trailer() -> Result<()> {
86-
let mut request = decode_str(
87-
r#"GET / HTTP/1.1
88-
host: domain.com
89-
content-type: application/octet-stream
90-
transfer-encoding: chunked
91-
trailer: x-invalid
92-
93-
0
94-
x-invalid: å
95-
96-
"#,
97-
)
85+
let mut request = decode_lines(vec![
86+
"GET / HTTP/1.1",
87+
"host: domain.com",
88+
"content-type: application/octet-stream",
89+
"transfer-encoding: chunked",
90+
"trailer: x-invalid",
91+
"",
92+
"0",
93+
"x-invalid: å",
94+
"",
95+
])
9896
.await?
9997
.unwrap();
10098

@@ -105,14 +103,14 @@ x-invalid: å
105103

106104
#[async_std::test]
107105
async fn unexpected_eof() -> Result<()> {
108-
let mut request = decode_str(
109-
r#"POST / HTTP/1.1
110-
host: example.com
111-
content-type: text/plain
112-
content-length: 11
113-
114-
not 11"#,
115-
)
106+
let mut request = decode_lines(vec![
107+
"POST / HTTP/1.1",
108+
"host: example.com",
109+
"content-type: text/plain",
110+
"content-length: 11",
111+
"",
112+
"not 11",
113+
])
116114
.await?
117115
.unwrap();
118116

0 commit comments

Comments
 (0)