refactor(tests): Use local httpbin instance in tests

Instead of relying on the external httpbin instance which is
occasionally wonky, use Docker to spin up a local instance in Travis
CI when testing.

This fixes #1.
This commit is contained in:
Vincent Ambo 2019-02-27 14:30:40 +01:00 committed by Vincent Ambo
parent 67bfba446d
commit 951bc7ae85
2 changed files with 23 additions and 10 deletions

View file

@ -1,2 +1,8 @@
language: rust language: rust
cache: cargo cache: cargo
# Configure local httpbin instance:
services:
- docker
before_install:
- docker run -d -p 4662:80 kennethreitz/httpbin

View file

@ -1,12 +1,19 @@
// All tests expect an httpbin instance to be available at
// `http://localhost:4662`.
//
// This is easily spun up using Docker by running:
//
// docker run --rm -p 4662:80 kennethreitz/httpbin
use super::*; use super::*;
use serde_json::{Value, json}; use serde_json::{Value, json};
// These tests check whether the correct HTTP method is used in the // These tests check whether the correct HTTP method is used in the
// requests. httpbin will return 405-statuses for incorrect methods. // requests.
#[test] #[test]
fn test_http_get() { fn test_http_get() {
let resp = Request::get("https://httpbin.org/get") let resp = Request::get("http://127.0.0.1:4662/get")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert!(resp.is_success(), "request should have succeeded"); assert!(resp.is_success(), "request should have succeeded");
@ -14,7 +21,7 @@ fn test_http_get() {
#[test] #[test]
fn test_http_delete() { fn test_http_delete() {
let resp = Request::delete("https://httpbin.org/delete") let resp = Request::delete("http://127.0.0.1:4662/delete")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
@ -22,7 +29,7 @@ fn test_http_delete() {
#[test] #[test]
fn test_http_put() { fn test_http_put() {
let resp = Request::put("https://httpbin.org/put") let resp = Request::put("http://127.0.0.1:4662/put")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
@ -30,7 +37,7 @@ fn test_http_put() {
#[test] #[test]
fn test_http_patch() { fn test_http_patch() {
let resp = Request::patch("https://httpbin.org/patch") let resp = Request::patch("http://127.0.0.1:4662/patch")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
@ -42,7 +49,7 @@ fn test_http_patch() {
#[test] #[test]
fn test_http_post() { fn test_http_post() {
let body = "test body"; let body = "test body";
let response = Request::post("https://httpbin.org/post") let response = Request::post("http://127.0.0.1:4662/post")
.user_agent("crimp test suite").expect("failed to set user-agent") .user_agent("crimp test suite").expect("failed to set user-agent")
.timeout(Duration::from_secs(5)).expect("failed to set request timeout") .timeout(Duration::from_secs(5)).expect("failed to set request timeout")
.body("text/plain", &body.as_bytes()) .body("text/plain", &body.as_bytes())
@ -69,7 +76,7 @@ fn test_http_post_json() {
"purpose": "testing!" "purpose": "testing!"
}); });
let response = Request::post("https://httpbin.org/post") let response = Request::post("http://127.0.0.1:4662/post")
.user_agent("crimp test suite").expect("failed to set user-agent") .user_agent("crimp test suite").expect("failed to set user-agent")
.timeout(Duration::from_secs(5)).expect("failed to set request timeout") .timeout(Duration::from_secs(5)).expect("failed to set request timeout")
.json(&body).expect("request serialization failed") .json(&body).expect("request serialization failed")
@ -96,7 +103,7 @@ fn test_http_post_json() {
#[test] #[test]
fn test_bearer_auth() { fn test_bearer_auth() {
let response = Request::get("https://httpbin.org/bearer") let response = Request::get("http://127.0.0.1:4662/bearer")
.bearer_auth("some-token").expect("failed to set auth header") .bearer_auth("some-token").expect("failed to set auth header")
.send().expect("failed to send request"); .send().expect("failed to send request");
@ -105,7 +112,7 @@ fn test_bearer_auth() {
#[test] #[test]
fn test_basic_auth() { fn test_basic_auth() {
let request = Request::get("https://httpbin.org/basic-auth/alan_watts/oneness"); let request = Request::get("http://127.0.0.1:4662/basic-auth/alan_watts/oneness");
let response = request let response = request
.basic_auth("alan_watts", "oneness").expect("failed to set auth header") .basic_auth("alan_watts", "oneness").expect("failed to set auth header")
@ -136,7 +143,7 @@ fn test_large_body() {
#[test] #[test]
fn test_error_for_status() { fn test_error_for_status() {
let response = Request::get("https://httpbin.org/patch") let response = Request::get("http://127.0.0.1:4662/patch")
.send().expect("failed to send request") .send().expect("failed to send request")
.error_for_status(|resp| format!("Response error code: {}", resp.status)); .error_for_status(|resp| format!("Response error code: {}", resp.status));