add form-urlencoded unit test

This commit is contained in:
Sean McArthur
2019-02-13 11:46:12 -08:00
parent 4dc679d535
commit 7eae51f56e
3 changed files with 48 additions and 7 deletions

View File

@@ -43,6 +43,10 @@ matrix:
sudo: false
dist: trusty
env:
global:
- REQWEST_TEST_BODY_FULL=1
script:
- cargo build $FEATURES
- cargo test -v $FEATURES

View File

@@ -172,6 +172,41 @@ fn test_post() {
assert_eq!(n, 0)
}
#[test]
fn test_post_form() {
let server = server! {
request: b"\
POST /form HTTP/1.1\r\n\
user-agent: $USERAGENT\r\n\
accept: */*\r\n\
content-type: application/x-www-form-urlencoded\r\n\
content-length: 24\r\n\
accept-encoding: gzip\r\n\
host: $HOST\r\n\
\r\n\
hello=world&sean=monstar\
",
response: b"\
HTTP/1.1 200 OK\r\n\
Server: post-form\r\n\
Content-Length: 0\r\n\
\r\n\
"
};
let form = &[("hello", "world"), ("sean", "monstar")];
let url = format!("http://{}/form", server.addr());
let res = reqwest::Client::new()
.post(&url)
.form(form)
.send()
.expect("request send");
assert_eq!(res.url().as_str(), &url);
assert_eq!(res.status(), reqwest::StatusCode::OK);
}
/// Calling `Response::error_for_status`` on a response with status in 4xx
/// returns a error.
#[test]

View File

@@ -99,13 +99,15 @@ pub fn spawn(txns: Vec<Txn>) -> Server {
match (::std::str::from_utf8(&expected), ::std::str::from_utf8(&buf[..n])) {
(Ok(expected), Ok(received)) => {
assert_eq!(
expected.len(),
received.len(),
"expected len = {}, received len = {}",
expected.len(),
received.len(),
);
if expected.len() > 300 && ::std::env::var("REQWEST_TEST_BODY_FULL").is_err() {
assert_eq!(
expected.len(),
received.len(),
"expected len = {}, received len = {}; to skip length check and see exact contents, re-run with REQWEST_TEST_BODY_FULL=1",
expected.len(),
received.len(),
);
}
assert_eq!(expected, received)
},
_ => {