4个版本

使用旧的Rust 2015

0.1.3 2018年8月6日
0.1.2 2018年8月5日
0.1.1 2018年8月5日
0.1.0 2018年8月5日

#766 in HTTP服务器

MIT/Apache

18KB
354

Gabira

Build Status Latest version License

一个用于测试HTTP服务器的Rust库。它专注于人体工程学和简洁性。

文档

使用方法

将以下依赖项添加到您的Cargo.toml中

[dev-dependencies]
gabira = "0.1"

并在测试中导入它

extern crate gabira;

use gabira::*;

示例

get("https://127.0.0.1:3000/ping")
  .expect_status(200) // <- Assert http status code
  .expect_body("pong") // <- Assert response body
  .end(); // <- Consume the test. Compile-time warnings are issued if forgotten.

以下是一个使用actix-web的TestServer的示例

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

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

期望可以链式调用

let invoice: InvoiceDto = post(&url)
  .set_header("Authorization", &token)
  .set_header("Accept", "application/json")
  .send_json(CreateInvoiceDto { ... })
  .expect_status(200)
  .expect_header("Access-Control-Allow-Origin", "*")
  .expect_header("Access-Control-Allow-Credentials", "true")
  .expect(|r| println!("{}", r.headers().len()))
  .expect(|r| assert!(r.headers().len() > 2))
  .end_json_with(|r: &InvoiceDto| {
    assert!(r.total > 1000);
  });

process_invoice(&invoice);

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: Into<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

功能

  • 请求是同步的
  • 期望(例如 expect_statusexpect_json)按照定义的顺序执行

限制

目前断言的范围有限。请参阅文档

许可证

许可协议为以下之一

任选其一。

贡献

除非您明确表示,否则您根据Apache-2.0许可证定义的任何有意提交以包含在作品中的贡献,应按上述方式双重许可,不附加任何额外条款或条件。

依赖项

~27MB
~499K SLoC