Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { URL } from '@pollyjs/utils';
* @returns {string}
*/
export default function getUrlFromOptions(options = {}) {
if (options.href) {
// Node's http.request documents hostname, host, path, port, and protocol.
// `href` is not a standard option, but some clients set it to a stale or
// unrelated value. Prefer the documented fields whenever they are present.
const hasStandardUrlParts = Boolean(
options.hostname || options.host || options.path
);

if (options.href && !hasStandardUrlParts) {
return options.href;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import getUrlFromOptions from '../../../src/utils/get-url-from-options';

describe('Unit | Utils | getUrlFromOptions', function () {
it('should exist', function () {
expect(getUrlFromOptions).to.be.a('function');
});

it('should build the url from host and path', function () {
expect(
getUrlFromOptions({
protocol: 'https:',
host: 'echo.free.beeceptor.com',
path: '/great-shot-kid',
method: 'GET'
})
).to.equal('https://echo.free.beeceptor.com/great-shot-kid');
});

it('should ignore a conflicting href when host and path are set', function () {
expect(
getUrlFromOptions({
protocol: 'https:',
host: 'echo.free.beeceptor.com',
path: '/great-shot-kid',
method: 'GET',
href: 'https://echo.free.beeceptor.com/these-are-not-the-droids-you-are-looking-for'
})
).to.equal('https://echo.free.beeceptor.com/great-shot-kid');
});

it('should fall back to href when no standard url fields are present', function () {
expect(
getUrlFromOptions({
href: 'https://example.com/from-href'
})
).to.equal('https://example.com/from-href');
});
});