-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
agent_test.ts
132 lines (128 loc) 路 3.39 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import {
runIfMain,
setFilter,
test
} from "./vendor/https/deno.land/std/testing/mod.ts";
import { encode } from "./vendor/https/deno.land/std/strings/encode.ts";
import { createAgent } from "./agent.ts";
import { createRouter } from "./router.ts";
import {
assertEquals,
assertThrows
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import Reader = Deno.Reader;
import Buffer = Deno.Buffer;
import copy = Deno.copy;
import { it } from "./test_util.ts";
import { ServeListener } from "./server.ts";
async function readString(r: Reader) {
const buf = new Buffer();
await copy(buf, r);
return buf.toString();
}
function setupRouter(port: number): ServeListener {
const router = createRouter();
router.handle("/get", async req => {
return req.respond({
status: 200,
body: encode("ok")
});
});
router.handle("/post", async req => {
return req.respond({
status: 200,
headers: req.headers,
body: req.body
});
});
return router.listen({
hostname: "127.0.0.1",
port
});
}
it("agent", t => {
let port = 8700;
t.beforeAfterAll(() => {
const listener = setupRouter(port);
return () => listener.close();
});
t.run("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 readString(res.body), "ok");
}
{
const res = await agent.send({
path: "/post",
method: "POST",
body: encode("denoland")
});
assertEquals(res.status, 200);
assertEquals(await readString(res.body), "denoland");
}
} finally {
agent.conn.close();
}
});
t.run("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 = JSON.parse(await readString(res.body));
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 body = await readString(res.body);
const resp = JSON.parse(body);
assertEquals(resp["form"]["deno"], "land");
}
} finally {
agent.conn.close();
}
});
t.run("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 { body } = await agent.send({
path: "/post",
method: "POST",
body: encode("denoland")
});
assertEquals(await readString(body), "denoland");
} finally {
agent.conn.close();
}
});
t.run("agent invalid scheme", async () => {
assertThrows(() => {
createAgent("ftp://127.0.0.1");
});
});
});
if (Deno.args.length > 1) {
setFilter(Deno.args[1]);
}
runIfMain(import.meta);