Skip to content

Commit

Permalink
Test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexliesenfeld committed Sep 22, 2020
1 parent 1a5f2c7 commit 6ee9f3e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
9 changes: 4 additions & 5 deletions README.md
Expand Up @@ -56,11 +56,10 @@ fn example_test() {

// Create a mock on the mock server. The mock will return HTTP status code 200 whenever
// the mock server receives a GET-request with path "/hello".
let hello_mock = Mock::new()
.expect_method(GET)
.expect_path("/hello")
.return_status(200)
.create_on(&mock_server);
let hello_mock = mock_server.mock(|when, then| {
when.method(GET);
then.status(200);
});

// Send an HTTP request to the mock server. This simulates your code.
// The mock_server variable is being used to generate a mock server URL for path "/hello".
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Expand Up @@ -388,19 +388,22 @@ impl MockServer {
/// ```
pub fn mock<F>(&self, mut config_fn: F) -> MockRef
where
F: FnOnce(When, Then),
F: FnOnce(Expectations, Responders),
{
let mock = Rc::new(Cell::new(Mock::new()));
config_fn(When { mock: mock.clone() }, Then { mock: mock.clone() });
config_fn(
Expectations { mock: mock.clone() },
Responders { mock: mock.clone() },
);
mock.take().create_on(self)
}
}

pub struct When {
pub struct Expectations {
pub(crate) mock: Rc<Cell<Mock>>,
}

impl When {
impl Expectations {
pub fn method(self, method: Method) -> Self {
self.mock.set(self.mock.take().expect_method(method));
self
Expand Down Expand Up @@ -495,11 +498,11 @@ impl When {
}
}

pub struct Then {
pub struct Responders {
pub(crate) mock: Rc<Cell<Mock>>,
}

impl Then {
impl Responders {
pub fn status(self, status: usize) -> Self {
self.mock.set(self.mock.take().return_status(status));
self
Expand Down
26 changes: 13 additions & 13 deletions tests/body_matching_tests.rs
Expand Up @@ -59,19 +59,19 @@ fn exact_body_match_test() {
let _ = env_logger::try_init();
let mock_server = MockServer::start();

let m = Mock::new()
.expect_method(POST)
.expect_path("/users")
.expect_header("Content-Type", "application/json")
.expect_json_body_obj(&TestUser {
name: String::from("Fred"),
})
.return_status(201)
.return_header("Content-Type", "application/json")
.return_json_body_obj(&TestUser {
name: String::from("Hans"),
})
.create_on(&mock_server);
let m = mock_server.mock(|when, then| {
when.method(POST)
.path("/users")
.header("Content-Type", "application/json")
.json_body_obj(&TestUser {
name: String::from("Fred"),
});
then.status(201)
.header("Content-Type", "application/json")
.json_body_obj(&TestUser {
name: String::from("Hans"),
});
});

// Act: Send the request and deserialize the response to JSON
let mut response = Request::post(&format!("http://{}/users", mock_server.address()))
Expand Down
9 changes: 4 additions & 5 deletions tests/getting_started_tests.rs
Expand Up @@ -21,11 +21,10 @@ fn example_test() {

// Create a mock on the mock server. The mock will return HTTP status code 200 whenever
// the mock server receives a GET-request with path "/hello".
let hello_mock = Mock::new()
.expect_method(GET)
.expect_path("/hello")
.return_status(200)
.create_on(&mock_server);
let hello_mock = mock_server.mock(|when, then| {
when.method(GET);
then.status(200);
});

// Send an HTTP request to the mock server. This simulates your code.
// The mock_server variable is being used to generate a mock server URL for path "/hello".
Expand Down
2 changes: 1 addition & 1 deletion tests/multiserver_tests.rs
Expand Up @@ -5,7 +5,7 @@ use isahc::{get, get_async, HttpClientBuilder};

use httpmock::Method::{GET, POST};
use httpmock::{Mock, MockServer, MockServerRequest, Regex};
use httpmock_macros::test_executors;
use httpmock_macros::httpmock_example_test;
use isahc::config::RedirectPolicy;
use std::fs::read_to_string;
use std::time::{Duration, SystemTime};
Expand Down

0 comments on commit 6ee9f3e

Please sign in to comment.