Skip to content

Commit

Permalink
Add headers (#59)
Browse files Browse the repository at this point in the history
* added headers as part of replies

* updated test cases

* updated readme, bumped version
  • Loading branch information
shubhbapna authored Mar 7, 2023
1 parent 21a6eda commit 9dacc88
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}}
]);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/endpoint-mocker/response/abstract-response-mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export abstract class ResponseMocker<T, S extends number> {
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 = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ReplyHeaders } from "nock";

export type Response<T, S extends number> = {
status: S;
data: T;
headers?: ReplyHeaders
repeat?: number
};
34 changes: 31 additions & 3 deletions test/moctokit/request-response-mocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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" });
});
}
);
Expand Down

0 comments on commit 9dacc88

Please sign in to comment.