#testing-http #http-request #testing #testing-utilities

httpc-test

极简HTTP客户端测试工具

14个版本

0.1.10 2024年8月7日
0.1.9 2024年1月24日
0.1.8 2023年11月20日
0.1.5 2023年7月14日
0.1.1 2023年3月19日

#59HTTP客户端

Download history 803/week @ 2024-05-03 813/week @ 2024-05-10 876/week @ 2024-05-17 791/week @ 2024-05-24 873/week @ 2024-05-31 656/week @ 2024-06-07 769/week @ 2024-06-14 872/week @ 2024-06-21 741/week @ 2024-06-28 509/week @ 2024-07-05 722/week @ 2024-07-12 802/week @ 2024-07-19 1210/week @ 2024-07-26 1063/week @ 2024-08-02 950/week @ 2024-08-09 812/week @ 2024-08-16

每月4,132次下载
greenhouse_core 中使用

MIT/Apache

26KB
578

极简HTTP客户端测试工具。

  • 基于 reqwest 构建。
  • 针对测试便利性进行优化,而非性能。
  • 不要用于生产/应用程序代码,仅用于测试。
  • 对于生产代码(应用程序、服务等),请使用底层 reqwest 库及其工具。

感谢

示例

use anyhow::Result;
use serde_json::{json, Value};

#[tokio::test]
async fn test_simple_base() -> httpc_test::Result<()> {
	// Create a new httpc test client with a base URL (will be prefixed for all calls)
	// The client will have a cookie_store.
	let hc = httpc_test::new_client("http://localhost:8080")?;


	//// do_get, do_post, do_put, do_patch, do_delete return a httpc_test::Response

	// Simple do_get
	let res = hc.do_get("/hello").await?; // httpc_test::Response 
	let status = res.status();
	// Pretty print the result (status, headers, response cookies, client cookies, body)
	res.print().await?;

	let auth_token = res.res_cookie_value("auth-token"); // Option<String>
	let content_type = res.header("content_type"); // Option<&String>

	// Another do_get
	let res = hc.do_get("/context.rs").await?;
	// Pretty print but do not print the body 
	res.print_no_body().await?;


	//// get, post, put, patch, delete return a DeserializeOwned

	// a get (return a Deserialized)
	let json_value = hc.get::<Value>("/api/tickets").await?;

	// Another post (with the cookie store updated from the login request above )
	let res = hc
		.do_post(
			"/api/tickets",
			json!({
				"subject": "ticket 01"
			}),
		)
		.await?;
	res.print().await?;


	// Post with text content and specific content type
	let res = hc
		.do_post(
			"/api/tickets",
			(r#"{
				"subject": "ticket bb"
			}
			"#, 
			"application/json"),
		)
		.await?;
	res.print().await?;

	// Same woth do_patch, do_put.


	Ok(())
}



这个GitHub仓库

依赖项

~7–22MB
~276K SLoC