-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
agent_test.ts
109 lines (107 loc) · 2.86 KB
/
agent_test.ts
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
108
109
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { encode } from "./_util.ts";
import { createAgent } from "./agent.ts";
import { createApp } from "./app.ts";
import {
assertEquals,
assertThrows,
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { group } from "./_test_util.ts";
import { ServeListener } from "./server.ts";
function setupRouter(port: number): ServeListener {
const app = createApp();
app.route("/get", async (req) => {
return req.respond({
status: 200,
body: encode("ok"),
});
});
app.route("/post", async (req) => {
return req.respond({
status: 200,
headers: req.headers,
body: req.body,
});
});
return app.listen({ port });
}
group("agent", ({ test, setupAll }) => {
let port = 8700;
setupAll(() => {
const listener = setupRouter(port);
return () => listener.close();
});
test("basic", async () => {
const agent = createAgent(`http://127.0.0.1:${port}`);
try {
{
const res = await agent.send({
path: "/get",
method: "GET",
});
assertEquals(res.status, 200);
assertEquals(await res.text(), "ok");
}
{
const res = await agent.send({
path: "/post",
method: "POST",
body: encode("denoland"),
});
assertEquals(res.status, 200);
assertEquals(await res.text(), "denoland");
}
} finally {
agent.conn.close();
}
});
test("agentTls", async () => {
const agent = createAgent(`https://httpbin.org`);
try {
{
const res = await agent.send({
path: "/get?deno=land",
method: "GET",
});
assertEquals(res.status, 200);
const resp = await res.json();
assertEquals(resp["args"]["deno"], "land");
}
{
const res = await agent.send({
path: "/post",
method: "POST",
headers: new Headers({
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
}),
body: "deno=land",
});
assertEquals(res.status, 200);
const resp = await res.json();
assertEquals(resp["form"]["deno"], "land");
}
} finally {
agent.conn.close();
}
});
test("agent unread body", async () => {
const agent = createAgent(`http://127.0.0.1:${port}`);
try {
await agent.send({ path: "/get", method: "GET" });
await agent.send({ path: "/post", method: "POST", body: encode("ko") });
const resp = await agent.send({
path: "/post",
method: "POST",
body: encode("denoland"),
});
assertEquals(await resp.text(), "denoland");
} finally {
agent.conn.close();
}
});
test("agent invalid scheme", async () => {
assertThrows(() => {
createAgent("ftp://127.0.0.1");
});
});
});