Crates.io | httpc-test |
lib.rs | httpc-test |
version | 0.1.10 |
source | src |
created_at | 2023-02-01 02:40:33.158581 |
updated_at | 2024-08-07 01:05:37.101316 |
description | Minimalistic HTTP Client Test Utilities |
homepage | https://github.com/jeremychone/rust-httpc-test |
repository | https://github.com/jeremychone/rust-httpc-test |
max_upload_size | |
id | 773197 |
size | 80,599 |
Minimalistic HTTP Client Test Utilities.
Thanks to @joeftiger for dependencies update (#23)
Thanks to @Manubi for the colored_json
update.
Thanks to @JamesGuthrie for
Thanks to @cyril-marpaud for the PR #9 - feat: provide Response's StatusCode.
Thanks to @eboody for the PR #7 - Add colors to output (enable with features = ["color-output"]
)
Thanks to @defic for the type client get/post/put/patch/delete
and the response body...
APIs.
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(())
}