@mswjs/interceptors 0.25.3 → 0.25.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  var _chunkUF7QIAQ5js = require('./chunk-UF7QIAQ5.js');
4
4
 
5
5
 
6
- var _chunkNCHFM2TBjs = require('./chunk-NCHFM2TB.js');
6
+ var _chunkBAS5F5A4js = require('./chunk-BAS5F5A4.js');
7
7
 
8
8
 
9
9
  var _chunkJCWVLTP7js = require('./chunk-JCWVLTP7.js');
@@ -24,7 +24,7 @@ var RemoteHttpInterceptor = class extends _chunkUF7QIAQ5js.BatchInterceptor {
24
24
  super({
25
25
  name: "remote-interceptor",
26
26
  interceptors: [
27
- new (0, _chunkNCHFM2TBjs.ClientRequestInterceptor)(),
27
+ new (0, _chunkBAS5F5A4js.ClientRequestInterceptor)(),
28
28
  new (0, _chunkJCWVLTP7js.XMLHttpRequestInterceptor)()
29
29
  ]
30
30
  });
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-UBEFEZXT.mjs";
4
4
  import {
5
5
  ClientRequestInterceptor
6
- } from "./chunk-MCO3RLQC.mjs";
6
+ } from "./chunk-6HZYLI7O.mjs";
7
7
  import {
8
8
  XMLHttpRequestInterceptor
9
9
  } from "./chunk-FB53TMYN.mjs";
@@ -152,6 +152,12 @@ function createRequest(clientRequest) {
152
152
  headers.append(headerName, value.toString());
153
153
  }
154
154
  }
155
+ if (clientRequest.url.username && clientRequest.url.password) {
156
+ const auth = `${clientRequest.url.username}:${clientRequest.url.password}`;
157
+ headers.set("Authorization", `Basic ${btoa(auth)}`);
158
+ clientRequest.url.username = "";
159
+ clientRequest.url.password = "";
160
+ }
155
161
  const method = clientRequest.method || "GET";
156
162
  return new Request(clientRequest.url, {
157
163
  method,
@@ -576,7 +582,9 @@ function getUrlByRequestOptions(options) {
576
582
  logger3.info("credentials", credentials);
577
583
  const authString = credentials ? `${credentials.username}:${credentials.password}@` : "";
578
584
  logger3.info("auth string:", authString);
579
- const url = new URL(`${protocol}//${authString}${hostname}${path}`);
585
+ const url = new URL(`${protocol}//${hostname}${path}`);
586
+ url.username = (credentials == null ? void 0 : credentials.username) || "";
587
+ url.password = (credentials == null ? void 0 : credentials.password) || "";
580
588
  logger3.info("created url:", url);
581
589
  return url;
582
590
  }
@@ -152,6 +152,12 @@ function createRequest(clientRequest) {
152
152
  headers.append(headerName, value.toString());
153
153
  }
154
154
  }
155
+ if (clientRequest.url.username && clientRequest.url.password) {
156
+ const auth = `${clientRequest.url.username}:${clientRequest.url.password}`;
157
+ headers.set("Authorization", `Basic ${btoa(auth)}`);
158
+ clientRequest.url.username = "";
159
+ clientRequest.url.password = "";
160
+ }
155
161
  const method = clientRequest.method || "GET";
156
162
  return new Request(clientRequest.url, {
157
163
  method,
@@ -576,7 +582,9 @@ function getUrlByRequestOptions(options) {
576
582
  logger3.info("credentials", credentials);
577
583
  const authString = credentials ? `${credentials.username}:${credentials.password}@` : "";
578
584
  logger3.info("auth string:", authString);
579
- const url = new URL(`${protocol}//${authString}${hostname}${path}`);
585
+ const url = new URL(`${protocol}//${hostname}${path}`);
586
+ url.username = (credentials == null ? void 0 : credentials.username) || "";
587
+ url.password = (credentials == null ? void 0 : credentials.password) || "";
580
588
  logger3.info("created url:", url);
581
589
  return url;
582
590
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkNCHFM2TBjs = require('../../chunk-NCHFM2TB.js');
3
+ var _chunkBAS5F5A4js = require('../../chunk-BAS5F5A4.js');
4
4
  require('../../chunk-OGN3ZR35.js');
5
5
  require('../../chunk-5PTPJLB7.js');
6
6
  require('../../chunk-3XFLRXRY.js');
7
7
 
8
8
 
9
- exports.ClientRequestInterceptor = _chunkNCHFM2TBjs.ClientRequestInterceptor;
9
+ exports.ClientRequestInterceptor = _chunkBAS5F5A4js.ClientRequestInterceptor;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ClientRequestInterceptor
3
- } from "../../chunk-MCO3RLQC.mjs";
3
+ } from "../../chunk-6HZYLI7O.mjs";
4
4
  import "../../chunk-3IYIKC3X.mjs";
5
5
  import "../../chunk-YQGTMMOZ.mjs";
6
6
  import "../../chunk-GM3YBSM3.mjs";
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkNCHFM2TBjs = require('../chunk-NCHFM2TB.js');
3
+ var _chunkBAS5F5A4js = require('../chunk-BAS5F5A4.js');
4
4
 
5
5
 
6
6
  var _chunkJCWVLTP7js = require('../chunk-JCWVLTP7.js');
@@ -12,7 +12,7 @@ require('../chunk-3XFLRXRY.js');
12
12
 
13
13
  // src/presets/node.ts
14
14
  var node_default = [
15
- new (0, _chunkNCHFM2TBjs.ClientRequestInterceptor)(),
15
+ new (0, _chunkBAS5F5A4js.ClientRequestInterceptor)(),
16
16
  new (0, _chunkJCWVLTP7js.XMLHttpRequestInterceptor)()
17
17
  ];
18
18
 
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ClientRequestInterceptor
3
- } from "../chunk-MCO3RLQC.mjs";
3
+ } from "../chunk-6HZYLI7O.mjs";
4
4
  import {
5
5
  XMLHttpRequestInterceptor
6
6
  } from "../chunk-FB53TMYN.mjs";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mswjs/interceptors",
3
3
  "description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
4
- "version": "0.25.3",
4
+ "version": "0.25.4",
5
5
  "main": "./lib/node/index.js",
6
6
  "module": "./lib/node/index.mjs",
7
7
  "types": "./lib/node/index.d.ts",
@@ -20,6 +20,21 @@ export function createRequest(clientRequest: NodeClientRequest): Request {
20
20
  }
21
21
  }
22
22
 
23
+ /**
24
+ * Translate the authentication from the request URL to
25
+ * the request "Authorization" header.
26
+ * @see https://github.com/mswjs/interceptors/issues/438
27
+ */
28
+ if (clientRequest.url.username && clientRequest.url.password) {
29
+ const auth = `${clientRequest.url.username}:${clientRequest.url.password}`
30
+ headers.set('Authorization', `Basic ${btoa(auth)}`)
31
+
32
+ // Remove the credentials from the URL since you cannot
33
+ // construct a Request instance with such a URL.
34
+ clientRequest.url.username = ''
35
+ clientRequest.url.password = ''
36
+ }
37
+
23
38
  const method = clientRequest.method || 'GET'
24
39
 
25
40
  return new Request(clientRequest.url, {
@@ -86,7 +86,14 @@ function getHostByRequestOptions(options: ResolvedRequestOptions): string {
86
86
  return host || DEFAULT_HOST
87
87
  }
88
88
 
89
- function getAuthByRequestOptions(options: ResolvedRequestOptions) {
89
+ interface RequestAuth {
90
+ username: string
91
+ password: string
92
+ }
93
+
94
+ function getAuthByRequestOptions(
95
+ options: ResolvedRequestOptions
96
+ ): RequestAuth | undefined {
90
97
  if (options.auth) {
91
98
  const [username, password] = options.auth.split(':')
92
99
  return { username, password }
@@ -159,7 +166,10 @@ export function getUrlByRequestOptions(options: ResolvedRequestOptions): URL {
159
166
  : ''
160
167
  logger.info('auth string:', authString)
161
168
 
162
- const url = new URL(`${protocol}//${authString}${hostname}${path}`)
169
+ const url = new URL(`${protocol}//${hostname}${path}`)
170
+ url.username = credentials?.username || ''
171
+ url.password = credentials?.password || ''
172
+
163
173
  logger.info('created url:', url)
164
174
 
165
175
  return url