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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use crate::Error;
use super::scenario::Scenario;
#[test]
fn write_response_body() {
// Create a scenario with a GET request and a response with content-length
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.status(200)
.header("content-length", "13")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Create a buffer to write the response body to
let mut output = [0u8; 1024];
// Write some response body data
let body = b"Hello, world!";
let (input_used, output_used) = scenario.write(body, &mut output).unwrap();
// Verify that all input was consumed
assert_eq!(input_used, body.len());
// Verify that the output contains the body
assert_eq!(&output[..output_used], body);
}
#[test]
fn write_chunked_response_body() {
// Create a scenario with a GET request and a chunked response
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.status(200)
.header("transfer-encoding", "chunked")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Create a buffer to write the response body to
let mut output = [0u8; 1024];
// Write some response body data
let body = b"Hello, world!";
let (input_used, output_used) = scenario.write(body, &mut output).unwrap();
// Verify that all input was consumed
assert_eq!(input_used, body.len());
// Verify that the output contains the chunked body
let output_str = std::str::from_utf8(&output[..output_used]).unwrap();
assert!(output_str.starts_with("d\r\n")); // 13 bytes in hex
assert!(output_str.contains("Hello, world!"));
assert!(output_str.ends_with("\r\n"));
// Verify that the response is chunked
assert!(scenario.is_chunked());
// Verify that the response is not finished yet
assert!(!scenario.is_finished());
// Write an empty chunk to finish the response
let (input_used, output_used) = scenario.write(&[], &mut output).unwrap();
assert_eq!(input_used, 0);
assert!(output_used > 0);
// Verify that the response is now finished
assert!(scenario.is_finished());
}
#[test]
fn calculate_max_input() {
// Create a scenario with a GET request and a content-length response
let scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.status(200)
.header("content-length", "100")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// For non-chunked responses, max input equals output length
assert_eq!(scenario.calculate_max_input(100), 100);
// Create a scenario with a GET request and a chunked response
let scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.status(200)
.header("transfer-encoding", "chunked")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// For chunked responses, max input is less than output length
assert!(scenario.calculate_max_input(100) < 100);
}
#[test]
fn proceed_to_cleanup() {
// Create a scenario with a GET request and a response with content-length
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("content-length", "13")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Create a buffer to write the response body to
let mut output = [0u8; 1024];
// Write the response body
let body = b"Hello, world!";
let (input_used, _) = scenario.write(body, &mut output).unwrap();
assert_eq!(input_used, body.len());
// Verify that the response is now finished
assert!(scenario.is_finished());
// Proceed to Cleanup
let _cleanup = scenario.proceed();
// The type system ensures that _cleanup is now Reply<Cleanup>
}
#[test]
#[should_panic]
fn proceed_before_finished_panics() {
// Create a scenario with a GET request and a response with content-length
let scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("content-length", "100")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Verify that the response is not finished
assert!(!scenario.is_finished());
// Proceed to Cleanup (should panic)
let _ = scenario.proceed();
}
#[test]
fn error_writing_after_finished() {
// Create a scenario with a GET request and a response with content-length
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("content-length", "13")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Create a buffer to write the response body to
let mut output = [0u8; 1024];
// Write the response body
let body = b"Hello, world!";
let (input_used, _) = scenario.write(body, &mut output).unwrap();
assert_eq!(input_used, body.len());
// Verify that the response is now finished
assert!(scenario.is_finished());
// Try to write more data (should fail)
let result = scenario.write(b"More data", &mut output);
assert!(result.is_err());
match result.unwrap_err() {
Error::BodyContentAfterFinish => {}
_ => panic!("Expected BodyContentAfterFinish error"),
}
}
#[test]
fn error_writing_more_than_content_length() {
// Create a scenario with a GET request and a response with content-length
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("content-length", "10")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Create a buffer to write the response body to
let mut output = [0u8; 1024];
// Try to write more data than the content-length (should fail)
let result = scenario.write(b"Hello, world!", &mut output);
assert!(result.is_err());
match result.unwrap_err() {
Error::BodyLargerThanContentLength => {}
_ => panic!("Expected BodyLargerThanContentLength error"),
}
}
#[test]
fn consume_direct_write() {
// Create a scenario with a GET request and a response with content-length
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("content-length", "13")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Consume direct write
scenario.consume_direct_write(13).unwrap();
// Verify that the response is now finished
assert!(scenario.is_finished());
}
#[test]
fn error_consuming_more_than_content_length() {
// Create a scenario with a GET request and a response with content-length
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("content-length", "10")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Try to consume more than the content-length (should fail)
let result = scenario.consume_direct_write(11);
assert!(result.is_err());
match result.unwrap_err() {
Error::BodyLargerThanContentLength => {}
_ => panic!("Expected BodyLargerThanContentLength error"),
}
}
#[test]
fn error_consuming_direct_write_with_chunked() {
// Create a scenario with a GET request and a chunked response
let mut scenario = Scenario::builder()
.get("/path")
.response(
http::Response::builder()
.header("transfer-encoding", "chunked")
.body(())
.unwrap(),
)
.build()
.to_send_body();
// Try to consume direct write with chunked encoding (should fail)
let result = scenario.consume_direct_write(10);
assert!(result.is_err());
match result.unwrap_err() {
Error::BodyIsChunked => {}
_ => panic!("Expected BodyIsChunked error"),
}
}