Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfilip committed Jan 14, 2022
1 parent dbcb4d4 commit d5ca769
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 52 deletions.
58 changes: 13 additions & 45 deletions test/agent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as sinon from "sinon";
import * as sinonDefault from "sinon";
import { assert } from "chai";
import { Agent, toHeader } from "../lib/agent";
import { Response } from "got";
import { errors } from "../lib";

const { IdealPostcodesError } = errors;

//@ts-ignore
const sinon = sinonDefault.default;

describe("Agent", () => {
let agent: Agent;

Expand Down Expand Up @@ -74,7 +77,9 @@ describe("Agent", () => {
throwHttpErrors: false,
searchParams: query,
responseType: "json",
timeout,
timeout: {
request: timeout
},
} as any);
});

Expand Down Expand Up @@ -109,7 +114,9 @@ describe("Agent", () => {
body,
headers: header,
searchParams: query,
timeout,
timeout: {
request: timeout
},
} as any);
});

Expand Down Expand Up @@ -144,7 +151,9 @@ describe("Agent", () => {
headers: {},
searchParams: {},
responseType: "json",
timeout: 1000,
timeout: {
request: 1000
}
} as any);
});

Expand Down Expand Up @@ -282,45 +291,4 @@ describe("Agent", () => {
throw new Error("This should be unreachable");
});
});

describe("Open API", () => {
it("passing openapi JSON body with post request", async () => {
const method: HttpVerb = "POST";
const query = { foo: "bar" };
const header = {
"user-agent": "openid-client", // the one that we use works
"accept": "application/json",
"accept-encoding": "gzip, deflate, br"
};
const url = "http://www.foo.com/";
const timeout = 1000;
const SUCCESS = 200;
const body = { foo: "bar" };

const response: unknown = {
statusCode: SUCCESS,
headers: header,
body: Buffer.from("{}"),
url,
};

const stub = sinon
.stub(agent, "got")
.resolves(response as Response<Buffer>);

await agent.http({ body, method, timeout, url, header, query });

sinon.assert.calledOnce(stub);
// @ts-ignore
sinon.assert.calledWithExactly(stub, url, {
method,
throwHttpErrors: false,
responseType: "json",
body,
headers: header,
searchParams: query,
timeout,
} as any);
});
});
});
14 changes: 8 additions & 6 deletions test/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { assert } from "chai";
import { Client, Config } from "../lib/client";
import { Agent } from "../lib/agent";
import {
defaults,
Config as InterfaceConfig,
} from "../lib";
import { defaults, Config as InterfaceConfig } from "../lib";

describe("Client", () => {
describe("instantiation", () => {
Expand All @@ -20,7 +17,10 @@ describe("Client", () => {
assert.equal(client.config.tls, defaults.tls);
assert.equal(client.config.baseUrl, defaults.baseUrl);
assert.equal(client.config.version, defaults.version);
assert.equal(client.config.strictAuthorisation, defaults.strictAuthorisation);
assert.equal(
client.config.strictAuthorisation,
defaults.strictAuthorisation
);
assert.equal(client.config.timeout, defaults.timeout);
});

Expand Down Expand Up @@ -51,7 +51,9 @@ describe("Client", () => {
});

it("assigns got config", () => {
const retry = 2;
const retry = {
limit: 2,
};
const customClient = new Client({ api_key }, { retry });
assert.deepEqual((customClient.config.agent as any).gotConfig, { retry });
});
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import nock from "nock";
import { resolve, basename } from "path";
import { writeFile } from "fs";

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename);


/**
* loadHttpFixtures
*
Expand Down
1 change: 1 addition & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert } from "chai";
import { Agent, Client } from "../lib/index";


describe("Module exports", () => {
it("exports Agent", () => {
assert.isDefined(Agent);
Expand Down
3 changes: 3 additions & 0 deletions test/integration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { fileURLToPath } from 'url'
import { assert } from "chai";
import { Client } from "../lib/client";
import { errors, ping, lookupPostcode } from "../lib";
import { loadHttpFixtures } from "./fixtures/index";

const __filename = fileURLToPath(import.meta.url);

loadHttpFixtures(__filename);

const SUCCESS = 200;
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"extends": "@cablanchard/tsconfig",
"compilerOptions": {
"outDir": "dist",
"target": "es2015"
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"allowJs": true,
"resolveJsonModule": true,
"skipLibCheck": true
},
"include": ["lib/**/*"]
}

0 comments on commit d5ca769

Please sign in to comment.