gabira 0.1.1

HTTP integration tests made easy.
Documentation

Gabira

Latest version License

A rust library for testing HTTP servers. It focuses on ergonomics and brevity.

Documentation:

Usage

Add the following dependency to your Cargo.toml:

[dev-dependencies]
gabira = "0.1"

and import it in your tests:

extern crate gabira;

use gabira::*;

Example

let expect = Body::from("pong");
get("http://localhost:3000/ping")
  .expect_status(200) // <- Assert http status code
  .expect_body(&expect) // <- Assert response body
  .end(); // <- Consume the test. Compile-time warnings are issued if forgotten.

Here's an example with actix-web's TestServer:

let srv = TestServer::with_factory(|| {
  App::new().resource("/login", |r| r.method(Method::POST).with(login))
});

// Response body should match this
let expect = TokenDto {
  token: "...",
};

// POST request with json body
post(&srv.url("/login"))
  .send_json(LoginDto {
    username: "...",
    password: "...",
  }).expect_status(200)
  .expect_json(&expect)
  .end();

API

get(path: &str)
post(path: &str)
put(path: &str)
delete(path: &str)
  .set_cookie(name: &str, value: &str)
  .set_header(field: &str, value: &str)
  .send(body: Into<Body>)
  .send_json(json: Serialize)
  .send_form(json: Serialize)
  .expect_status(status: u16)
  .expect_cookie(name: &str, value: &str)
  .expect_header(field: &str, value: &str)
  .expect_json(json: &Serialize)
  .expect_form(form: &Serialize)
  .expect_body(body: &Body)
  .expect(f: FnMut(&ClientResponse))
  .end() -> ClientResponse
  .end_with(f: FnMut(&ClientResponse)) -> ClientResponse
  .end_json<T: DeserializeOwned>() -> T
  .end_json_with<T: DeserializeOwned>(f: FnMut(&T)) -> T

Functionality

  • Requests are synchronous
  • Expectations (e.g. expect_status, expect_json) are run in the order of definition

Limitations

Range of assertions are limited for the moment. See the documentation.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.