Skip to content

Commit

Permalink
fix: parse document arg order adding tests #120
Browse files Browse the repository at this point in the history
  • Loading branch information
zcstarr committed May 31, 2019
1 parent 9818718 commit 7d00f73
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@
"@types/json-schema": "^7.0.3",
"@types/lodash": "^4.14.123",
"@types/node-fetch": "^2.1.6",
"@types/rimraf": "^2.0.2",
"@types/webpack-env": "^1.13.9",
"jest": "^24.0.0",
"json-schema": "^0.2.3",
"rimraf": "^2.6.3",
"semantic-release": "^15.13.12",
"ts-jest": "^24.0.0",
"tslint": "^5.13.1",
Expand Down
70 changes: 70 additions & 0 deletions src/index.test.ts
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);
});

});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MethodCallValidator, ParameterValidationError } from "./method-call-val
import readSchemaFromFile from "./get-open-rpc-document-from-file";
import fetchUrlSchema from "./get-open-rpc-document-from-url";

const parseOpenRPCDocument = makeParseOpenRPCDocument(readSchemaFromFile, fetchUrlSchema);
const parseOpenRPCDocument = makeParseOpenRPCDocument(fetchUrlSchema, readSchemaFromFile);

export {
parseOpenRPCDocument,
Expand Down

0 comments on commit 7d00f73

Please # to comment.