Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/tests/http.rs

2.2 KiB, 10 runs

created by r1870400018:615, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// Exercises the async `HttpMessageReader`, so it belongs to the async build alone.
2#![cfg(feature = "async")]
3
4use oxedyne_fe2o3_net::{
5 conc::AsyncReadIterator,
6 constant,
7 http::msg::HttpMessageReader,
8};
9
10use oxedyne_fe2o3_core::{
11 prelude::*,
12};
13
14use std::{
15 pin::Pin,
16};
17
18
19pub fn test_http(filter: &'static str) -> Outcome<()> {
20
21 match filter {
22 "all" | "request" => {
23 let header = "POST /adduser HTTP/1.1\r\n\
24Host: my.domain.com\r\n\
25User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0\r\n\
26Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\r\n\
27Accept-Language: en-US,en;q=0.5\r\n\
28Accept-Encoding: gzip, deflate, br\r\n\
29Content-Type: application/x-www-form-urlencoded\r\n\
30Content-Length: 25\r\n\
31Origin: null\r\n\
32Connection: keep-alive\r\n\
33Cookie: _ga_PYS2NF62KB=GS1.1.1703943898.1.1.1703943949.0.0.0; _ga=GA1.1.1548945696.1703943899\r\n\
34Upgrade-Insecure-Requests: 1\r\n\
35Sec-Fetch-Dest: document\r\n\
36Sec-Fetch-Mode: navigate\r\n\
37Sec-Fetch-Site: cross-site\r\n\
38Sec-Fetch-User: ?1\r\n\r\n";
39 let body = "username=jane&password=doe";
40 let wire = fmt!("{}{}", header, body);
41 let byts = wire.as_bytes();
42 let mut stream = std::io::Cursor::new(byts);
43 let rt = res!(tokio::runtime::Runtime::new());
44
45 let mut reader: HttpMessageReader<
46 '_,
47 { constant::HTTP_DEFAULT_HEADER_CHUNK_SIZE },
48 { constant::HTTP_DEFAULT_BODY_CHUNK_SIZE },
49 _,
50 > = HttpMessageReader::new(Pin::new(&mut stream));
51
52 let result = rt.block_on(async {
53 reader.next().await
54 });
55
56 if let Some(result) = result {
57 let http_msg = res!(result);
58 let body_str = http_msg.body_as_string();
59 req!(&body_str, body);
60 test!("Successfully parsed HTTP request.");
61 } else {
62 return Err(err!(
63 "Message reading should not have returned None.";
64 Missing, Test));
65 }
66 },
67 _ => (),
68 }
69
70 Ok(())
71}