[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Fix #1

Merged
merged 19 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add async stream fetch test 4
  • Loading branch information
nicoburniske committed May 20, 2024
commit ef48a6ed304e9854d72dba14c036c4c85ba74b5a
2 changes: 1 addition & 1 deletion tests/e2e/fetch-3/expect_serve_body.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"method":"POST","protocol":"https","host":"echo.free.beeceptor.com","path":"/","ip":"4.1.157.141:65103","headers":{"Host":"echo.free.beeceptor.com","Transfer-Encoding":"chunked","Content-Type":"application/json","Accept-Encoding":"gzip"},"parsedQueryParams":{},"parsedBody":{"hello":"world"}}
{"method":"POST","parsedBody":{"hello":"world"}}
11 changes: 6 additions & 5 deletions tests/e2e/fetch-3/fetch-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ async function main(event) {
});
event.respondWith(responsePromise);

console.log("fetching")

let response = await fetch("https://echo.free.beeceptor.com", {
method: "POST",
headers: {
Expand All @@ -18,12 +16,15 @@ async function main(event) {
body: JSON.stringify({hello: "world"})
})

console.log("fetch executed")

let responseJson = await response.json();

let result = {
method: responseJson.method,
parsedBody: responseJson.parsedBody,
};

console.log("Successfully received response json body");
resolve(new Response(JSON.stringify(responseJson)));
resolve(new Response(JSON.stringify(result)));
} catch (e) {
console.log(`Error: ${e}. Stack: ${e.stack}`);
}
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/fetch-4/expect_serve_body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"method":"POST","parsedBody":{"hello":"world"}}
Empty file.
1 change: 1 addition & 0 deletions tests/e2e/fetch-4/expect_serve_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stdout [0] :: Log: Successfully received response json body
62 changes: 62 additions & 0 deletions tests/e2e/fetch-4/fetch-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
async function main(event) {
let resolve, reject;

// Function to create an async stream
function createAsyncStream() {
const encoder = new TextEncoder();
const readableStream = new ReadableStream({
start(controller) {
const chunk = { hello: "world" };
const encodedChunk = encoder.encode(JSON.stringify(chunk));

// Simulate an async operation
setTimeout(() => {
controller.enqueue(encodedChunk);
controller.close();
}, 1000); // 1 second delay
}
});
return readableStream;
}

try {
let responsePromise = new Promise(async (res, rej) => {
resolve = res;
reject = rej;
});
event.respondWith(responsePromise);

console.log("Sending fetch request with async stream");

// Create the async stream
const asyncStream = createAsyncStream();

// Perform the fetch request with the async stream as the body
let response = await fetch("https://echo.free.beeceptor.com", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: asyncStream
});

console.log("Fetch request success");

let responseJson = await response.json();

console.log("Response JSON:", responseJson);

let result = {
method: responseJson.method,
parsedBody: responseJson.parsedBody,
};

console.log("Successfully received response json body:", result);

resolve(new Response(JSON.stringify(result)));
} catch (e) {
console.log(`Error: ${e}. Stack: ${e.stack}`);
}
}

addEventListener('fetch', main);
1 change: 1 addition & 0 deletions tests/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test_e2e(smoke)
test_e2e(fetch-1)
test_e2e(fetch-2)
test_e2e(fetch-3)
test_e2e(fetch-4)
test_e2e(tla)
test_e2e(syntax-err)
test_e2e(tla-err)
Expand Down