diff options
author | Vincent Ambo <mail@tazj.in> | 2019-02-26T13·45+0100 |
---|---|---|
committer | Vincent Ambo <mail@tazj.in> | 2019-02-26T13·45+0100 |
commit | 569e10e50987fdcbdaa5807ba795309623ddad39 (patch) | |
tree | abd931c1897ddfc5547e8c4c3bebfd733136b3a1 /src/tests.rs | |
parent | b71b44a6729f2e41a6a42543002ac7b010f26bb4 (diff) |
test: Add tests for HTTP verbs & POSTing data
Diffstat (limited to 'src/tests.rs')
-rw-r--r-- | src/tests.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 000000000000..f4e3a393eaae --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,94 @@ +use super::*; +use serde_json::{Value, json}; + +// These tests check whether the correct HTTP method is used in the +// requests. httpbin will return 405-statuses for incorrect methods. + +#[test] +fn test_http_get() { + let resp = Request::new(Method::Get, "https://httpbin.org/get") + .expect("failed to create request") + .send() + .expect("failed to send request"); + + assert_eq!(200, resp.status, "response status should be 200 OK"); +} + +#[test] +fn test_http_delete() { + let resp = Request::new(Method::Delete, "https://httpbin.org/delete") + .expect("failed to create request") + .send().expect("failed to send request"); + + assert_eq!(200, resp.status, "response status should be 200 OK"); +} + +#[test] +fn test_http_put() { + let resp = Request::new(Method::Put, "https://httpbin.org/put") + .expect("failed to create request") + .send().expect("failed to send request"); + + assert_eq!(200, resp.status, "response status should be 200 OK"); +} + +#[test] +fn test_http_patch() { + let resp = Request::new(Method::Patch, "https://httpbin.org/patch") + .expect("failed to create request") + .send().expect("failed to send request"); + + assert_eq!(200, resp.status, "response status should be 200 OK"); +} + +#[test] +fn test_http_post() { + let body = "test body"; + let response = Request::new(Method::Post, "https://httpbin.org/post") + .expect("failed to create request") + .user_agent("crimp test suite").expect("failed to set user-agent") + .body("text/plain", &body.as_bytes()) + .send().expect("failed to send request") + .as_json::<Value>().expect("failed to deserialize response"); + + let data = response.body; + + assert_eq!(200, response.status, "response status should be 200 OK"); + + assert_eq!(data.get("data").unwrap(), &json!("test body"), + "test body should have been POSTed"); + + assert_eq!( + data.get("headers").unwrap().get("Content-Type").unwrap(), + &json!("text/plain"), + "Content-Type should be `text/plain`", + ); +} + +#[test] +fn test_http_post_json() { + let body = json!({ + "purpose": "testing!" + }); + + let response = Request::new(Method::Post, "https://httpbin.org/post") + .expect("failed to create request") + .user_agent("crimp test suite").expect("failed to set user-agent") + .json(&body).expect("request serialization failed") + .send().expect("failed to send request") + .as_json::<Value>().expect("failed to deserialize response"); + + + let data = response.body; + + assert_eq!(200, response.status, "response status should be 200 OK"); + + assert_eq!(data.get("json").unwrap(), &body, + "test body should have been POSTed"); + + assert_eq!( + data.get("headers").unwrap().get("Content-Type").unwrap(), + &json!("application/json"), + "Content-Type should be `application/json`", + ); +} |