httpageboy 1.0.19

A lightweight library for handling raw HTTP request/response transmission. Good base for APIs. Supports both synchronous and asynchronous programming models.
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#![cfg(feature = "async_std")]

use httpageboy::test_utils::{run_test, setup_test_server};
use httpageboy::{Request, Response, Rt, Server, StatusCode, handler};
use std::collections::BTreeMap;

const REGULAR_SERVER_URL: &str = "127.0.0.1:58080";
const STRICT_SERVER_URL: &str = "127.0.0.1:58081";

async fn common_server_definition(server_url: &str) -> Server {
  let mut server = match Server::new(server_url, None).await {
    Ok(server) => server,
    Err(_) => Server::new("127.0.0.1:0", None)
      .await
      .expect("failed to bind test server"),
  };
  server.add_route("/", Rt::GET, handler!(demo_handle_home));
  server.add_route("/test", Rt::GET, handler!(demo_handle_get));
  server.add_route("/test", Rt::POST, handler!(demo_handle_post));
  server.add_route("/test/{param1}", Rt::POST, handler!(demo_handle_post));
  server.add_route("/test/{param1}/{param2}", Rt::POST, handler!(demo_handle_post));
  server.add_route("/test", Rt::PUT, handler!(demo_handle_put));
  server.add_route("/test", Rt::PATCH, handler!(demo_handle_put));
  server.add_route("/test", Rt::DELETE, handler!(demo_handle_delete));
  server.add_route("/test", Rt::HEAD, handler!(demo_handle_head));
  server.add_route("/test", Rt::OPTIONS, handler!(demo_handle_options));
  server.add_route("/test", Rt::CONNECT, handler!(demo_handle_connect));
  server.add_route("/test", Rt::TRACE, handler!(demo_handle_trace));
  server.add_route("/redirect", Rt::GET, handler!(demo_handle_redirect));
  server.add_route("/json", Rt::GET, handler!(demo_handle_json));
  server.add_route("/custom-header", Rt::GET, handler!(demo_handle_custom_header));
  let res_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("res");
  server.add_files_source(res_path.to_str().unwrap());
  server
}

async fn regular_server_definition() -> Server {
  common_server_definition(REGULAR_SERVER_URL).await
}

async fn strict_server_definition() -> Server {
  common_server_definition(STRICT_SERVER_URL).await
}

async fn create_test_server() -> Server {
  regular_server_definition().await
}

async fn boot_regular() {
  setup_test_server(Some(REGULAR_SERVER_URL), || create_test_server()).await;
}

async fn boot_strict() {
  setup_test_server(Some(STRICT_SERVER_URL), || strict_server_definition()).await;
}

async fn run_regular(request: &[u8], expected: &[u8]) -> String {
  run_test(request, expected, Some(REGULAR_SERVER_URL)).await
}

async fn run_strict(request: &[u8], expected: &[u8]) -> String {
  run_test(request, expected, Some(STRICT_SERVER_URL)).await
}

async fn demo_handle_home(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"home".to_vec(),
  }
}

async fn demo_handle_post(_request: &Request) -> Response {
  let mut ordered: BTreeMap<&String, &String> = BTreeMap::new();
  for (k, v) in &_request.params {
    ordered.insert(k, v);
  }
  let body = format!(
    "Method: {}\nUri: {}\nParams: {:?}\nBody: {:?}",
    _request.method, _request.path, ordered, _request.body
  );
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: body.into_bytes(),
  }
}

async fn demo_handle_get(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"get".to_vec(),
  }
}

async fn demo_handle_put(_request: &Request) -> Response {
  let body = format!(
    "Method: {}\nUri: {}\nParams: {:?}\nBody: {:?}",
    _request.method, _request.path, _request.params, _request.body
  );
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: body.into_bytes(),
  }
}

async fn demo_handle_delete(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"delete".to_vec(),
  }
}

async fn demo_handle_head(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"head".to_vec(),
  }
}

async fn demo_handle_options(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"options".to_vec(),
  }
}

async fn demo_handle_connect(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"connect".to_vec(),
  }
}

async fn demo_handle_trace(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![],
    body: b"trace".to_vec(),
  }
}

async fn demo_handle_redirect(_request: &Request) -> Response {
  Response {
    status: StatusCode::TemporaryRedirect.to_string(),
    headers: vec![
      ("Location".to_string(), "https://example.com".to_string()),
      ("Content-Type".to_string(), "text/plain".to_string()),
    ],
    body: Vec::new(),
  }
}

async fn demo_handle_json(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![("Content-Type".to_string(), "application/json".to_string())],
    body: br#"{"ok":true}"#.to_vec(),
  }
}

async fn demo_handle_custom_header(_request: &Request) -> Response {
  Response {
    status: StatusCode::Ok.to_string(),
    headers: vec![
      ("Content-Type".to_string(), "text/plain".to_string()),
      ("X-Trace-Id".to_string(), "abc-123".to_string()),
    ],
    body: b"custom".to_vec(),
  }
}

#[async_std::test]
async fn test_home() {
  boot_regular().await;
  let request = b"GET / HTTP/1.1\r\n\r\n";
  let expected = b"home";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_get() {
  boot_regular().await;
  let request = b"GET /test HTTP/1.1\r\n\r\n";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_get_with_query() {
  boot_regular().await;
  let request = b"GET /test?foo=bar&baz=qux HTTP/1.1\r\n\r\n";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_get_no_content_length() {
  boot_regular().await;
  let request = b"GET /test HTTP/1.1\r\n\r\n";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_get_with_content_length_matching_body() {
  boot_regular().await;
  let request = b"GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nping";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_get_with_content_length_smaller_than_body() {
  boot_regular().await;
  let request = b"GET /test HTTP/1.1\r\nContent-Length: 1\r\n\r\npong";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_get_with_content_length_larger_than_body() {
  boot_regular().await;
  let request = b"GET /test HTTP/1.1\r\nContent-Length: 10\r\n\r\nhi";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\n\r\nmueve tu cuerpo";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"mueve tu cuerpo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_without_content_length_empty_body() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\n\r\n";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_query() {
  boot_regular().await;
  let request = b"POST /test?foo=bar HTTP/1.1\r\n\r\nmueve tu cuerpo";
  let expected = b"Method: POST\nUri: /test\nParams: {\"foo\": \"bar\"}\nBody: \"mueve tu cuerpo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_content_length() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\nContent-Length: 15\r\n\r\nmueve tu cuerpo";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"mueve tu cuerpo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_params() {
  boot_regular().await;
  let request = b"POST /test/hola/que?param4=hoy&param3=hace HTTP/1.1\r\n\r\nmueve tu cuerpo";
  let expected =
    b"Method: POST\nUri: /test/hola/que\nParams: {\"param1\": \"hola\", \"param2\": \"que\", \"param3\": \"hace\", \"param4\": \"hoy\"}\nBody: \"mueve tu cuerpo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_incomplete_path_params() {
  boot_regular().await;
  let request = b"POST /test/hola HTTP/1.1\r\n\r\nmueve tu cuerpo";
  let expected = b"Method: POST\nUri: /test/hola\nParams: {\"param1\": \"hola\"}\nBody: \"mueve tu cuerpo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_without_content_length_body() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\n\r\nbody";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"body\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_matching_content_length() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"body\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_smaller_content_length() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\nContent-Length: 2\r\n\r\nbody";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"bo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_post_with_larger_content_length() {
  boot_regular().await;
  let request = b"POST /test HTTP/1.1\r\nContent-Length: 10\r\n\r\nbody";
  let expected = b"HTTP/1.1 200 OK";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_put() {
  boot_regular().await;
  let request = b"PUT /test HTTP/1.1\r\n\r\nmueve tu cuerpo";
  let expected = b"Method: PUT\nUri: /test\nParams: {}\nBody: \"mueve tu cuerpo\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_put_without_content_length() {
  boot_regular().await;
  let request = b"PUT /test HTTP/1.1\r\n\r\nput";
  let expected = b"Method: PUT\nUri: /test\nParams: {}\nBody: \"put\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_put_with_matching_content_length() {
  boot_regular().await;
  let request = b"PUT /test HTTP/1.1\r\nContent-Length: 3\r\n\r\nput";
  let expected = b"Method: PUT\nUri: /test\nParams: {}\nBody: \"put\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_put_with_smaller_content_length() {
  boot_regular().await;
  let request = b"PUT /test HTTP/1.1\r\nContent-Length: 1\r\n\r\nput";
  let expected = b"Method: PUT\nUri: /test\nParams: {}\nBody: \"p\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_put_with_larger_content_length() {
  boot_regular().await;
  let request = b"PUT /test HTTP/1.1\r\nContent-Length: 8\r\n\r\nput";
  let expected = b"HTTP/1.1 200 OK";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_patch() {
  boot_regular().await;
  let request = b"PATCH /test HTTP/1.1\r\n\r\npatch";
  let expected = b"Method: PATCH\nUri: /test\nParams: {}\nBody: \"patch\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_head() {
  boot_regular().await;
  let request = b"HEAD /test HTTP/1.1\r\n\r\n";
  let expected = b"head";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_options() {
  boot_regular().await;
  let request = b"OPTIONS /test HTTP/1.1\r\n\r\n";
  let expected = b"options";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_connect() {
  boot_regular().await;
  let request = b"CONNECT /test HTTP/1.1\r\n\r\n";
  let expected = b"connect";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_trace() {
  boot_regular().await;
  let request = b"TRACE /test HTTP/1.1\r\n\r\n";
  let expected = b"trace";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_delete() {
  boot_regular().await;
  let request = b"DELETE /test HTTP/1.1\r\n\r\n";
  let expected = b"delete";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_delete_no_content_length() {
  boot_regular().await;
  let request = b"DELETE /test HTTP/1.1\r\n\r\n";
  let expected = b"delete";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_delete_with_content_length_matching_body() {
  boot_regular().await;
  let request = b"DELETE /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nping";
  let expected = b"delete";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_delete_with_content_length_smaller_than_body() {
  boot_regular().await;
  let request = b"DELETE /test HTTP/1.1\r\nContent-Length: 1\r\n\r\nping";
  let expected = b"delete";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_delete_with_content_length_larger_than_body() {
  boot_regular().await;
  let request = b"DELETE /test HTTP/1.1\r\nContent-Length: 20\r\n\r\nping";
  let expected = b"delete";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_strict_mode_without_content_length() {
  boot_strict().await;
  let request = b"POST /test HTTP/1.1\r\n\r\npayload";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"payload\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_strict(request, expected).await;
}

#[async_std::test]
async fn test_strict_mode_with_content_length() {
  boot_strict().await;
  let request = b"POST /test HTTP/1.1\r\nContent-Length: 7\r\n\r\npayload";
  let expected = b"Method: POST\nUri: /test\nParams: {}\nBody: \"payload\"";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_strict(request, expected).await;
}

#[async_std::test]
async fn test_strict_mode_get_without_content_length() {
  boot_strict().await;
  let request = b"GET /test HTTP/1.1\r\n\r\n";
  let expected = b"get";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_strict(request, expected).await;
}

#[async_std::test]
async fn test_file_exists() {
  boot_regular().await;
  let request = b"GET /numano.png HTTP/1.1\r\nHost: localhost\r\n\r\n";
  let expected = b"HTTP/1.1 200 OK";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_file_not_found() {
  boot_regular().await;
  let request = b"GET /test.png HTTP/1.1\r\n\r\n";
  let expected = b"HTTP/1.1 404 Not Found";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_method_not_allowed() {
  boot_regular().await;
  let request = b"BREW /coffee HTTP/1.1\r\n\r\n";
  let expected = b"HTTP/1.1 405 Method Not Allowed";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_allowed_method_missing_route() {
  boot_regular().await;
  let request = b"TRACE /missing HTTP/1.1\r\n\r\n";
  let expected = b"HTTP/1.1 404 Not Found";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_empty_request() {
  boot_regular().await;
  let request = b"";
  let expected = b"HTTP/1.1 400 Bad Request";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_malformed_request() {
  boot_regular().await;
  let request = b"THIS_IS_NOT_HTTP\r\n\r\n";
  let expected = b"HTTP/1.1 400 Bad Request";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_unsupported_http_version() {
  boot_regular().await;
  let request = b"GET / HTTP/0.9\r\n\r\n";
  let expected = b"HTTP/1.1 505 HTTP Version Not Supported";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_long_path() {
  boot_regular().await;
  let long_path = "/".to_string() + &"a".repeat(10_000);
  let request = format!("GET {} HTTP/1.1\r\n\r\n", long_path);
  let expected = b"HTTP/1.1 414 URI Too Long";
  run_regular(request.as_bytes(), expected).await;
}

#[async_std::test]
async fn test_missing_method() {
  boot_regular().await;
  let request = b"/ HTTP/1.1\r\n\r\n";
  let expected = b"HTTP/1.1 400 Bad Request";
  async_std::task::sleep(std::time::Duration::from_millis(100)).await;
  run_regular(request, expected).await;
}

#[async_std::test]
async fn test_redirect_with_location_header() {
  boot_regular().await;
  let response = run_regular(b"GET /redirect HTTP/1.1\r\n\r\n", b"HTTP/1.1 307 Temporary Redirect").await;
  assert!(
    response.contains("Location: https://example.com"),
    "missing Location header: {}",
    response
  );
  assert!(
    response.contains("Content-Length: 0"),
    "wrong Content-Length for redirect: {}",
    response
  );
}

#[async_std::test]
async fn test_json_content_type_header() {
  boot_regular().await;
  let response = run_regular(b"GET /json HTTP/1.1\r\n\r\n", br#"{"ok":true}"#).await;
  assert!(
    response.contains("Content-Type: application/json"),
    "missing JSON content type: {}",
    response
  );
  assert!(
    response.contains("Content-Length: 11"),
    "wrong Content-Length for JSON: {}",
    response
  );
}

#[async_std::test]
async fn test_custom_header_is_serialized() {
  boot_regular().await;
  let response = run_regular(b"GET /custom-header HTTP/1.1\r\n\r\n", b"custom").await;
  assert!(
    response.contains("X-Trace-Id: abc-123"),
    "missing custom header: {}",
    response
  );
  assert!(
    response.contains("Content-Type: text/plain"),
    "missing Content-Type: {}",
    response
  );
}