#test-server #testing-http #http-request #mocking #http #testing

dev http-test-server

Programatically create resources and pre-defined responses for tests

6 stable releases

Uses old Rust 2015

2.1.1 Apr 27, 2023
2.1.0 May 28, 2020
2.0.1 Apr 14, 2020
2.0.0 Mar 20, 2020
1.0.0 Oct 13, 2018

#91 in HTTP server

Download history 423/week @ 2024-07-23 157/week @ 2024-07-30 136/week @ 2024-08-06 107/week @ 2024-08-13 104/week @ 2024-08-20 137/week @ 2024-08-27 123/week @ 2024-09-03 181/week @ 2024-09-10 162/week @ 2024-09-17 184/week @ 2024-09-24 129/week @ 2024-10-01 146/week @ 2024-10-08 90/week @ 2024-10-15 151/week @ 2024-10-22 148/week @ 2024-10-29 242/week @ 2024-11-05

655 downloads per month
Used in 8 crates

MIT/Apache

66KB
1K SLoC

HTTP Test Server

Documentation Build Status

Programatically create end-points that listen for connections and return pre-defined responses.

  • Allows multiple endpoints and simultaneous client connections
  • Streaming support
  • Helper functions to retrieve data such as request count, number of connected clients and requests metadata
  • Automatically allocates free port and close server after use

Examples:

Accept POST requests:

extern crate http_test_server;

use http_test_server::{TestServer, Resource};
use http_test_server::http::{Status, Method};

let server = TestServer::new().unwrap();
let resource = server.create_resource("/some-endpoint/new");

resource
    .status(Status::Created)
    .method(Method::POST)
    .header("Content-Type", "application/json")
    .header("Cache-Control", "no-cache")
    .body("{ \"message\": \"this is a message\" }");

// request: POST /some-endpoint/new

// HTTP/1.1 201 Created\r\n
// Content-Type: application/json\r\n
// Cache-Control: no-cache\r\n
// \r\n
// { "message": "this is a message" }

Use path and query parameters

extern crate http_test_server;

use http_test_server::{TestServer, Resource};
use http_test_server::http::{Status, Method};

let server = TestServer::new().unwrap();
let resource = server.create_resource("/user/{userId}?filter=*");

resource
    .status(Status::OK)
    .header("Content-Type", "application/json")
    .header("Cache-Control", "no-cache")
    .body(r#"{ "id": "{path.userId}", "filter": "{query.filter}" }"#);

// request: GET /user/abc123?filter=all

// HTTP/1.1 200 Ok\r\n
// Content-Type: application/json\r\n
// Cache-Control: no-cache\r\n
// \r\n
// { "id": "abc123", "filter": "all" }

Expose a persistent stream:

let server = TestServer::new().unwrap();
let resource = server.create_resource("/sub");

resource
    .header("Content-Type", "text/event-stream")
    .header("Cache-Control", "no-cache")
    .stream()
    .body(": initial data");

// ...

resource
    .send("some data")
    .send(" some extra data\n")
    .send_line("some extra data with line break")
    .close_open_connections();

// request: GET /sub

// HTTP/1.1 200 Ok\r\n
// Content-Type: text/event-stream\r\n
// Cache-Control: no-cache\r\n
// \r\n
// : initial data
// some data some extra data\n
// some extra data with line break\n

Redirects:

let server = TestServer::new().unwrap();
let resource_redirect = server.create_resource("/original");
let resource_target = server.create_resource("/new");

resource_redirect
    .status(Status::SeeOther)
    .header("Location", "/new" );

resource_target.body("Hi!");

// request: GET /original

// HTTP/1.1 303 See Other\r\n
// Location: /new\r\n
// \r\n

Regex URI:

let server = TestServer::new().unwrap();
let resource = server.create_resource("/hello/[0-9]/[A-z]/.*");

// request: GET /hello/8/b/doesntmatter-hehe

// HTTP/1.1 200 Ok\r\n
// \r\n

Check /tests/integration_test.rs for more usage examples.


NOTE: This is not intended to work as a full featured server. For this reason, many validations and behaviours are not implemented. e.g: A request with Accept header with not supported Content-Type won't trigger a 406 Not Acceptable.

As this crate was devised to be used in tests, smart behaviours could be confusing and misleading. Having said that, for the sake of convenience, some default behaviours were implemented:

  • Server returns 404 Not Found when requested resource was not configured.
  • Server returns 405 Method Not Allowed when trying to reach resource with different method from those configured.
  • When a resource is created it responds to GET with 200 Ok by default.

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.

Dependencies

~2.2–3MB
~54K SLoC