From d8d10a292a0d6bd0134fcfe785718bb8f48dd085 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 25 Aug 2020 14:10:44 -0700 Subject: tests: proper URIs; filter request bodies --- tests/common/mod.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'tests/common') diff --git a/tests/common/mod.rs b/tests/common/mod.rs index a65fb2e..890dead 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,20 +1,21 @@ +use hyper::{Request, Body, Method}; use std::path::Path; use std::io::BufRead; -pub struct ExampleRequest { +pub struct ExampleParts { pub method: String, pub path_and_query: String, pub body: Option, } -pub fn load_request_by_name(name: &str) -> ExampleRequest { +pub fn load_parts_by_name(name: &str) -> ExampleParts { let path = format!("tests/files/{}.txt", name); let path = Path::new(&path); - load_request(&path) + load_parts(&path) } -pub fn load_request(path: &Path) -> ExampleRequest { +pub fn load_parts(path: &Path) -> ExampleParts { let file = std::fs::File::open(path).unwrap(); let mut lines = std::io::BufReader::new(file).lines(); @@ -26,9 +27,21 @@ pub fn load_request(path: &Path) -> ExampleRequest { Some(body.join("\n")) }; - ExampleRequest { + ExampleParts { method: first_line[0].clone(), path_and_query: first_line[1].clone(), body: body, } } + +pub fn load_request(path: &Path) -> Request { + let parts = load_parts(path); + Request::builder() + .uri(parts.path_and_query) + .method(Method::from_bytes(parts.method.as_bytes()).expect("valid method in example text file")) + .body(match parts.body { + Some(data) => Body::from(data), + None => Body::empty(), + }) + .expect("constructing upstream request") +} -- cgit v1.2.3