-
Notifications
You must be signed in to change notification settings - Fork 5
/
net.test.js
67 lines (65 loc) · 1.86 KB
/
net.test.js
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
import { assert } from "chai";
import { request } from "../src/net.js";
describe("request", () => {
it("should return a text from the web", async () => {
try {
const text = (
await request(
"https://raw.githubusercontent.com/GaelGirodon/markdown-to-document/develop/README.md"
)
).body.toString("utf8");
assert.isString(text);
assert.isNotEmpty(text);
} catch (e) {
assert.fail("should not throw");
}
});
it("should fail if the URL is invalid", async () => {
let error;
try {
await request("this!is-not*a°valid#URL");
} catch (e) {
error = e;
}
assert.exists(error);
assert.match(error.message, /^Invalid URL$/);
});
it("should fail if response is not 200 OK", async () => {
let error;
try {
await request(
"https://raw.githubusercontent.com/GaelGirodon/markdown-to-document/develop/NOTFOUND.md"
);
} catch (e) {
error = e;
}
assert.exists(error);
assert.match(error.message, /^an error occurred fetching content \(status: 404\)$/);
});
it("should return an empty text without error if failIfEmpty is false", async () => {
try {
const text = (
await request(
"https://raw.githubusercontent.com/GaelGirodon/markdown-to-document/0.2.0/assets/themes/.gitkeep"
)
).body.toString("utf8");
assert.isString(text);
assert.isEmpty(text);
} catch (e) {
assert.fail("should not throw");
}
});
it("should fail with an empty text if failIfEmpty is true", async () => {
let error;
try {
await request(
"https://raw.githubusercontent.com/GaelGirodon/markdown-to-document/0.2.0/assets/themes/.gitkeep",
true
);
} catch (e) {
error = e;
}
assert.exists(error);
assert.match(error.message, /^content is empty$/);
});
});