2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, testPerm, assert, assertEquals } from "./test_util.ts";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchJsonSuccess(): Promise<void> {
|
2018-08-30 13:49:24 -04:00
|
|
|
const response = await fetch("http://localhost:4545/package.json");
|
|
|
|
const json = await response.json();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(json.name, "deno");
|
2018-08-30 13:49:24 -04:00
|
|
|
});
|
2018-09-05 02:29:02 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function fetchPerm(): Promise<void> {
|
2018-09-05 02:29:02 -04:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await fetch("http://localhost:4545/package.json");
|
|
|
|
} catch (err_) {
|
|
|
|
err = err_;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-05 02:29:02 -04:00
|
|
|
});
|
2018-09-12 15:16:42 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchHeaders(): Promise<void> {
|
2018-09-12 15:16:42 -04:00
|
|
|
const response = await fetch("http://localhost:4545/package.json");
|
|
|
|
const headers = response.headers;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(headers.get("Content-Type"), "application/json");
|
2018-09-12 15:16:42 -04:00
|
|
|
assert(headers.get("Server").startsWith("SimpleHTTP"));
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchBlob(): Promise<void> {
|
2018-09-14 13:56:37 -04:00
|
|
|
const response = await fetch("http://localhost:4545/package.json");
|
|
|
|
const headers = response.headers;
|
|
|
|
const blob = await response.blob();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(blob.type, headers.get("Content-Type"));
|
|
|
|
assertEquals(blob.size, Number(headers.get("Content-Length")));
|
2018-09-14 13:56:37 -04:00
|
|
|
});
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2019-06-22 10:22:27 -04:00
|
|
|
testPerm({ net: true }, async function fetchAsyncIterator(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4545/package.json");
|
|
|
|
const headers = response.headers;
|
|
|
|
let total = 0;
|
|
|
|
for await (const chunk of response.body) {
|
|
|
|
total += chunk.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(total, Number(headers.get("Content-Length")));
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function responseClone(): Promise<void> {
|
2018-10-21 17:42:18 -04:00
|
|
|
const response = await fetch("http://localhost:4545/package.json");
|
|
|
|
const response1 = response.clone();
|
|
|
|
assert(response !== response1);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(response.status, response1.status);
|
|
|
|
assertEquals(response.statusText, response1.statusText);
|
2018-10-21 17:42:18 -04:00
|
|
|
const ab = await response.arrayBuffer();
|
|
|
|
const ab1 = await response1.arrayBuffer();
|
|
|
|
for (let i = 0; i < ab.byteLength; i++) {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(ab[i], ab1[i]);
|
2018-10-21 17:42:18 -04:00
|
|
|
}
|
|
|
|
});
|
2018-11-14 20:36:34 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchEmptyInvalid(): Promise<void> {
|
2018-12-21 04:47:09 -05:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await fetch("");
|
|
|
|
} catch (err_) {
|
|
|
|
err = err_;
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.InvalidUri);
|
|
|
|
assertEquals(err.name, "InvalidUri");
|
2018-12-21 04:47:09 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchMultipartFormDataSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-12-21 17:09:53 -05:00
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/tests/subdir/multipart_form_data.txt"
|
|
|
|
);
|
|
|
|
const formData = await response.formData();
|
|
|
|
assert(formData.has("field_1"));
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("field_1").toString(), "value_1 \r\n");
|
2018-12-21 17:09:53 -05:00
|
|
|
assert(formData.has("field_2"));
|
2018-12-23 11:44:08 -05:00
|
|
|
/* TODO(ry) Re-enable this test once we bring back the global File type.
|
2018-12-21 17:09:53 -05:00
|
|
|
const file = formData.get("field_2") as File;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(file.name, "file.js");
|
2018-12-23 11:44:08 -05:00
|
|
|
*/
|
2018-12-21 17:09:53 -05:00
|
|
|
// Currently we cannot read from file...
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm(
|
|
|
|
{ net: true },
|
|
|
|
async function fetchURLEncodedFormDataSuccess(): Promise<void> {
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/tests/subdir/form_urlencoded.txt"
|
|
|
|
);
|
|
|
|
const formData = await response.formData();
|
|
|
|
assert(formData.has("field_1"));
|
|
|
|
assertEquals(formData.get("field_1").toString(), "Hi");
|
|
|
|
assert(formData.has("field_2"));
|
|
|
|
assertEquals(formData.get("field_2").toString(), "<Deno>");
|
|
|
|
}
|
|
|
|
);
|
2018-12-21 17:09:53 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchInitStringBody(): Promise<void> {
|
2019-01-03 06:41:20 -05:00
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: data
|
|
|
|
});
|
|
|
|
const text = await response.text();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(text, data);
|
2019-01-03 06:41:20 -05:00
|
|
|
assert(response.headers.get("content-type").startsWith("text/plain"));
|
|
|
|
});
|
|
|
|
|
2019-05-01 23:56:42 -04:00
|
|
|
testPerm({ net: true }, async function fetchRequestInitStringBody(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const req = new Request("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: data
|
|
|
|
});
|
|
|
|
const response = await fetch(req);
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchInitTypedArrayBody(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-01-03 06:41:20 -05:00
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: new TextEncoder().encode(data)
|
|
|
|
});
|
|
|
|
const text = await response.text();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(text, data);
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchInitURLSearchParamsBody(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-01-03 06:41:20 -05:00
|
|
|
const data = "param1=value1¶m2=value2";
|
|
|
|
const params = new URLSearchParams(data);
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: params
|
|
|
|
});
|
|
|
|
const text = await response.text();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(text, data);
|
2019-01-03 06:41:20 -05:00
|
|
|
assert(
|
|
|
|
response.headers
|
|
|
|
.get("content-type")
|
|
|
|
.startsWith("application/x-www-form-urlencoded")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchInitBlobBody(): Promise<void> {
|
2019-01-03 06:41:20 -05:00
|
|
|
const data = "const a = 1";
|
|
|
|
const blob = new Blob([data], {
|
|
|
|
type: "text/javascript"
|
|
|
|
});
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: blob
|
|
|
|
});
|
|
|
|
const text = await response.text();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(text, data);
|
2019-01-03 06:41:20 -05:00
|
|
|
assert(response.headers.get("content-type").startsWith("text/javascript"));
|
|
|
|
});
|
|
|
|
|
2019-01-17 16:58:06 -05: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
|
2019-03-06 20:48:46 -05:00
|
|
|
// assertEquals failed. actual = expected = POST /blah HTTP/1.1
|
2019-01-17 16:58:06 -05:00
|
|
|
// 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
|
2019-03-06 20:48:46 -05:00
|
|
|
// at Object.assertEquals (file:///C:/deno/js/testing/util.ts:29:11)
|
2019-01-17 16:58:06 -05:00
|
|
|
// at fetchPostBodyString (file
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
/*
|
2019-02-12 10:08:56 -05:00
|
|
|
function bufferServer(addr: string): Deno.Buffer {
|
|
|
|
const listener = Deno.listen("tcp", addr);
|
|
|
|
const buf = new Deno.Buffer();
|
2018-11-14 20:36:34 -05:00
|
|
|
listener.accept().then(async conn => {
|
2018-11-14 21:19:38 -05:00
|
|
|
const p1 = buf.readFrom(conn);
|
|
|
|
const p2 = conn.write(
|
2018-11-14 20:36:34 -05:00
|
|
|
new TextEncoder().encode(
|
|
|
|
"HTTP/1.0 404 Not Found\r\nContent-Length: 2\r\n\r\nNF"
|
|
|
|
)
|
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
// 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]);
|
2018-11-14 20:36:34 -05:00
|
|
|
conn.close();
|
2018-11-14 21:19:38 -05:00
|
|
|
listener.close();
|
2018-11-14 20:36:34 -05:00
|
|
|
});
|
2018-11-14 21:19:38 -05:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchRequest():Promise<void> {
|
2018-11-14 21:19:38 -05:00
|
|
|
const addr = "127.0.0.1:4501";
|
|
|
|
const buf = bufferServer(addr);
|
2018-11-14 20:36:34 -05:00
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [["Hello", "World"], ["Foo", "Bar"]]
|
|
|
|
});
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
2018-11-14 20:36:34 -05:00
|
|
|
|
|
|
|
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("");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(actual, expected);
|
2018-11-14 20:36:34 -05:00
|
|
|
});
|
2018-11-14 21:19:38 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchPostBodyString():Promise<void> {
|
2018-11-14 21:19:38 -05:00
|
|
|
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
|
|
|
|
});
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
2018-11-14 21:19:38 -05:00
|
|
|
|
|
|
|
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("");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(actual, expected);
|
2018-11-14 21:19:38 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ net: true }, async function fetchPostBodyTypedArray():Promise<void> {
|
2018-11-14 21:19:38 -05:00
|
|
|
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
|
|
|
|
});
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
2018-11-14 21:19:38 -05:00
|
|
|
|
|
|
|
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("");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(actual, expected);
|
2018-11-14 21:19:38 -05:00
|
|
|
});
|
2019-01-17 16:58:06 -05:00
|
|
|
*/
|