-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.test.ts
More file actions
74 lines (67 loc) · 2.22 KB
/
index.test.ts
File metadata and controls
74 lines (67 loc) · 2.22 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
import fs from "fs-extra";
import { OpenrpcDocument as OpenRPC } from "./types";
import { parseOpenRPCDocument } from "./";
import rimraf from "rimraf";
import { promisify } from "util";
import http from "http";
import { AddressInfo } from "net";
import { rpcDocIsEqual } from "./helper-functions";
const rmDir = promisify(rimraf);
export const mockServer = (file: string): Promise<http.Server> => {
return new Promise((resolve: (value: http.Server) => void) => {
const testServer = http.createServer((req, res) => {
const rs = fs.createReadStream(file);
if (!req.url) {
throw new Error("Request missing url");
}
if (req.url.search("download") > 0) {
res.writeHead(200, { "Content-Type": "application/json" });
rs.pipe(res);
rs.on("close", () => {
res.end(null);
});
return;
}
});
testServer.listen(0, () => {
resolve(testServer);
});
});
};
describe("parseOpenRPCDocument", () => {
let dirName: string;
let testDocPath: string;
let testServer: http.Server;
const testDoc: OpenRPC = {
info: {
description: "test-doc",
title: "testDoc",
version: "1.0.0",
},
methods: [],
openrpc: "1.0.0",
};
beforeAll(async () => {
dirName = await fs.mkdtemp("test-openrpc-doc");
testDocPath = `${dirName}/openrpc.json`;
await fs.writeFile(testDocPath, JSON.stringify(testDoc, null, 2));
testServer = await mockServer(testDocPath);
});
afterAll(async () => {
await rmDir(dirName);
await new Promise((resolve) => testServer.close(resolve));
});
it("should parseOpenRPCDocument from string", async () => {
const doc = await parseOpenRPCDocument(JSON.stringify(testDoc, null, 2));
expect(rpcDocIsEqual(doc, testDoc)).toBe(true);
});
it("should parseOpenRPCDocument from file", async () => {
const doc = await parseOpenRPCDocument(testDocPath);
expect(rpcDocIsEqual(doc, testDoc)).toBe(true);
});
it("should parseOpenRPCDocument from server", async () => {
const { port } = testServer.address() as AddressInfo;
const doc = await parseOpenRPCDocument(`http://localhost:${port}/download/openrpc.json`);
expect(rpcDocIsEqual(doc, testDoc)).toBe(true);
});
});