move lib, server and client to separate packages
This commit is contained in:
13
zoidberg_client/Cargo.toml
Normal file
13
zoidberg_client/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "zoidberg_client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies.zoidberg_lib]
|
||||
path = "../zoidberg_lib"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||
66
zoidberg_client/src/main.rs
Normal file
66
zoidberg_client/src/main.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use core::time::Duration;
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::header;
|
||||
|
||||
use zoidberg_lib::types::Update;
|
||||
|
||||
fn build_client(secret: &str) -> Client {
|
||||
let cookie = format!("secret={}", secret);
|
||||
|
||||
let mut headers = header::HeaderMap::new();
|
||||
headers.insert(
|
||||
"cookie",
|
||||
header::HeaderValue::from_str(&cookie)
|
||||
.unwrap_or_else(|_| panic!("invalid header value {}", &cookie)),
|
||||
);
|
||||
|
||||
Client::builder()
|
||||
.default_headers(headers)
|
||||
.timeout(Duration::from_secs(15))
|
||||
.build()
|
||||
.expect("Could not create HTTP client")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// test get request to index
|
||||
let res = build_client("some_secret")
|
||||
.get("http://localhost:8080/")
|
||||
.send()
|
||||
.expect("Could not send get request");
|
||||
|
||||
println!("Status: {}", res.status());
|
||||
println!("Headers:\n{:#?}", res.headers());
|
||||
|
||||
let body = res.text().unwrap();
|
||||
println!("Body:\n{}", body);
|
||||
|
||||
(0..10).for_each(|_| {
|
||||
// test get request to /register
|
||||
let res = build_client("some_secret")
|
||||
.get("http://localhost:8080/register")
|
||||
.send()
|
||||
.expect("Could not send get request");
|
||||
|
||||
println!("Status: {}", res.status());
|
||||
println!("Headers:\n{:#?}", res.headers());
|
||||
let body = res.text().unwrap();
|
||||
println!("Body:\n{}", body);
|
||||
});
|
||||
|
||||
// test post request to /update
|
||||
let update = Update {
|
||||
id: 99,
|
||||
status: "hi".to_string(),
|
||||
};
|
||||
|
||||
let res = build_client("some_secret")
|
||||
.post("http://localhost:8080/update")
|
||||
.json(&update)
|
||||
.send()
|
||||
.expect("Could not send get request");
|
||||
|
||||
println!("Status: {}", res.status());
|
||||
println!("Headers:\n{:#?}", res.headers());
|
||||
let body = res.text().unwrap();
|
||||
println!("Body:\n{}", body);
|
||||
}
|
||||
Reference in New Issue
Block a user