-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: parse document arg order adding tests #120
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import fs from "fs-extra"; | ||
import { OpenRPC } from "@open-rpc/meta-schema"; | ||
import { parseOpenRPCDocument } from "./"; | ||
import rimraf from "rimraf"; | ||
import { promisify } from "util"; | ||
import _ from "lodash"; | ||
import http from "http"; | ||
import { AddressInfo } from "net"; | ||
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-rc1", | ||
}; | ||
|
||
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(_.isEqual(doc, testDoc)).toBe(true); | ||
}); | ||
|
||
it("should parseOpenRPCDocument from file", async () => { | ||
const doc = await parseOpenRPCDocument(testDocPath); | ||
expect(_.isEqual(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(_.isEqual(doc, testDoc)).toBe(true); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters