14 releases

0.1.10 Aug 7, 2024
0.1.9 Jan 24, 2024
0.1.8 Nov 20, 2023
0.1.5 Jul 14, 2023
0.1.1 Mar 19, 2023

#55 in HTTP client

Download history 905/week @ 2024-07-23 1139/week @ 2024-07-30 1052/week @ 2024-08-06 965/week @ 2024-08-13 858/week @ 2024-08-20 866/week @ 2024-08-27 856/week @ 2024-09-03 754/week @ 2024-09-10 833/week @ 2024-09-17 973/week @ 2024-09-24 782/week @ 2024-10-01 541/week @ 2024-10-08 1020/week @ 2024-10-15 733/week @ 2024-10-22 909/week @ 2024-10-29 681/week @ 2024-11-05

3,486 downloads per month
Used in greenhouse_core

MIT/Apache

26KB
578 lines

Minimalistic HTTP Client Test Utilities.

  • Built on top of reqwest.
  • Optimized for testing convenience, not for performance.
  • Do not use for production/application code, just for testing.
  • For production code (apps, services, ...) use the underlying reqwest library and its utilities.

Thanks

Example

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(())
}



This GitHub repo

Dependencies

~8–19MB
~256K SLoC