Skip to content

Commit

Permalink
feat(rest): allow to set request header
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Aug 5, 2024
1 parent 22e623a commit 92a8963
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/REST.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
void describe('REST', () => {
void it('should implement a REST client', async () => {
const scope = nock(`https://api.example.com`)

scope
.get('/foo', undefined, {
reqheaders: {
Accept: 'application/json',
Expand All @@ -27,6 +29,24 @@ void describe('REST', () => {
},
)

scope
.post('/bar', undefined, {
reqheaders: {
Authorization: 'Bearer BAR',
},
})
.reply(
200,
{
'@context':
'https://github.com/hello-nrfcloud/bdd-markdown-steps/tests',
success: true,
},
{
'content-type': 'application/json; charset=utf-8',
},
)

const runner = await runFolder({
folder: path.join(__dirname, 'test-data', 'REST'),
name: 'REST',
Expand Down
24 changes: 22 additions & 2 deletions src/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ let currentRequest: ReturnType<typeof doRequest> = {
match: async () => Promise.reject(new Error(`No request pending!`)),
}

let nextRequestHeaders: Headers = new Headers()

export const steps: StepRunner<Record<string, any>>[] = [
regExpMatchedStep(
{
Expand All @@ -34,14 +36,18 @@ export const steps: StepRunner<Record<string, any>>[] = [
async ({ match: { method, endpoint, withPayload, retry }, log, step }) => {

Check warning on line 36 in src/REST.ts

View workflow job for this annotation

GitHub Actions / tests

Async arrow function has no 'await' expression
const url = new URL(endpoint)

const headers: HeadersInit = {
const headers: Headers = new Headers({
Accept: 'application/json',
})
for (const [key, value] of nextRequestHeaders.entries()) {
headers.set(key, value)
}
nextRequestHeaders = new Headers()

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

currentRequest = doRequest(
Expand All @@ -59,6 +65,20 @@ export const steps: StepRunner<Record<string, any>>[] = [
)
},
),
regExpMatchedStep(
{
regExp:
/^the `(?<header>[^`]+)` header of the next request is `(?<value>[^`]+)`$/,
schema: Type.Object({
header: Type.String({ minLength: 1 }),
value: Type.String({ minLength: 1 }),
}),
},
async ({ match: { header, value }, log: { debug } }) => {

Check warning on line 77 in src/REST.ts

View workflow job for this annotation

GitHub Actions / tests

Async arrow function has no 'await' expression
debug(header, value)
nextRequestHeaders.set(header, value)
},
),
regExpMatchedStep(
{
regExp: /^I should receive a `(?<context>https?:\/\/[^`]+)` response$/,
Expand Down
11 changes: 11 additions & 0 deletions src/test-data/REST/REST.feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ Then I should receive a
`https://github.com/hello-nrfcloud/bdd-markdown-steps/tests` response

And the status code of the last response should be `200`

## Setting headers

Given the `Authorization` header of the next request is `Bearer BAR`

When I `POST` to `${endpoint}/bar`

Then I should receive a
`https://github.com/hello-nrfcloud/bdd-markdown-steps/tests` response

And the status code of the last response should be `200`

0 comments on commit 92a8963

Please sign in to comment.