Add unittest for cookie handling on redirects.

This commit is contained in:
Jacob Hoffman-Andrews
2020-08-04 22:37:43 -07:00
committed by Martin Algesten
parent d52fa78ebc
commit 99cdff7519
2 changed files with 74 additions and 6 deletions

View File

@@ -99,3 +99,50 @@ fn connection_reuse() {
} }
assert_eq!(resp.status(), 200); assert_eq!(resp.status(), 200);
} }
#[cfg(test)]
fn cookie_and_redirect(mut stream: TcpStream) -> io::Result<()> {
let headers = read_headers(&stream);
match headers.path() {
"/first" => {
stream.write_all(b"HTTP/1.1 302 Found\r\n")?;
stream.write_all(b"Location: /second\r\n")?;
stream.write_all(b"Set-Cookie: first=true\r\n")?;
stream.write_all(b"Content-Length: 0\r\n\r\n")?;
},
"/second" => {
if headers.headers().iter().find(|&x| x.contains("Cookie: first")).is_none() {
panic!("request did not contain cookie 'first'");
}
stream.write_all(b"HTTP/1.1 302 Found\r\n")?;
stream.write_all(b"Location: /third\r\n")?;
stream.write_all(b"Set-Cookie: second=true\r\n")?;
stream.write_all(b"Content-Length: 0\r\n\r\n")?;
},
"/third" => {
if headers.headers().iter().find(|&x| x.contains("Cookie: first")).is_none() {
panic!("request did not contain cookie 'second'");
}
stream.write_all(b"HTTP/1.1 200 OK\r\n")?;
stream.write_all(b"Set-Cookie: third=true\r\n")?;
stream.write_all(b"Content-Length: 0\r\n\r\n")?;
},
_ => {},
}
Ok(())
}
#[cfg(feature = "cookie")]
#[test]
fn test_cookies_on_redirect() {
let testserver = TestServer::new(cookie_and_redirect);
let url = format!("http://localhost:{}/first", testserver.port);
let agent = Agent::default().build();
let resp = agent.post(&url).call();
if resp.error() {
panic!("error: {} {}", resp.status(), resp.into_string().unwrap());
}
assert!(agent.cookie("first").is_some());
assert!(agent.cookie("second").is_some());
assert!(agent.cookie("third").is_some());
}

View File

@@ -9,14 +9,35 @@ pub struct TestServer {
pub done: Arc<AtomicBool>, pub done: Arc<AtomicBool>,
} }
pub struct TestHeaders(Vec<String>);
impl TestHeaders {
// Return the path for a request, e.g. /foo from "GET /foo HTTP/1.1"
pub fn path(&self) -> &str {
if self.0.len() == 0 {
""
} else {
&self.0[0].split(" ").nth(1).unwrap()
}
}
pub fn headers(&self) -> &[String] {
&self.0[1..]
}
}
// Read a stream until reaching a blank line, in order to consume // Read a stream until reaching a blank line, in order to consume
// request headers. // request headers.
pub fn read_headers(stream: &TcpStream) { pub fn read_headers(stream: &TcpStream) -> TestHeaders {
let mut results = vec![];
for line in BufReader::new(stream).lines() { for line in BufReader::new(stream).lines() {
if line.unwrap() == "" { match line {
break; Err(e) => panic!(e),
} Ok(line) if line == "" => break,
Ok(line) => results.push(line),
};
} }
TestHeaders(results)
} }
impl TestServer { impl TestServer {