Skip to content

feat(internet): add options to url() #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2022
Merged
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
21 changes: 18 additions & 3 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type HTTPStatusCodeType =
| 'serverError'
| 'redirection';

export type HTTPProtocolType = 'http' | 'https';

/**
* Module to generate internet related entries.
*/
Expand Down Expand Up @@ -226,15 +228,28 @@ export class InternetModule {
}

/**
* Generates a random url.
* Generates a random http(s) url.
*
* @param options Optional options object.
* @param options.appendSlash Whether to append a slash to the end of the url (path). Defaults to a random boolean value.
* @param options.protocol The protocol to use. Defaults to `'https'`.
*
* @example
* faker.internet.url() // 'https://remarkable-hackwork.info'
* faker.internet.url({ appendSlash: true }) // 'https://slow-timer.info/'
* faker.internet.url({ protocol: 'http', appendSlash: false }) // 'http://www.terrible-idea.com'
*
* @since 2.1.5
*/
url(): string {
return `${this.protocol()}://${this.domainName()}`;
url(
options: {
appendSlash?: boolean;
protocol?: HTTPProtocolType;
} = {}
): string {
const { appendSlash = this.faker.datatype.boolean(), protocol = 'https' } =
options;
return `${protocol}://${this.domainName()}${appendSlash ? '/' : ''}`;
}

/**
Expand Down
18 changes: 15 additions & 3 deletions test/__snapshots__/internet.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ exports[`internet > 42 > port 1`] = `24545`;

exports[`internet > 42 > protocol 1`] = `"http"`;

exports[`internet > 42 > url 1`] = `"http://staid-veil.biz"`;
exports[`internet > 42 > url > noArgs 1`] = `"https://staid-veil.biz"`;

exports[`internet > 42 > url > with slash appended 1`] = `"https://hasty-shin.org/"`;

exports[`internet > 42 > url > without slash appended and with http protocol 1`] = `"http://hasty-shin.org"`;

exports[`internet > 42 > userAgent 1`] = `"Mozilla/5.0 (Windows; U; Windows NT 6.2) AppleWebKit/538.0.2 (KHTML, like Gecko) Chrome/29.0.815.0 Safari/538.0.2"`;

Expand Down Expand Up @@ -112,7 +116,11 @@ exports[`internet > 1211 > port 1`] = `60851`;

exports[`internet > 1211 > protocol 1`] = `"https"`;

exports[`internet > 1211 > url 1`] = `"https://jubilant-temple.net"`;
exports[`internet > 1211 > url > noArgs 1`] = `"https://jubilant-temple.net/"`;

exports[`internet > 1211 > url > with slash appended 1`] = `"https://vibrant-infix.org/"`;

exports[`internet > 1211 > url > without slash appended and with http protocol 1`] = `"http://vibrant-infix.org"`;

exports[`internet > 1211 > userAgent 1`] = `"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6 rv:3.0; PT) AppleWebKit/538.2.0 (KHTML, like Gecko) Version/4.1.0 Safari/538.2.0"`;

Expand Down Expand Up @@ -172,7 +180,11 @@ exports[`internet > 1337 > port 1`] = `17172`;

exports[`internet > 1337 > protocol 1`] = `"http"`;

exports[`internet > 1337 > url 1`] = `"http://neat-chopstick.biz"`;
exports[`internet > 1337 > url > noArgs 1`] = `"https://neat-chopstick.biz"`;

exports[`internet > 1337 > url > with slash appended 1`] = `"https://fair-migration.com/"`;

exports[`internet > 1337 > url > without slash appended and with http protocol 1`] = `"http://fair-migration.com"`;

exports[`internet > 1337 > userAgent 1`] = `"Mozilla/5.0 (Windows; U; Windows NT 6.1) AppleWebKit/532.0.0 (KHTML, like Gecko) Chrome/13.0.832.0 Safari/532.0.0"`;

Expand Down
27 changes: 26 additions & 1 deletion test/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('internet', () => {
'avatar',
'protocol',
'httpMethod',
'url',
'domainName',
'domainSuffix',
'domainWord',
Expand Down Expand Up @@ -61,6 +60,15 @@ describe('internet', () => {
t.describe('emoji', (t) => {
t.it('noArgs').it('with options', { types: ['nature'] });
});

t.describe('url', (t) => {
t.it('noArgs')
.it('with slash appended', { appendSlash: true })
.it('without slash appended and with http protocol', {
appendSlash: false,
protocol: 'http',
});
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
Expand Down Expand Up @@ -325,6 +333,23 @@ describe('internet', () => {
expect(url).toBeTypeOf('string');
expect(url).toSatisfy(validator.isURL);
});

it('should return a valid url with slash appended at the end', () => {
const url = faker.internet.url({ appendSlash: true });

expect(url).toBeTruthy();
expect(url).toBeTypeOf('string');
expect(url).toSatisfy(validator.isURL);
expect(url.endsWith('/')).toBeTruthy();
});

it('should return a valid url with given protocol', () => {
const url = faker.internet.url({ protocol: 'http' });

expect(url).toBeTruthy();
expect(url).toBeTypeOf('string');
expect(url).toSatisfy(validator.isURL);
});
});

describe('domainName()', () => {
Expand Down