Skip to content

Commit 0e06d0d

Browse files
committed
Improve hyper imports in recording proxy
1 parent 278d265 commit 0e06d0d

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/tests/record.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
};
1515

1616
use futures::{channel::oneshot, future, prelude::*};
17+
use hyper::{Body, Error, Request, Response, Server, StatusCode, Uri};
1718
use tokio::{
1819
net::{TcpListener, TcpStream},
1920
runtime::current_thread::Runtime,
@@ -114,7 +115,7 @@ pub fn proxy() -> (String, Bomb) {
114115
};
115116

116117
let record = Arc::new(Mutex::new(record));
117-
let srv = hyper::Server::builder(listener.incoming())
118+
let srv = Server::builder(listener.incoming())
118119
.serve(Proxy {
119120
sink: sink2,
120121
record: Arc::clone(&record),
@@ -153,17 +154,16 @@ struct Proxy {
153154
client: Option<Client>,
154155
}
155156

156-
impl tower_service::Service<hyper::Request<hyper::Body>> for Proxy {
157-
type Response = hyper::Response<hyper::Body>;
158-
type Error = hyper::Error;
159-
type Future =
160-
Pin<Box<dyn Future<Output = Result<hyper::Response<hyper::Body>, hyper::Error>> + Send>>;
157+
impl tower_service::Service<Request<Body>> for Proxy {
158+
type Response = Response<Body>;
159+
type Error = Error;
160+
type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send>>;
161161

162162
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
163163
Poll::Ready(Ok(()))
164164
}
165165

166-
fn call(&mut self, req: hyper::Request<hyper::Body>) -> Self::Future {
166+
fn call(&mut self, req: Request<Body>) -> Self::Future {
167167
match *self.record.lock().unwrap() {
168168
Record::Capture(_, _) => {
169169
let client = self.client.as_ref().unwrap().clone();
@@ -185,8 +185,8 @@ impl tower_service::Service<hyper::Request<hyper::Body>> for Proxy {
185185

186186
impl<'a> tower_service::Service<&'a TcpStream> for Proxy {
187187
type Response = Proxy;
188-
type Error = hyper::Error;
189-
type Future = Pin<Box<dyn Future<Output = Result<Proxy, hyper::Error>> + Send + 'static>>;
188+
type Error = Error;
189+
type Future = Pin<Box<dyn Future<Output = Result<Proxy, Error>> + Send + 'static>>;
190190

191191
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
192192
Poll::Ready(Ok(()))
@@ -199,32 +199,29 @@ impl<'a> tower_service::Service<&'a TcpStream> for Proxy {
199199

200200
#[derive(Serialize, Deserialize)]
201201
struct Exchange {
202-
request: Request,
203-
response: Response,
202+
request: RecordedRequest,
203+
response: RecordedResponse,
204204
}
205205

206206
#[derive(Serialize, Deserialize)]
207-
struct Request {
207+
struct RecordedRequest {
208208
uri: String,
209209
method: String,
210210
headers: HashSet<(String, String)>,
211211
body: String,
212212
}
213213

214214
#[derive(Serialize, Deserialize)]
215-
struct Response {
215+
struct RecordedResponse {
216216
status: u16,
217217
headers: HashSet<(String, String)>,
218218
body: String,
219219
}
220220

221221
type Client = hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>;
222-
type ResponseAndExchange = (hyper::Response<hyper::Body>, Exchange);
222+
type ResponseAndExchange = (Response<Body>, Exchange);
223223

224-
async fn record_http(
225-
req: hyper::Request<hyper::Body>,
226-
client: Client,
227-
) -> Result<ResponseAndExchange, hyper::Error> {
224+
async fn record_http(req: Request<Body>, client: Client) -> Result<ResponseAndExchange, Error> {
228225
// Deconstruct the incoming request and await for the full body
229226
let (header_parts, body) = req.into_parts();
230227
let method = header_parts.method;
@@ -233,7 +230,7 @@ async fn record_http(
233230
let body = body.try_concat().await?;
234231

235232
// Save info on the incoming request for the exchange log
236-
let request = Request {
233+
let request = RecordedRequest {
237234
uri: uri.to_string(),
238235
method: method.to_string(),
239236
headers: headers
@@ -245,8 +242,8 @@ async fn record_http(
245242

246243
// Construct an outgoing request
247244
let uri = uri.to_string().replace("http://", "https://");
248-
let uri = uri.parse::<hyper::Uri>().unwrap();
249-
let mut req = hyper::Request::builder()
245+
let uri = uri.parse::<Uri>().unwrap();
246+
let mut req = Request::builder()
250247
.method(method.clone())
251248
.uri(uri)
252249
.body(body.into())
@@ -260,7 +257,7 @@ async fn record_http(
260257
let body = hyper_response.into_body().try_concat().await?;
261258

262259
// Save the response for the exchange log
263-
let response = Response {
260+
let response = RecordedResponse {
264261
status: status.as_u16(),
265262
headers: headers
266263
.iter()
@@ -270,7 +267,7 @@ async fn record_http(
270267
};
271268

272269
// Construct an outgoing response
273-
let mut hyper_response = hyper::Response::builder()
270+
let mut hyper_response = Response::builder()
274271
.status(status)
275272
.body(body.into())
276273
.unwrap();
@@ -280,10 +277,10 @@ async fn record_http(
280277
}
281278

282279
fn replay_http(
283-
req: hyper::Request<hyper::Body>,
280+
req: Request<Body>,
284281
mut exchange: Exchange,
285282
stdout: &mut dyn Write,
286-
) -> impl Future<Output = Result<hyper::Response<hyper::Body>, hyper::Error>> + Send {
283+
) -> impl Future<Output = Result<Response<Body>, Error>> + Send {
287284
static IGNORED_HEADERS: &[&str] = &["authorization", "date", "user-agent"];
288285

289286
assert_eq!(req.uri().to_string(), exchange.request.uri);
@@ -319,8 +316,8 @@ fn replay_http(
319316
base64::decode(&exchange.request.body).unwrap()
320317
);
321318

322-
let mut builder = hyper::Response::builder();
323-
builder.status(hyper::StatusCode::from_u16(exchange.response.status).unwrap());
319+
let mut builder = Response::builder();
320+
builder.status(StatusCode::from_u16(exchange.response.status).unwrap());
324321
for (key, value) in exchange.response.headers {
325322
builder.header(key.as_str(), value.as_str());
326323
}

0 commit comments

Comments
 (0)