Skip to content

Commit

Permalink
Merge pull request http-rs#130 from http-rs/zkat/mock-tests
Browse files Browse the repository at this point in the history
tests: stop hitting the network when running the test suite
  • Loading branch information
yoshuawuyts committed Dec 17, 2019
2 parents 27d26f8 + 502ee52 commit c033bf2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ features = [
async-std = { version = "1.0", features = ["attributes"] }
femme = "1.1.0"
serde = { version = "1.0.97", features = ["derive"] }
mockito = "0.22.0"
4 changes: 2 additions & 2 deletions src/middleware/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! # Examples
//!
//! ```
//! ```no_run
//! # #[async_std::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
//! let mut res = surf::get("https://httpbin.org/get")
Expand All @@ -28,7 +28,7 @@ use native::Logger;
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let mut res = surf::get("https://httpbin.org/get")
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Middleware types
//!
//! # Examples
//! ```
//! ```no_run
//! use futures::future::BoxFuture;
//! use surf::middleware::{Next, Middleware, Request, Response, HttpClient};
//! use std::time;
Expand Down Expand Up @@ -30,7 +30,7 @@
//! `Middleware` can also be instantiated using a free function thanks to some convenient trait
//! implementations.
//!
//! ```
//! ```no_run
//! use futures::future::BoxFuture;
//! use surf::middleware::{Next, Middleware, Request, Response, HttpClient};
//! use std::time;
Expand Down
18 changes: 9 additions & 9 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # use serde::{Deserialize, Serialize};
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand All @@ -140,7 +140,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # use serde::{Deserialize, Serialize};
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let req = surf::get("https://httpbin.org/get")
Expand All @@ -193,7 +193,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let req = surf::get("https://httpbin.org/get")
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// use surf::http;
Expand All @@ -251,7 +251,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// use surf::url::Url;
Expand All @@ -278,7 +278,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// use surf::mime;
Expand All @@ -298,7 +298,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// use surf::mime;
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<C: HttpClient> Request<C> {
///
/// # Examples
///
/// ```
/// ```no_run
/// # use serde::{Deserialize, Serialize};
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand Down
28 changes: 19 additions & 9 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use mockito::mock;

#[async_std::test]
async fn post_json() -> Result<(), surf::Exception> {
#[derive(serde::Deserialize, serde::Serialize)]
Expand All @@ -9,22 +11,30 @@ async fn post_json() -> Result<(), surf::Exception> {
name: "Chashu".to_string(),
};

let res = surf::post("https://httpbin.org/post")
.body_json(&cat)?
.await?;
let m = mock("POST", "/")
.with_status(200)
.match_body(&serde_json::to_string(&cat)?[..])
.with_body(&serde_json::to_string(&cat)?[..])
.create();
let res = surf::post(mockito::server_url()).body_json(&cat)?.await?;
m.assert();
assert_eq!(res.status(), 200);
Ok(())
}

#[async_std::test]
async fn get_json() -> Result<(), surf::Exception> {
#[derive(serde::Deserialize)]
struct Ip {
ip: String,
struct Message {
message: String,
}

let uri = "https://api.ipify.org?format=json";
let ip: Ip = surf::get(uri).recv_json().await?;
assert!(ip.ip.len() > 10);
let m = mock("GET", "/")
.with_status(200)
.with_body(r#"{"message": "hello, world!"}"#)
.create();
let uri = &mockito::server_url();
let msg: Message = surf::get(uri).recv_json().await?;
m.assert();
assert_eq!(msg.message, "hello, world!");
Ok(())
}

0 comments on commit c033bf2

Please sign in to comment.