Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

tests: verify the response body to make sure the library is sending the http request correctly #12

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions crates/test-programs/src/bin/get_with_json_response.rs

This file was deleted.

10 changes: 10 additions & 0 deletions crates/test-programs/src/bin/get_with_query.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use serde::Deserialize;
use std::collections::HashMap;
use wasi_http_client::Client;

#[derive(Deserialize)]
struct Data {
args: HashMap<String, String>,
}

fn main() {
let resp = Client::new()
.get("https://httpbin.org/get?a=b")
.headers([("Content-Type", "application/json"), ("Accept", "*/*")])
.send()
.unwrap();
assert_eq!(resp.status(), 200);

let data = resp.json::<Data>().unwrap();
assert_eq!(data.args.get("a").unwrap(), "b");
}
11 changes: 11 additions & 0 deletions crates/test-programs/src/bin/post_with_form_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use serde::Deserialize;
use std::collections::HashMap;
use std::time::Duration;
use wasi_http_client::Client;

#[derive(Deserialize)]
struct Data {
form: HashMap<String, String>,
}

fn main() {
let resp = Client::new()
.post("https://httpbin.org/post")
Expand All @@ -9,4 +16,8 @@ fn main() {
.send()
.unwrap();
assert_eq!(resp.status(), 200);

let data = resp.json::<Data>().unwrap();
assert_eq!(data.form.get("a").unwrap(), "b");
assert_eq!(data.form.get("c").unwrap(), "");
}
9 changes: 9 additions & 0 deletions crates/test-programs/src/bin/post_with_json_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use serde::Deserialize;
use std::collections::HashMap;
use std::time::Duration;
use wasi_http_client::Client;

#[derive(Deserialize)]
struct Data {
json: HashMap<String, String>,
}

fn main() {
let resp = Client::new()
.post("https://httpbin.org/post")
Expand All @@ -10,4 +16,7 @@ fn main() {
.send()
.unwrap();
assert_eq!(resp.status(), 200);

let data = resp.json::<Data>().unwrap();
assert_eq!(data.json.get("data").unwrap(), "hello");
}
12 changes: 12 additions & 0 deletions crates/test-programs/src/bin/post_with_multipart_form_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use serde::Deserialize;
use std::collections::HashMap;
use std::time::Duration;
use wasi_http_client::Client;

#[derive(Deserialize)]
struct Data {
form: HashMap<String, String>,
files: HashMap<String, String>,
}

fn main() {
let resp = Client::new()
.post("https://httpbin.org/post")
Expand All @@ -22,4 +30,8 @@ hello
.send()
.unwrap();
assert_eq!(resp.status(), 200);

let data = resp.json::<Data>().unwrap();
assert_eq!(data.form.get("field1").unwrap(), "value1");
assert_eq!(data.files.get("field2").unwrap(), "hello");
}
10 changes: 0 additions & 10 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ fn get_chunk() {
assert!(status.success());
}

#[test]
fn get_with_json_response() {
let mut cmd = wasmtime();
let status = cmd
.arg(test_programs_artifacts::GET_WITH_JSON_RESPONSE_COMPONENT)
.status()
.unwrap();
assert!(status.success());
}

#[test]
fn get_with_query() {
let mut cmd = wasmtime();
Expand Down