Skip to content

Commit

Permalink
fix: Request options as undefined or null result in cast error to object
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisgcm committed Dec 16, 2024
1 parent c11a8b5 commit 1d0bd8c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions modules/llrt_http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'js> Trace<'js> for Request<'js> {
#[rquickjs::methods(rename_all = "camelCase")]
impl<'js> Request<'js> {
#[qjs(constructor)]
pub fn new(ctx: Ctx<'js>, input: Value<'js>, options: Opt<Object<'js>>) -> Result<Self> {
pub fn new(ctx: Ctx<'js>, input: Value<'js>, options: Opt<Value<'js>>) -> Result<Self> {
let mut request = Self {
url: String::from(""),
method: "GET".to_string(),
Expand All @@ -70,7 +70,9 @@ impl<'js> Request<'js> {
})?;
}
if let Some(options) = options.0 {
assign_request(&mut request, ctx.clone(), &options)?;
if let Some(obj) = options.as_object() {
assign_request(&mut request, ctx.clone(), obj)?;
}
}
if request.headers.is_none() {
let headers = Class::instance(ctx, Headers::default())?;
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ describe("Request class", () => {
expect((await request.blob()).size).toEqual(blob.size);
expect((await request.blob()).type).toEqual("text/plain");
});

it("should ignore request options which are not an object", async () => {
const request = new Request("http://localhost", undefined);
expect(request instanceof Request).toBeTruthy();
});
});

describe("Response class", () => {
Expand Down

0 comments on commit 1d0bd8c

Please sign in to comment.