Struct httpmock::Then[][src]

pub struct Then { /* fields omitted */ }
Expand description

A type that allows the specification of HTTP response values.

Implementations

Sets the HTTP response code that will be returned by the mock server.

  • status - The status code.

Example:

use httpmock::prelude::*;

// Arrange
let server = MockServer::start();

let m = server.mock(|when, then|{
    when.path("/hello");
    then.status(200);
});

// Act
let response = isahc::get(server.url("/hello")).unwrap();

// Assert
m.assert();
assert_eq!(response.status(), 200);

Sets the HTTP response body that will be returned by the mock server.

  • body - The response body content.

Example:

use httpmock::prelude::*;
use isahc::{prelude::*, ResponseExt};

// Arrange
let server = MockServer::start();

let m = server.mock(|when, then| {
    when.path("/hello");
    then.status(200)
        .body("ohi!");
});

// Act
let mut response = isahc::get(server.url("/hello")).unwrap();

// Assert
m.assert();
assert_eq!(response.status(), 200);
assert_eq!(response.text().unwrap(), "ohi!");

Sets the HTTP response body that will be returned by the mock server.

  • body - The response body content.

Example:

use httpmock::prelude::*;
use isahc::{prelude::*, ResponseExt};

// Arrange
let server = MockServer::start();

let m = server.mock(|when, then|{
    when.path("/hello");
    then.status(200)
        .body_from_file("tests/resources/simple_body.txt");
});

// Act
let mut response = isahc::get(server.url("/hello")).unwrap();

// Assert
m.assert();
assert_eq!(response.status(), 200);
assert_eq!(response.text().unwrap(), "ohi!");

Sets the JSON body for the HTTP response that will be returned by the mock server.

The provided JSON object needs to be both, a deserializable and serializable serde object.

Note that this method does not set the “content-type” header automatically, so you need to provide one yourself!

  • body - The HTTP response body the mock server will return in the form of a serde_json::Value object.

Example

You can use this method conveniently as follows:

use httpmock::prelude::*;
use serde_json::{Value, json};
use isahc::ResponseExt;
use isahc::prelude::*;

// Arrange
let _ = env_logger::try_init();
let server = MockServer::start();

let m = server.mock(|when, then|{
    when.path("/user");
    then.status(200)
        .header("content-type", "application/json")
        .json_body(json!({ "name": "Hans" }));
});

// Act
let mut response = isahc::get(server.url("/user")).unwrap();

let user: Value =
    serde_json::from_str(&response.text().unwrap()).expect("cannot deserialize JSON");

// Assert
m.assert();
assert_eq!(response.status(), 200);
assert_eq!(user.as_object().unwrap().get("name").unwrap(), "Hans");

Sets the JSON body that will be returned by the mock server. This method expects a serializable serde object that will be serialized/deserialized to/from a JSON string.

Note that this method does not set the “content-type” header automatically, so you need to provide one yourself!

  • body - The HTTP body object that will be serialized to JSON using serde.
use httpmock::prelude::*;
use isahc::{prelude::*, ResponseExt};

// This is a temporary type that we will use for this example
#[derive(serde::Serialize, serde::Deserialize)]
struct TestUser {
    name: String,
}

// Arrange
let _ = env_logger::try_init();
let server = MockServer::start();

let m = server.mock(|when, then| {
    when.path("/user");
    then.status(200)
        .header("content-type", "application/json")
        .json_body_obj(&TestUser {
            name: String::from("Hans"),
        });
});

// Act
let mut response = isahc::get(server.url("/user")).unwrap();

let user: TestUser =
    serde_json::from_str(&response.text().unwrap()).unwrap();

// Assert
m.assert();
assert_eq!(response.status(), 200);
assert_eq!(user.name, "Hans");

Sets an HTTP header that the mock server will return.

  • name - The name of the header.
  • value - The value of the header.

Example

You can use this method conveniently as follows:

// Arrange
use httpmock::prelude::*;
use serde_json::Value;
use isahc::ResponseExt;

let _ = env_logger::try_init();
let server = MockServer::start();

let m = server.mock(|when, then|{
    when.path("/");
    then.status(200)
        .header("Expires", "Wed, 21 Oct 2050 07:28:00 GMT");
});

// Act
let mut response = isahc::get(server.url("/")).unwrap();

// Assert
m.assert();
assert_eq!(response.status(), 200);

Sets a duration that will delay the mock server response.

  • duration - The delay.
// Arrange
use std::time::{SystemTime, Duration};
use httpmock::prelude::*;

let _ = env_logger::try_init();
let start_time = SystemTime::now();
let three_seconds = Duration::from_secs(3);
let server = MockServer::start();

let mock = server.mock(|when, then| {
    when.path("/delay");
    then.status(200)
        .delay(three_seconds);
});

// Act
let response = isahc::get(server.url("/delay")).unwrap();

// Assert
mock.assert();
assert_eq!(start_time.elapsed().unwrap() > three_seconds, true);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more