fermyon / spin-test

A utility for testing Spin applications
Apache License 2.0
17 stars 5 forks source link

Can't set the body on a request #106

Closed benbrandt closed 2 months ago

benbrandt commented 2 months ago

Hi, I was attempting to write a test that not only made a request, but also sent a body. Am I doing something wrong here? I keep getting an empty body in my request:

use spin_test_sdk::{
    bindings::{
        fermyon::spin_test::http_helper::OutgoingRequest,
        wasi::http::types::{Headers, Method},
    },
    spin_test,
};

#[spin_test]
fn make_request() {
    let headers =
        Headers::from_list(&[("content-type".to_owned(), "text-plain".as_bytes().to_vec())])
            .unwrap();
    let request = OutgoingRequest::new(headers);
    request.set_method(&Method::Post).unwrap();
    let body = request.body().unwrap();
    body.write_bytes("hello".as_bytes());

    let response = spin_test_sdk::perform_request(request);

    assert_eq!(response.status(), 200);
    let body = String::from_utf8(response.body().unwrap()).unwrap();
    assert_eq!(body, "hello");
}

The handler just reads and echos the body for now, and I can confirm it works via spin up and a curl call. But it fails to set the body in the test.

rylev commented 2 months ago

Thanks for reporting the issue! We'll get the fix merged ASAP and a new release built.

benbrandt commented 2 months ago

@rylev I built this locally and can confirm this solved the issue! Thanks for the quick turnaround!