Skip to content

Commit 92a8963

Browse files
committed
feat(rest): allow to set request header
1 parent 22e623a commit 92a8963

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/REST.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
1010
void describe('REST', () => {
1111
void it('should implement a REST client', async () => {
1212
const scope = nock(`https://api.example.com`)
13+
14+
scope
1315
.get('/foo', undefined, {
1416
reqheaders: {
1517
Accept: 'application/json',
@@ -27,6 +29,24 @@ void describe('REST', () => {
2729
},
2830
)
2931

32+
scope
33+
.post('/bar', undefined, {
34+
reqheaders: {
35+
Authorization: 'Bearer BAR',
36+
},
37+
})
38+
.reply(
39+
200,
40+
{
41+
'@context':
42+
'https://github.com/hello-nrfcloud/bdd-markdown-steps/tests',
43+
success: true,
44+
},
45+
{
46+
'content-type': 'application/json; charset=utf-8',
47+
},
48+
)
49+
3050
const runner = await runFolder({
3151
folder: path.join(__dirname, 'test-data', 'REST'),
3252
name: 'REST',

src/REST.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ let currentRequest: ReturnType<typeof doRequest> = {
1313
match: async () => Promise.reject(new Error(`No request pending!`)),
1414
}
1515

16+
let nextRequestHeaders: Headers = new Headers()
17+
1618
export const steps: StepRunner<Record<string, any>>[] = [
1719
regExpMatchedStep(
1820
{
@@ -34,14 +36,18 @@ export const steps: StepRunner<Record<string, any>>[] = [
3436
async ({ match: { method, endpoint, withPayload, retry }, log, step }) => {
3537
const url = new URL(endpoint)
3638

37-
const headers: HeadersInit = {
39+
const headers: Headers = new Headers({
3840
Accept: 'application/json',
41+
})
42+
for (const [key, value] of nextRequestHeaders.entries()) {
43+
headers.set(key, value)
3944
}
45+
nextRequestHeaders = new Headers()
4046

4147
let bodyAsString: string | undefined = undefined
4248
if (withPayload !== undefined) {
4349
bodyAsString = JSON.stringify(JSON.parse(codeBlockOrThrow(step).code))
44-
headers['Content-type'] = 'application/json'
50+
headers.set('content-type', 'application/json')
4551
}
4652

4753
currentRequest = doRequest(
@@ -59,6 +65,20 @@ export const steps: StepRunner<Record<string, any>>[] = [
5965
)
6066
},
6167
),
68+
regExpMatchedStep(
69+
{
70+
regExp:
71+
/^the `(?<header>[^`]+)` header of the next request is `(?<value>[^`]+)`$/,
72+
schema: Type.Object({
73+
header: Type.String({ minLength: 1 }),
74+
value: Type.String({ minLength: 1 }),
75+
}),
76+
},
77+
async ({ match: { header, value }, log: { debug } }) => {
78+
debug(header, value)
79+
nextRequestHeaders.set(header, value)
80+
},
81+
),
6282
regExpMatchedStep(
6383
{
6484
regExp: /^I should receive a `(?<context>https?:\/\/[^`]+)` response$/,

src/test-data/REST/REST.feature.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ Then I should receive a
1313
`https://github.com/hello-nrfcloud/bdd-markdown-steps/tests` response
1414

1515
And the status code of the last response should be `200`
16+
17+
## Setting headers
18+
19+
Given the `Authorization` header of the next request is `Bearer BAR`
20+
21+
When I `POST` to `${endpoint}/bar`
22+
23+
Then I should receive a
24+
`https://github.com/hello-nrfcloud/bdd-markdown-steps/tests` response
25+
26+
And the status code of the last response should be `200`

0 commit comments

Comments
 (0)