|
| 1 | +import { Then, When, World } from '@cucumber/cucumber' |
| 2 | +import { expect } from 'chai' |
| 3 | +import axios, { AxiosResponse } from 'axios' |
| 4 | + |
| 5 | +const BASE_URL = 'http://localhost:18808' |
| 6 | + |
| 7 | +When( |
| 8 | + 'a client requests path {string} with Accept header {string}', |
| 9 | + async function (this: World<Record<string, any>>, requestPath: string, acceptHeader: string) { |
| 10 | + const response: AxiosResponse = await axios.get(`${BASE_URL}${requestPath}`, { |
| 11 | + headers: { Accept: acceptHeader }, |
| 12 | + validateStatus: () => true, |
| 13 | + }) |
| 14 | + |
| 15 | + this.parameters.httpResponse = response |
| 16 | + }, |
| 17 | +) |
| 18 | + |
| 19 | +When('a client requests dynamic path {string}', async function (this: World<Record<string, any>>, requestPath: string) { |
| 20 | + const response: AxiosResponse = await axios.get(`${BASE_URL}${requestPath}`, { |
| 21 | + validateStatus: () => true, |
| 22 | + }) |
| 23 | + |
| 24 | + this.parameters.httpResponse = response |
| 25 | +}) |
| 26 | + |
| 27 | +When( |
| 28 | + 'a client posts {string} to path {string} with Content-Type {string}', |
| 29 | + async function ( |
| 30 | + this: World<Record<string, any>>, |
| 31 | + body: string, |
| 32 | + requestPath: string, |
| 33 | + contentTypeHeader: string, |
| 34 | + ) { |
| 35 | + const response: AxiosResponse = await axios.post(`${BASE_URL}${requestPath}`, body, { |
| 36 | + headers: { 'content-type': contentTypeHeader }, |
| 37 | + validateStatus: () => true, |
| 38 | + }) |
| 39 | + |
| 40 | + this.parameters.httpResponse = response |
| 41 | + }, |
| 42 | +) |
| 43 | + |
| 44 | +Then('the HTTP response status is {int}', function (this: World<Record<string, any>>, statusCode: number) { |
| 45 | + expect(this.parameters.httpResponse.status).to.equal(statusCode) |
| 46 | +}) |
| 47 | + |
| 48 | +Then( |
| 49 | + 'the HTTP response Content-Type includes {string}', |
| 50 | + function (this: World<Record<string, any>>, contentType: string) { |
| 51 | + // Normalize header value to plain string and compare case-insensitively |
| 52 | + // so assertions are robust to header casing/charset formatting variations. |
| 53 | + const contentTypeHeader = this.parameters.httpResponse.headers['content-type'] |
| 54 | + const headerValue = Array.isArray(contentTypeHeader) ? contentTypeHeader.join(';') : contentTypeHeader |
| 55 | + const normalizedHeader = typeof headerValue === 'string' ? headerValue.toLowerCase() : '' |
| 56 | + expect(normalizedHeader).to.include(contentType.toLowerCase()) |
| 57 | + }, |
| 58 | +) |
0 commit comments