/src/h2/tests/h2-support/src/prelude.rs
Line | Count | Source |
1 | | // Re-export H2 crate |
2 | | pub use h2; |
3 | | |
4 | | pub use h2::client; |
5 | | pub use h2::ext::Protocol; |
6 | | pub use h2::frame::StreamId; |
7 | | pub use h2::server; |
8 | | pub use h2::*; |
9 | | |
10 | | // Re-export mock |
11 | | pub use super::mock::{self, idle_ms}; |
12 | | |
13 | | // Re-export frames helpers |
14 | | pub use super::frames; |
15 | | |
16 | | // Re-export utility mod |
17 | | pub use super::util; |
18 | | |
19 | | // Re-export some type defines |
20 | | pub use super::{Codec, SendFrame}; |
21 | | |
22 | | // Re-export macros |
23 | | pub use super::{ |
24 | | assert_closed, assert_data, assert_default_settings, assert_go_away, assert_headers, |
25 | | assert_ping, assert_settings, poll_err, poll_frame, raw_codec, |
26 | | }; |
27 | | |
28 | | pub use super::assert::assert_frame_eq; |
29 | | |
30 | | // Re-export useful crates |
31 | | pub use tokio_test::io as mock_io; |
32 | | pub use {bytes, futures, http, tokio::io as tokio_io, tracing, tracing_subscriber}; |
33 | | |
34 | | // Re-export primary future types |
35 | | pub use futures::{Future, Sink, Stream}; |
36 | | |
37 | | // And our Future extensions |
38 | | pub use super::future_ext::{join, join3, join4, join_all, select, try_join, TestFuture}; |
39 | | |
40 | | // Our client_ext helpers |
41 | | pub use super::client_ext::SendRequestExt; |
42 | | |
43 | | // Re-export HTTP types |
44 | | pub use http::{uri, HeaderMap, Method, Request, Response, StatusCode, Version}; |
45 | | |
46 | | pub use bytes::{Buf, BufMut, Bytes, BytesMut}; |
47 | | |
48 | | pub use tokio::io::{AsyncRead, AsyncWrite}; |
49 | | |
50 | | pub use std::thread; |
51 | | pub use std::time::Duration; |
52 | | |
53 | | pub static MAGIC_PREFACE: &[u8] = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; |
54 | | |
55 | | // ===== Everything under here shouldn't be used ===== |
56 | | // TODO: work on deleting this code |
57 | | |
58 | | use futures::future; |
59 | | pub use futures::future::poll_fn; |
60 | | use futures::future::Either::*; |
61 | | use std::pin::Pin; |
62 | | |
63 | | pub trait MockH2 { |
64 | | fn handshake(&mut self) -> &mut Self; |
65 | | |
66 | | fn handshake_read_settings(&mut self, settings: &[u8]) -> &mut Self; |
67 | | } |
68 | | |
69 | | impl MockH2 for tokio_test::io::Builder { |
70 | | fn handshake(&mut self) -> &mut Self { |
71 | | self.handshake_read_settings(frames::SETTINGS) |
72 | | } |
73 | | |
74 | | fn handshake_read_settings(&mut self, settings: &[u8]) -> &mut Self { |
75 | | self.write(MAGIC_PREFACE) |
76 | | // Settings frame |
77 | | .write(frames::SETTINGS) |
78 | | .read(settings) |
79 | | .read(frames::SETTINGS_ACK) |
80 | | } |
81 | | } |
82 | | |
83 | | pub trait ClientExt { |
84 | | fn run<'a, F: Future + Unpin + 'a>( |
85 | | &'a mut self, |
86 | | f: F, |
87 | | ) -> Pin<Box<dyn Future<Output = F::Output> + 'a>>; |
88 | | } |
89 | | |
90 | | impl<T, B> ClientExt for client::Connection<T, B> |
91 | | where |
92 | | T: AsyncRead + AsyncWrite + Unpin + 'static, |
93 | | B: Buf, |
94 | | { |
95 | 0 | fn run<'a, F: Future + Unpin + 'a>( |
96 | 0 | &'a mut self, |
97 | 0 | f: F, |
98 | 0 | ) -> Pin<Box<dyn Future<Output = F::Output> + 'a>> { |
99 | 0 | let res = future::select(self, f); |
100 | 0 | Box::pin(async { |
101 | 0 | match res.await { |
102 | 0 | Left((Ok(_), b)) => { |
103 | | // Connection is done... |
104 | 0 | b.await |
105 | | } |
106 | 0 | Right((v, _)) => v, |
107 | 0 | Left((Err(e), _)) => panic!("err: {:?}", e), |
108 | | } |
109 | 0 | }) |
110 | 0 | } |
111 | | } |
112 | | |
113 | | pub fn build_large_headers() -> Vec<(&'static str, String)> { |
114 | | vec![ |
115 | | ("one", "hello".to_string()), |
116 | | ("two", build_large_string('2', 4 * 1024)), |
117 | | ("three", "three".to_string()), |
118 | | ("four", build_large_string('4', 4 * 1024)), |
119 | | ("five", "five".to_string()), |
120 | | ("six", build_large_string('6', 4 * 1024)), |
121 | | ("seven", "seven".to_string()), |
122 | | ("eight", build_large_string('8', 4 * 1024)), |
123 | | ("nine", "nine".to_string()), |
124 | | ("ten", build_large_string('0', 4 * 1024)), |
125 | | ("eleven", build_large_string('1', 32 * 1024)), |
126 | | ] |
127 | | } |
128 | | |
129 | | fn build_large_string(ch: char, len: usize) -> String { |
130 | | let mut ret = String::new(); |
131 | | |
132 | | for _ in 0..len { |
133 | | ret.push(ch); |
134 | | } |
135 | | |
136 | | ret |
137 | | } |