Skip to content

Commit

Permalink
Add cookie parameters to request parameters (#19)
Browse files Browse the repository at this point in the history
* add support for cookie parameters

* add support for multiple cookie parameters
  • Loading branch information
Joe-Dunleavy authored Oct 10, 2022
1 parent 82ea371 commit a3615cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions features/support/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export class ModelSteps extends BaseModelStep {
expect(this.requestParams.headers).to.have.property("test", headerParam)
}

@then(/the request header should have a cookie property with value (.*)/)
public checkRequestCookie(cookieParam: string) {
expect(this.requestParams.headers).to.have.property("cookie", cookieParam)
}

@when(/calling the method ([a-zA-Z]*) and the server provides an empty response/)
public async callWithoutResponse(methodName: string) {
this.serverResponseObject = null;
Expand Down
15 changes: 14 additions & 1 deletion template/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export async function request(
return acc;
}, {});

// add cookies to headers
const cookies = params
.filter((p) => p.location === "cookie")
.reduce<Headers>((acc, param, index, array) => {
acc["cookie"] ??= "";
acc["cookie"] += `${param.name}=${param.value}`;
if(index !== array.length - 1){
acc["cookie"] += ";"
}
return acc;
}, {});

// provide a bearer token if required
if (config.bearerToken) {
additionalHeaders["Authorization"] = `Bearer ${config.bearerToken}`;
Expand All @@ -59,13 +71,14 @@ export async function request(
accept: "application/json",
"Content-Type": "application/json",
...additionalHeaders,
...cookies,
},
};

const bodyParam = params.find((p) => p.location === "body");
if (bodyParam) {
requestParams.body = bodyParam.value;
}

return await config.transport(requestParams);
}

0 comments on commit a3615cb

Please sign in to comment.