deno/js/fetch_test.ts

173 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-08-30 17:49:24 +00:00
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
2018-09-05 06:29:02 +00:00
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
2018-08-30 17:49:24 +00:00
import * as deno from "deno";
testPerm({ net: true }, async function fetchJsonSuccess() {
const response = await fetch("http://localhost:4545/package.json");
const json = await response.json();
assertEqual(json.name, "deno");
});
2018-09-05 06:29:02 +00:00
test(async function fetchPerm() {
let err;
try {
await fetch("http://localhost:4545/package.json");
} catch (err_) {
err = err_;
}
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
2018-09-05 06:29:02 +00:00
});
2018-09-12 19:16:42 +00:00
testPerm({ net: true }, async function fetchHeaders() {
const response = await fetch("http://localhost:4545/package.json");
const headers = response.headers;
assertEqual(headers.get("Content-Type"), "application/json");
assert(headers.get("Server").startsWith("SimpleHTTP"));
});
2018-09-14 17:56:37 +00:00
testPerm({ net: true }, async function fetchBlob() {
const response = await fetch("http://localhost:4545/package.json");
const headers = response.headers;
const blob = await response.blob();
assertEqual(blob.type, headers.get("Content-Type"));
assertEqual(blob.size, Number(headers.get("Content-Length")));
});
2018-09-30 14:31:50 +00:00
testPerm({ net: true }, async function responseClone() {
const response = await fetch("http://localhost:4545/package.json");
const response1 = response.clone();
assert(response !== response1);
assertEqual(response.status, response1.status);
assertEqual(response.statusText, response1.statusText);
const ab = await response.arrayBuffer();
const ab1 = await response1.arrayBuffer();
for (let i = 0; i < ab.byteLength; i++) {
assertEqual(ab[i], ab1[i]);
}
});
testPerm({ net: true }, async function fetchEmptyInvalid() {
let err;
try {
await fetch("");
} catch (err_) {
err = err_;
}
assertEqual(err.kind, deno.ErrorKind.InvalidUri);
assertEqual(err.name, "InvalidUri");
});
2018-11-26 02:07:26 +00:00
// TODO(ry) The following tests work but are flaky. There's a race condition
// somewhere. Here is what one of these flaky failures looks like:
//
// test fetchPostBodyString_permW0N1E0R0
// assertEqual failed. actual = expected = POST /blah HTTP/1.1
// hello: World
// foo: Bar
// host: 127.0.0.1:4502
// content-length: 11
// hello world
// Error: actual: expected: POST /blah HTTP/1.1
// hello: World
// foo: Bar
// host: 127.0.0.1:4502
// content-length: 11
// hello world
// at Object.assertEqual (file:///C:/deno/js/testing/util.ts:29:11)
// at fetchPostBodyString (file
/*
function bufferServer(addr: string): deno.Buffer {
const listener = deno.listen("tcp", addr);
const buf = new deno.Buffer();
listener.accept().then(async conn => {
const p1 = buf.readFrom(conn);
const p2 = conn.write(
new TextEncoder().encode(
"HTTP/1.0 404 Not Found\r\nContent-Length: 2\r\n\r\nNF"
)
);
// Wait for both an EOF on the read side of the socket and for the write to
// complete before closing it. Due to keep-alive, the EOF won't be sent
// until the Connection close (HTTP/1.0) response, so readFrom() can't
// proceed write. Conversely, if readFrom() is async, waiting for the
// write() to complete is not a guarantee that we've read the incoming
// request.
await Promise.all([p1, p2]);
conn.close();
listener.close();
});
return buf;
}
testPerm({ net: true }, async function fetchRequest() {
const addr = "127.0.0.1:4501";
const buf = bufferServer(addr);
const response = await fetch(`http://${addr}/blah`, {
method: "POST",
headers: [["Hello", "World"], ["Foo", "Bar"]]
});
assertEqual(response.status, 404);
assertEqual(response.headers.get("Content-Length"), "2");
const actual = new TextDecoder().decode(buf.bytes());
const expected = [
"POST /blah HTTP/1.1\r\n",
"hello: World\r\n",
"foo: Bar\r\n",
`host: ${addr}\r\n\r\n`
].join("");
assertEqual(actual, expected);
});
testPerm({ net: true }, async function fetchPostBodyString() {
const addr = "127.0.0.1:4502";
const buf = bufferServer(addr);
const body = "hello world";
const response = await fetch(`http://${addr}/blah`, {
method: "POST",
headers: [["Hello", "World"], ["Foo", "Bar"]],
body
});
assertEqual(response.status, 404);
assertEqual(response.headers.get("Content-Length"), "2");
const actual = new TextDecoder().decode(buf.bytes());
const expected = [
"POST /blah HTTP/1.1\r\n",
"hello: World\r\n",
"foo: Bar\r\n",
`host: ${addr}\r\n`,
`content-length: ${body.length}\r\n\r\n`,
body
].join("");
assertEqual(actual, expected);
});
testPerm({ net: true }, async function fetchPostBodyTypedArray() {
const addr = "127.0.0.1:4503";
const buf = bufferServer(addr);
const bodyStr = "hello world";
const body = new TextEncoder().encode(bodyStr);
const response = await fetch(`http://${addr}/blah`, {
method: "POST",
headers: [["Hello", "World"], ["Foo", "Bar"]],
body
});
assertEqual(response.status, 404);
assertEqual(response.headers.get("Content-Length"), "2");
const actual = new TextDecoder().decode(buf.bytes());
const expected = [
"POST /blah HTTP/1.1\r\n",
"hello: World\r\n",
"foo: Bar\r\n",
`host: ${addr}\r\n`,
`content-length: ${body.byteLength}\r\n\r\n`,
bodyStr
].join("");
assertEqual(actual, expected);
});
2018-11-26 02:07:26 +00:00
*/