Charlotte Garnier
Charlotte Garnier

Reputation: 1

How to change the statusText of statusCode: 200 in webdriverio

I'm testing a website api with mocks and stubs, and need to respond to a http GET request. I'm using "webdriverio": "8.32.2" and browser.mock. The response needs to set the statusText of statusCode: 200 to a custom text, let's say "my-text". But, I did not find any way to do it with webdriverio.

Instead of this kind of response :

{
    "headers": {
        "normalizedNames": {},
        "lazyUpdate": null
    },
    "status": 200,
    "statusText": "OK",
    "url": "https://localhost:8090/api/my-api,
    "ok": true,
    "type": 4,
    "body": {}
}

I need to provide this one :

{
    "headers": {
        "normalizedNames": {},
        "lazyUpdate": null
    },
    "status": 200,
    "statusText": "my-text",
    "url": "https://localhost:8090/api/my-api",
    "ok": true,
    "type": 4,
    "body": {}
}

Where the statusText is set to "my-text" instead of "OK".

webdriverio mock and types libraries let me respond with the structure MockResponseParams :

export type MockResponseParams = {
    statusCode?: number | ((request: Matches) => number);
    headers?: Record<string, string> | ((request: Matches) => Record<string, string>);
    /**
     * fetch real response before responding with mocked data. Default: true
     */
    fetchResponse?: boolean;
};

But as you can see, there's no room for statusText.

I tried to force the addition of statusText like this :

export async function myStub(): Promise<void> {
  const mock = await browser.mock(myUrlRegExp, { method: HttpMethod.Get });
  mock.respond(getResponseFromRequest,
 {
      fetchResponse: false,
      headers: CORS_HEADERS,
      statusCode: 200,
      ...{ statusText: 'my-text' },
   }
);
}

But it did not change the response.

I also tried to add the statusText to the body of the response, but if statusText is in the body, it is not recognized by my website as the text of statusCode 200, so my test is failing.

Is there any way to change or customize the statusText of a statusCode in webdriverio?

Upvotes: 0

Views: 42

Answers (0)

Related Questions