mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-17 17:38:10 +00:00
Merge pull request #972 from lightpanda-io/fetch
Fetch + ReadableStream
This commit is contained in:
16
src/tests/fetch/fetch.html
Normal file
16
src/tests/fetch/fetch.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<script src="../testing.js"></script>
|
||||
<script id=fetch type=module>
|
||||
const promise1 = new Promise((resolve) => {
|
||||
fetch('http://127.0.0.1:9582/xhr/json')
|
||||
.then((res) => {
|
||||
return res.json()
|
||||
})
|
||||
.then((json) => {
|
||||
resolve(json);
|
||||
});
|
||||
});
|
||||
|
||||
testing.async(promise1, (json) => {
|
||||
testing.expectEqual({over: '9000!!!'}, json);
|
||||
});
|
||||
</script>
|
||||
102
src/tests/fetch/headers.html
Normal file
102
src/tests/fetch/headers.html
Normal file
@@ -0,0 +1,102 @@
|
||||
<script src="../testing.js"></script>
|
||||
|
||||
<script id=headers>
|
||||
let headers = new Headers({"Set-Cookie": "name=world"});
|
||||
testing.expectEqual("name=world", headers.get("set-cookie"));
|
||||
|
||||
let myHeaders = new Headers();
|
||||
myHeaders.append("Content-Type", "image/jpeg"),
|
||||
testing.expectEqual(false, myHeaders.has("Picture-Type"));
|
||||
testing.expectEqual("image/jpeg", myHeaders.get("Content-Type"));
|
||||
|
||||
myHeaders.append("Content-Type", "image/png");
|
||||
testing.expectEqual("image/jpeg, image/png", myHeaders.get("Content-Type"));
|
||||
|
||||
myHeaders.delete("Content-Type");
|
||||
testing.expectEqual(null, myHeaders.get("Content-Type"));
|
||||
|
||||
myHeaders.set("Picture-Type", "image/svg")
|
||||
testing.expectEqual("image/svg", myHeaders.get("Picture-Type"));
|
||||
testing.expectEqual(true, myHeaders.has("Picture-Type"))
|
||||
|
||||
const originalHeaders = new Headers([["Content-Type", "application/json"], ["Authorization", "Bearer token123"]]);
|
||||
testing.expectEqual("application/json", originalHeaders.get("Content-Type"));
|
||||
testing.expectEqual("Bearer token123", originalHeaders.get("Authorization"));
|
||||
|
||||
const newHeaders = new Headers(originalHeaders);
|
||||
testing.expectEqual("application/json", newHeaders.get("Content-Type"));
|
||||
testing.expectEqual("Bearer token123" ,newHeaders.get("Authorization"));
|
||||
testing.expectEqual(true ,newHeaders.has("Content-Type"));
|
||||
testing.expectEqual(true ,newHeaders.has("Authorization"));
|
||||
testing.expectEqual(false, newHeaders.has("X-Custom"));
|
||||
|
||||
newHeaders.set("X-Custom", "test-value");
|
||||
testing.expectEqual("test-value", newHeaders.get("X-Custom"));
|
||||
testing.expectEqual(null, originalHeaders.get("X-Custom"));
|
||||
testing.expectEqual(false, originalHeaders.has("X-Custom"));
|
||||
</script>
|
||||
|
||||
<script id=keys>
|
||||
const testKeyHeaders = new Headers();
|
||||
testKeyHeaders.set("Content-Type", "application/json");
|
||||
testKeyHeaders.set("Authorization", "Bearer token123");
|
||||
testKeyHeaders.set("X-Custom", "test-value");
|
||||
|
||||
const keys = [];
|
||||
for (const key of testKeyHeaders.keys()) {
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
testing.expectEqual(3, keys.length);
|
||||
testing.expectEqual(true, keys.includes("Content-Type"));
|
||||
testing.expectEqual(true, keys.includes("Authorization"));
|
||||
testing.expectEqual(true, keys.includes("X-Custom"));
|
||||
</script>
|
||||
|
||||
<script id=values>
|
||||
const testValuesHeaders = new Headers();
|
||||
testValuesHeaders.set("Content-Type", "application/json");
|
||||
testValuesHeaders.set("Authorization", "Bearer token123");
|
||||
testValuesHeaders.set("X-Custom", "test-value");
|
||||
|
||||
const values = [];
|
||||
for (const value of testValuesHeaders.values()) {
|
||||
values.push(value);
|
||||
}
|
||||
|
||||
testing.expectEqual(3, values.length);
|
||||
testing.expectEqual(true, values.includes("application/json"));
|
||||
testing.expectEqual(true, values.includes("Bearer token123"));
|
||||
testing.expectEqual(true, values.includes("test-value"));
|
||||
</script>
|
||||
|
||||
<script id=entries>
|
||||
const testEntriesHeaders = new Headers();
|
||||
testEntriesHeaders.set("Content-Type", "application/json");
|
||||
testEntriesHeaders.set("Authorization", "Bearer token123");
|
||||
testEntriesHeaders.set("X-Custom", "test-value");
|
||||
|
||||
const entries = [];
|
||||
for (const entry of testEntriesHeaders.entries()) {
|
||||
entries.push(entry);
|
||||
}
|
||||
|
||||
testing.expectEqual(3, entries.length);
|
||||
|
||||
const entryMap = new Map(entries);
|
||||
testing.expectEqual("application/json", entryMap.get("Content-Type"));
|
||||
testing.expectEqual("Bearer token123", entryMap.get("Authorization"));
|
||||
testing.expectEqual("test-value", entryMap.get("X-Custom"));
|
||||
|
||||
const entryKeys = Array.from(entryMap.keys());
|
||||
testing.expectEqual(3, entryKeys.length);
|
||||
testing.expectEqual(true, entryKeys.includes("Content-Type"));
|
||||
testing.expectEqual(true, entryKeys.includes("Authorization"));
|
||||
testing.expectEqual(true, entryKeys.includes("X-Custom"));
|
||||
|
||||
const entryValues = Array.from(entryMap.values());
|
||||
testing.expectEqual(3, entryValues.length);
|
||||
testing.expectEqual(true, entryValues.includes("application/json"));
|
||||
testing.expectEqual(true, entryValues.includes("Bearer token123"));
|
||||
testing.expectEqual(true, entryValues.includes("test-value"))
|
||||
</script>
|
||||
22
src/tests/fetch/request.html
Normal file
22
src/tests/fetch/request.html
Normal file
@@ -0,0 +1,22 @@
|
||||
<script src="../testing.js"></script>
|
||||
|
||||
<script id=request>
|
||||
let request = new Request("flower.png");
|
||||
testing.expectEqual("http://localhost:9582/src/tests/fetch/flower.png", request.url);
|
||||
testing.expectEqual("GET", request.method);
|
||||
|
||||
let request2 = new Request("https://google.com", {
|
||||
method: "POST",
|
||||
body: "Hello, World",
|
||||
cache: "reload",
|
||||
credentials: "omit",
|
||||
headers: { "Sender": "me", "Target": "you" }
|
||||
}
|
||||
);
|
||||
testing.expectEqual("https://google.com", request2.url);
|
||||
testing.expectEqual("POST", request2.method);
|
||||
testing.expectEqual("omit", request2.credentials);
|
||||
testing.expectEqual("reload", request2.cache);
|
||||
testing.expectEqual("me", request2.headers.get("SeNdEr"));
|
||||
testing.expectEqual("you", request2.headers.get("target"));
|
||||
</script>
|
||||
49
src/tests/fetch/response.html
Normal file
49
src/tests/fetch/response.html
Normal file
@@ -0,0 +1,49 @@
|
||||
<script src="../testing.js"></script>
|
||||
|
||||
<script id=response>
|
||||
let response = new Response("Hello, World!");
|
||||
testing.expectEqual(200, response.status);
|
||||
testing.expectEqual("", response.statusText);
|
||||
testing.expectEqual(true, response.ok);
|
||||
testing.expectEqual("", response.url);
|
||||
testing.expectEqual(false, response.redirected);
|
||||
|
||||
let response2 = new Response("Error occurred", {
|
||||
status: 404,
|
||||
statusText: "Not Found",
|
||||
headers: {
|
||||
"Content-Type": "text/plain",
|
||||
"X-Custom": "test-value",
|
||||
"Cache-Control": "no-cache"
|
||||
}
|
||||
});
|
||||
testing.expectEqual(404, response2.status);
|
||||
testing.expectEqual("Not Found", response2.statusText);
|
||||
testing.expectEqual(false, response2.ok);
|
||||
testing.expectEqual("text/plain", response2.headers.get("Content-Type"));
|
||||
testing.expectEqual("test-value", response2.headers.get("X-Custom"));
|
||||
testing.expectEqual("no-cache", response2.headers.get("cache-control"));
|
||||
|
||||
let response3 = new Response("Created", { status: 201, statusText: "Created" });
|
||||
testing.expectEqual(201, response3.status);
|
||||
testing.expectEqual("Created", response3.statusText);
|
||||
testing.expectEqual(true, response3.ok);
|
||||
|
||||
let nullResponse = new Response(null);
|
||||
testing.expectEqual(200, nullResponse.status);
|
||||
testing.expectEqual("", nullResponse.statusText);
|
||||
|
||||
let emptyResponse = new Response("");
|
||||
testing.expectEqual(200, emptyResponse.status);
|
||||
</script>
|
||||
|
||||
<script id=json type=module>
|
||||
const promise1 = new Promise((resolve) => {
|
||||
let response = new Response('[]');
|
||||
response.json().then(resolve)
|
||||
});
|
||||
|
||||
testing.async(promise1, (json) => {
|
||||
testing.expectEqual([], json);
|
||||
});
|
||||
</script>
|
||||
117
src/tests/streams/readable_stream.html
Normal file
117
src/tests/streams/readable_stream.html
Normal file
@@ -0,0 +1,117 @@
|
||||
<script src="../testing.js"></script>
|
||||
|
||||
<script id=readable_stream>
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue("hello");
|
||||
controller.enqueue("world");
|
||||
controller.close();
|
||||
}
|
||||
});
|
||||
|
||||
const reader = stream.getReader();
|
||||
|
||||
testing.async(reader.read(), (data) => {
|
||||
testing.expectEqual("hello", data.value);
|
||||
testing.expectEqual(false, data.done);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script id=readable_stream_close>
|
||||
var closeResult;
|
||||
|
||||
const stream1 = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue("first");
|
||||
controller.enqueue("second");
|
||||
controller.close();
|
||||
}
|
||||
});
|
||||
|
||||
const reader1 = stream1.getReader();
|
||||
|
||||
testing.async(reader1.read(), (data) => {
|
||||
testing.expectEqual("first", data.value);
|
||||
testing.expectEqual(false, data.done);
|
||||
});
|
||||
|
||||
testing.async(reader1.read(), (data) => {
|
||||
testing.expectEqual("second", data.value);
|
||||
testing.expectEqual(false, data.done);
|
||||
});
|
||||
|
||||
testing.async(reader1.read(), (data) => {
|
||||
testing.expectEqual(undefined, data.value);
|
||||
testing.expectEqual(true, data.done);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script id=readable_stream_cancel>
|
||||
var readResult;
|
||||
var cancelResult;
|
||||
var closeResult;
|
||||
|
||||
const stream2 = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue("data1");
|
||||
controller.enqueue("data2");
|
||||
controller.enqueue("data3");
|
||||
},
|
||||
cancel(reason) {
|
||||
closeResult = `Stream cancelled: ${reason}`;
|
||||
}
|
||||
});
|
||||
|
||||
const reader2 = stream2.getReader();
|
||||
|
||||
testing.async(reader2.read(), (data) => {
|
||||
testing.expectEqual("data1", data.value);
|
||||
testing.expectEqual(false, data.done);
|
||||
});
|
||||
|
||||
testing.async(reader2.cancel("user requested"), (result) => {
|
||||
testing.expectEqual(undefined, result);
|
||||
testing.expectEqual("Stream cancelled: user requested", closeResult);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script id=readable_stream_cancel_no_reason>
|
||||
var closeResult2;
|
||||
|
||||
const stream3 = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue("test");
|
||||
},
|
||||
cancel(reason) {
|
||||
closeResult2 = reason === undefined ? "no reason" : reason;
|
||||
}
|
||||
});
|
||||
|
||||
const reader3 = stream3.getReader();
|
||||
|
||||
testing.async(reader3.cancel(), (result) => {
|
||||
testing.expectEqual(undefined, result);
|
||||
testing.expectEqual("no reason", closeResult2);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script id=readable_stream_read_after_cancel>
|
||||
var readAfterCancelResult;
|
||||
|
||||
const stream4 = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue("before_cancel");
|
||||
},
|
||||
});
|
||||
|
||||
const reader4 = stream4.getReader();
|
||||
|
||||
testing.async(reader4.cancel("test cancel"), (cancelResult) => {
|
||||
testing.expectEqual(undefined, cancelResult);
|
||||
});
|
||||
|
||||
testing.async(reader4.read(), (data) => {
|
||||
testing.expectEqual(undefined, data.value);
|
||||
testing.expectEqual(true, data.done);
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user