http.spec.ts
5.6 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { deepEqual, equal, ok } from "assert";
import { HTTPError, RequestError } from "../lib/exceptions";
import HTTPClient from "../lib/http";
import { getStreamData } from "./helpers/stream";
import * as nock from "nock";
import { readFileSync, createReadStream } from "fs";
import { join } from "path";
const pkg = require("../package.json");
const baseURL = "https://line.me";
const defaultHeaders = {
"test-header-key": "Test-Header-Value",
};
describe("http", () => {
const http = new HTTPClient({
baseURL,
defaultHeaders,
});
before(() => nock.disableNetConnect());
afterEach(() => nock.cleanAll());
after(() => nock.enableNetConnect());
const interceptionOption = {
reqheaders: {
...defaultHeaders,
"User-Agent": `${pkg.name}/${pkg.version}`,
},
};
const mockGet = (
path: string,
expectedQuery?: boolean | string | nock.DataMatcherMap | URLSearchParams,
) => {
let _it = nock(baseURL, interceptionOption).get(path);
if (expectedQuery) {
_it = _it.query(expectedQuery);
}
return _it.reply(200, {});
};
const mockPost = (path: string, expectedBody?: nock.RequestBodyMatcher) => {
return nock(baseURL, interceptionOption)
.post(path, expectedBody)
.reply(200, {});
};
const mockDelete = (
path: string,
expectedQuery?: boolean | string | nock.DataMatcherMap | URLSearchParams,
) => {
let _it = nock(baseURL, interceptionOption).delete(path);
if (expectedQuery) {
_it = _it.query(expectedQuery);
}
return _it.reply(200, {});
};
it("get", async () => {
const scope = mockGet("/get");
const res = await http.get<any>(`/get`);
equal(scope.isDone(), true);
deepEqual(res, {});
});
it("get with query", async () => {
const scope = mockGet("/get", { x: 10 });
const res = await http.get<any>(`/get`, { x: 10 });
equal(scope.isDone(), true);
deepEqual(res, {});
});
it("post without body", async () => {
const scope = mockPost("/post");
const res = await http.post<any>(`/post`);
equal(scope.isDone(), true);
deepEqual(res, {});
});
it("post with body", async () => {
const testBody = {
id: 12345,
message: "hello, body!",
};
const scope = mockPost("/post/body", testBody);
const res = await http.post<any>(`/post/body`, testBody);
equal(scope.isDone(), true);
deepEqual(res, {});
});
it("getStream", async () => {
const scope = nock(baseURL, interceptionOption)
.get("/stream.txt")
.reply(200, () =>
createReadStream(join(__dirname, "./helpers/stream.txt")),
);
const stream = await http.getStream(`/stream.txt`);
const data = await getStreamData(stream);
equal(scope.isDone(), true);
equal(data, "hello, stream!\n");
});
it("delete", async () => {
const scope = mockDelete("/delete");
await http.delete(`/delete`);
equal(scope.isDone(), true);
});
it("delete with query", async () => {
const scope = mockDelete("/delete", { x: 10 });
await http.delete(`/delete`, { x: 10 });
equal(scope.isDone(), true);
});
const mockPostBinary = (
buffer: Buffer,
reqheaders: Record<string, nock.RequestHeaderMatcher>,
) => {
return nock(baseURL, {
reqheaders: {
...interceptionOption.reqheaders,
...reqheaders,
"content-length": buffer.length + "",
},
})
.post("/post/binary", buffer)
.reply(200, {});
};
it("postBinary", async () => {
const filepath = join(__dirname, "/helpers/line-icon.png");
const buffer = readFileSync(filepath);
const scope = mockPostBinary(buffer, {
"content-type": "image/png",
});
await http.postBinary(`/post/binary`, buffer);
equal(scope.isDone(), true);
});
it("postBinary with specific content type", async () => {
const filepath = join(__dirname, "/helpers/line-icon.png");
const buffer = readFileSync(filepath);
const scope = mockPostBinary(buffer, {
"content-type": "image/jpeg",
});
await http.postBinary(`/post/binary`, buffer, "image/jpeg");
equal(scope.isDone(), true);
});
it("postBinary with stream", async () => {
const filepath = join(__dirname, "/helpers/line-icon.png");
const stream = createReadStream(filepath);
const buffer = readFileSync(filepath);
const scope = mockPostBinary(buffer, {
"content-type": "image/png",
});
await http.postBinary(`/post/binary`, stream);
equal(scope.isDone(), true);
});
it("fail with 404", async () => {
const scope = nock(baseURL, interceptionOption).get("/404").reply(404, {});
try {
await http.get(`/404`);
ok(false);
} catch (err) {
ok(err instanceof HTTPError);
equal(scope.isDone(), true);
equal(err.statusCode, 404);
}
});
it("fail with wrong addr", async () => {
nock.enableNetConnect();
try {
await http.get("http://domain.invalid");
ok(false);
} catch (err) {
ok(err instanceof RequestError);
equal(err.code, "ENOTFOUND");
nock.disableNetConnect();
}
});
it("will generate default params", async () => {
const scope = nock(baseURL, {
reqheaders: {
"User-Agent": `${pkg.name}/${pkg.version}`,
},
})
.get("/get")
.reply(200, {});
const http = new HTTPClient();
const res = await http.get<any>(`${baseURL}/get`);
equal(scope.isDone(), true);
deepEqual(res, {});
});
});