Skip to content

Commit

Permalink
test response body filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
serverhiccups committed Jan 16, 2020
1 parent 4f2e74f commit 3cef47a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
45 changes: 40 additions & 5 deletions cli/js/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,33 +339,68 @@ export class Response implements domTypes.Response {
}

async arrayBuffer(): Promise<ArrayBuffer> {
if (this.body === null)
if (
this.type == "error" ||
this.type == "opaque" ||
this.type == "opaqueredirect" ||
this.body == null ||
this.body == undefined
) {
return Promise.reject(new Error("Response body is null"));
}
return this.body.arrayBuffer();
}

async blob(): Promise<domTypes.Blob> {
if (this.body === null)
if (
this.type == "error" ||
this.type == "opaque" ||
this.type == "opaqueredirect" ||
this.body == null ||
this.body == undefined
) {
return Promise.reject(new Error("Response body is null"));
}
return this.body.blob();
}

async formData(): Promise<domTypes.FormData> {
if (this.body === null)
if (
this.type == "error" ||
this.type == "opaque" ||
this.type == "opaqueredirect" ||
this.body == null ||
this.body == undefined
) {
return Promise.reject(new Error("Response body is null"));
}
return this.body.formData();
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async json(): Promise<any> {
if (this.body === null)
if (
this.type == "error" ||
this.type == "opaque" ||
this.type == "opaqueredirect" ||
this.body == null ||
this.body == undefined
) {
return Promise.reject(new Error("Response body is null"));
}
return this.body.json();
}

async text(): Promise<string> {
if (this.body === null)
if (
this.type == "error" ||
this.type == "opaque" ||
this.type == "opaqueredirect" ||
this.body == null ||
this.body == undefined
) {
return Promise.reject(new Error("Response body is null"));
}
return this.body.text();
}

Expand Down
21 changes: 20 additions & 1 deletion cli/js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
assert,
assertEquals,
assertStrContains,
assertThrows
assertThrows,
fail
} from "./test_util.ts";

testPerm({ net: true }, async function fetchConnectionError(): Promise<void> {
Expand Down Expand Up @@ -360,3 +361,21 @@ testPerm({ net: true }, async function fetchPostBodyTypedArray():Promise<void> {
assertEquals(actual, expected);
});
*/

testPerm({ net: true }, async function fetchWithRedirection(): Promise<void> {
const response = await fetch("http://localhost:4546/", {
redirect: "manual"
}); // will redirect to http://localhost:4545/
assertEquals(response.status, 0);
assertEquals(response.statusText, "");
assertEquals(response.url, "");
assertEquals(response.type, "opaqueredirect");
try {
await response.text();
fail(
"Reponse.text() on a filtered response without a body (opaqueredirect)"
);
} catch (e) {
return;
}
});
3 changes: 2 additions & 1 deletion cli/js/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export {
assertNotEquals,
assertStrictEq,
assertStrContains,
unreachable
unreachable,
fail
} from "../../std/testing/asserts.ts";

interface TestPermissions {
Expand Down

0 comments on commit 3cef47a

Please sign in to comment.