aboutsummaryrefslogtreecommitdiff
path: root/test/js/deno/fetch/response.test.ts
blob: a8066c75b4e0596771dca122ac54a34c43407597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// GENERATED - DO NOT EDIT
// Copyright 2018+ the Deno authors. All rights reserved. MIT license.
// https://raw.githubusercontent.com/denoland/deno/main/cli/tests/unit/response_test.ts
import { createDenoTest } from "deno:harness";
const { test, assert, assertEquals, assertStringIncludes, assertThrows } = createDenoTest(import.meta.path);
test(async function responseText() {
    const response = new Response("hello world");
    const textPromise = response.text();
    assert(textPromise instanceof Promise);
    const text = await textPromise;
    assert(typeof text === "string");
    assertEquals(text, "hello world");
});
test(async function responseArrayBuffer() {
    const response = new Response(new Uint8Array([
        1,
        2,
        3
    ]));
    const arrayBufferPromise = response.arrayBuffer();
    assert(arrayBufferPromise instanceof Promise);
    const arrayBuffer = await arrayBufferPromise;
    assert(arrayBuffer instanceof ArrayBuffer);
    assertEquals(new Uint8Array(arrayBuffer), new Uint8Array([
        1,
        2,
        3
    ]));
});
test(async function responseJson() {
    const response = new Response('{"hello": "world"}');
    const jsonPromise = response.json();
    assert(jsonPromise instanceof Promise);
    const json = await jsonPromise;
    assert(json instanceof Object);
    assertEquals(json, {
        hello: "world"
    });
});
test(async function responseBlob() {
    const response = new Response(new Uint8Array([
        1,
        2,
        3
    ]));
    const blobPromise = response.blob();
    assert(blobPromise instanceof Promise);
    const blob = await blobPromise;
    assert(blob instanceof Blob);
    assertEquals(blob.size, 3);
    assertEquals(await blob.arrayBuffer(), new Uint8Array([
        1,
        2,
        3
    ]).buffer);
});
test(async function responseFormData() {
    const input = new FormData();
    input.append("hello", "world");
    const response = new Response(input);
    const contentType = response.headers.get("content-type")!;
    assert(contentType.startsWith("multipart/form-data"));
    const formDataPromise = response.formData();
    assert(formDataPromise instanceof Promise);
    const formData = await formDataPromise;
    assert(formData instanceof FormData);
    assertEquals([
        ...formData
    ], [
        ...input
    ]);
});
test(function responseInvalidInit() {
    assertThrows(()=>new Response("", 0));
    assertThrows(()=>new Response("", {
            status: 0
        }));
    assertThrows(()=>new Response("", {
            status: null
        }));
});
test(function responseNullInit() {
    const response = new Response("", null);
    assertEquals(response.status, 200);
});
test.ignore(function customInspectFunction() {
    const response = new Response();
    assertEquals(Deno.inspect(response), `Response {
  body: null,
  bodyUsed: false,
  headers: Headers {},
  ok: true,
  redirected: false,
  status: 200,
  statusText: "",
  url: ""
}`);
    assertStringIncludes(Deno.inspect(Response.prototype), "Response");
});
test(async function responseBodyUsed() {
    const response = new Response("body");
    assert(!response.bodyUsed);
    await response.text();
    assert(response.bodyUsed);
    response.body;
    assert(response.bodyUsed);
});