Skip to content

Commit

Permalink
test: signedFetchFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
2fd committed Nov 24, 2021
1 parent 5eb2417 commit 3fe50d8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
36 changes: 29 additions & 7 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@
},
"homepage": "https://github.com/decentraland/decentraland-crypto-fetch#readme",
"devDependencies": {
"@types/isomorphic-fetch": "0.0.35",
"@types/isomorphic-fetch": "^0.0.35",
"@types/jest": "^27.0.2",
"@types/node-fetch": "^2.5.12",
"decentraland-crypto-middleware": "^1.0.1",
"fetch-mock": "^9.11.0",
"husky": "^7.0.4",
"isomorphic-fetch": "^3.0.0",
"jest": "^27.3.1",
"lint-staged": "^11.2.3",
"node-fetch": "^2.6.6",
"prettier": "^2.4.1",
"semantic-release": "^18.0.0",
"ts-jest": "^27.0.7",
Expand Down
6 changes: 3 additions & 3 deletions src/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Authenticator } from "dcl-crypto/dist/Authenticator";
import { AuthIdentity, AuthLinkType } from "dcl-crypto/dist/types";
import signedFetchFactory from "./signedFetchFactory";
import verify from "decentraland-crypto-middleware/lib/verify";
import "isomorphic-fetch";
import fetch, { Request, Headers } from "node-fetch";

const signedFetch = signedFetchFactory();
const signedFetch = signedFetchFactory({ fetch, Headers, Request });

const identity: AuthIdentity = {
ephemeralIdentity: {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe(`fetch`, () => {
const request = new Request("https://httpbin.org/anything", {
method: "POST",
});
const response = await signedFetch(request, { identity, metadata });
const response = await signedFetch(request as any, { identity, metadata });
const body = await response.json();
expect(body).toHaveProperty("method", "POST");
expect(body).toHaveProperty("url", "https://httpbin.org/anything");
Expand Down
6 changes: 3 additions & 3 deletions src/signedFetchFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { SignedRequestInit } from "./types";
import { getImplementation } from "./utils";

export type SignedFetchFactoryOptions = SignedHeaderFactoryOptions & {
URL?: typeof URL;
Request?: typeof Request;
fetch?: typeof fetch;
URL?: typeof URL | any;
Request?: typeof Request | any;
fetch?: typeof fetch | any;
};

export default function signedFetchFactory(
Expand Down
2 changes: 1 addition & 1 deletion src/signedHeaderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const AUTH_TIMESTAMP_HEADER = "x-identity-timestamp";
const AUTH_METADATA_HEADER = "x-identity-metadata";

export type SignedHeaderFactoryOptions = {
Headers?: typeof Headers;
Headers?: typeof Headers | any;
};

export default function signedHeaderFactory(
Expand Down
22 changes: 22 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getImplementation } from "./utils";

describe("src/utils", () => {
describe("getImplementation", () => {
test(`should return implementation if there is any in as option`, () => {
const Math = jest.fn() as any;
const impl = getImplementation({ Math }, "Math");
expect(impl).toBe(impl);
});

test(`should return global implementation if there isn't any as option`, () => {
const impl = getImplementation({}, "Math");
expect(impl).toBe(Math);
});

test(`should should fail if ther isn't any implementation as options or global`, () => {
expect(() => getImplementation({}, "None" as any)).toThrowError(
new ReferenceError('"None" is not defined')
);
});
});
});
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import globalThis from "core-js-pure/stable/global-this";
type Global = typeof globalThis;

export function getImplementation<R extends keyof Global>(
options: Partial<Record<R, Global[R]>>,
options: Record<string, any>,
key: R
): Global[R] {
const result = options[key] ?? globalThis[key];
Expand Down

0 comments on commit 3fe50d8

Please sign in to comment.