From 9dacc88d1059968b0994a897266f3876b780356e Mon Sep 17 00:00:00 2001 From: Shubh Bapna <38372682+shubhbapna@users.noreply.github.com> Date: Tue, 7 Mar 2023 17:42:56 -0500 Subject: [PATCH] Add headers (#59) * added headers as part of replies * updated test cases * updated readme, bumped version --- README.md | 4 +-- package-lock.json | 4 +-- package.json | 2 +- .../response/abstract-response-mocker.ts | 4 +-- .../abstract-response-mocker.types.ts | 3 ++ test/moctokit/request-response-mocker.test.ts | 34 +++++++++++++++++-- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d9e628e..4bc0999 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ const moctokit = new Moctokit(); */ moctokit.rest.projects .createForRepo() - .reply({ status: 200, data: { owner_url: "whatever url" } }); + .reply({ status: 200, data: { owner_url: "whatever url" }, headers: {"some-header": "value"} }); ``` #### Reply N times @@ -149,7 +149,7 @@ const mockedCreateForRepo = moctokit.rest.projects.createForRepo() * Adds all of these responses after the above response in the array. Again doesn't actually mock the api */ mockedCreateForRepo.setResponse([ - {status: 201, data: {owner_url: "something"}}, + {status: 201, data: {owner_url: "something"}, headers: {"some-header": "value"}}, {status: 400, data: {owner_url: "something else"}, repeat: 2} {status: 404, data: {owner_url: "something completely difference"}} ]); diff --git a/package-lock.json b/package-lock.json index 7f48cea..d7c26c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kie/mock-github", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@kie/mock-github", - "version": "1.0.1", + "version": "1.0.2", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@octokit/openapi-types-ghec": "^14.0.0", diff --git a/package.json b/package.json index d4bdfe7..74bf5e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kie/mock-github", - "version": "1.0.1", + "version": "1.0.2", "description": "A bunch of tools to configure and create a local github environment to test your github actions in without having to clutter your github with test repositories, actions or hitting github api rate limits.", "main": "build/src/index.js", "types": "build/src/index.d.ts", diff --git a/src/endpoint-mocker/response/abstract-response-mocker.ts b/src/endpoint-mocker/response/abstract-response-mocker.ts index 8e80627..6ece21a 100644 --- a/src/endpoint-mocker/response/abstract-response-mocker.ts +++ b/src/endpoint-mocker/response/abstract-response-mocker.ts @@ -40,13 +40,13 @@ export abstract class ResponseMocker { if (response) { this.scope = this.interceptor .times(response.repeat ?? 1) - .reply(response.status, response.data as nock.Body); + .reply(response.status, response.data as nock.Body, response.headers); this.interceptor = this.createInterceptor(); } else { this.responses.forEach(res => { this.scope = this.interceptor .times(res.repeat ?? 1) - .reply(res.status, res.data as nock.Body); + .reply(res.status, res.data as nock.Body, res.headers); this.interceptor = this.createInterceptor(); }); this.responses = []; diff --git a/src/endpoint-mocker/response/abstract-response-mocker.types.ts b/src/endpoint-mocker/response/abstract-response-mocker.types.ts index 7b9ed59..4b3356b 100644 --- a/src/endpoint-mocker/response/abstract-response-mocker.types.ts +++ b/src/endpoint-mocker/response/abstract-response-mocker.types.ts @@ -1,5 +1,8 @@ +import { ReplyHeaders } from "nock"; + export type Response = { status: S; data: T; + headers?: ReplyHeaders repeat?: number }; diff --git a/test/moctokit/request-response-mocker.test.ts b/test/moctokit/request-response-mocker.test.ts index 116c237..efd501b 100644 --- a/test/moctokit/request-response-mocker.test.ts +++ b/test/moctokit/request-response-mocker.test.ts @@ -194,16 +194,17 @@ describe.each(["get", "post", "delete", "put", "patch"])( requestMocker .request() - .setResponse({ status: 200, data: { msg: "hello world" } } as never) + .setResponse({ status: 200, data: { msg: "hello world" }, headers: {"test-header": "value"} } as never) .reply(); - const { status, data } = await instance({ + const { status, data, headers } = await instance({ method, url: "/response/any/response?query1=hello", params: { query2: 1 }, }); expect(status).toBe(200); expect(data).toStrictEqual({ msg: "hello world" }); + expect(headers).toMatchObject({ "test-header": "value" }); }); test("setResponse: multiple response", async () => { @@ -220,7 +221,7 @@ describe.each(["get", "post", "delete", "put", "patch"])( requestMocker .request() .setResponse([ - { status: 200, data: { msg: "hello world" } }, + { status: 200, data: { msg: "hello world" }, headers: {"test-header": "value"} }, { status: 201, data: { msg: "another response" } }, ] as never) .reply(); @@ -232,6 +233,7 @@ describe.each(["get", "post", "delete", "put", "patch"])( }); expect(response1.status).toBe(200); expect(response1.data).toStrictEqual({ msg: "hello world" }); + expect(response1.headers).toMatchObject({"test-header": "value"}); const response2 = await instance({ method, @@ -240,6 +242,32 @@ describe.each(["get", "post", "delete", "put", "patch"])( }); expect(response2.status).toBe(201); expect(response2.data).toStrictEqual({ msg: "another response" }); + expect(response2.headers).not.toMatchObject({"test-header": "value"}); + }); + + test("reply: with headers", async () => { + const requestMocker = new MoctokitRequestMocker(url, { + path: "/response/{param1}/response", + method: method as EndpointMethod, + parameters: { + path: ["param1"], + query: ["query1", "query2", "query3"], + body: [], + }, + }); + + requestMocker + .request() + .reply({ status: 200, data: { msg: "hello world" }, headers: {"test-header": "value"} } as never); + + const { status, data, headers } = await instance({ + method, + url: "/response/any/response?query1=hello", + params: { query2: 1 }, + }); + expect(status).toBe(200); + expect(data).toStrictEqual({ msg: "hello world" }); + expect(headers).toMatchObject({ "test-header": "value" }); }); } );