Skip to content

Pass request headers to native xhr request in xhrinterceptor #163

@zzarcon

Description

@zzarcon

Currently, when there is no registered route handler we bypass the request but we are not passing the headers on the XHR case. That can be solved by setting the request headers into the real request:

// xhrinterceptor

nativeSend(data?: any): void {
    const request = new NativeXMLHttpRequest();

    request.timeout = this.timeout;
    request.onload = () => {
      const headers = request
        .getAllResponseHeaders()
        .split("\r\n")
        .reduce((previous, current) => {
          const [header, value] = current.split(": ");
          return {
            ...previous,
            [header]: value
          };
        }, {});
      const response = new KakapoResponse(
        request.status,
        request.responseBody,
        headers
      );
      this._handleResponse(response);
    };

    request.open(this._method, this._url);
    // start of the fix
   Object.keys(this._requestHeaders).forEach(headerName => request.setRequestHeader(headerName, this._requestHeaders[headerName]))
   // end of the fix
    request.send(data);
  }
}

cc @mtscrb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions