Skip to content

Commit 09e8356

Browse files
authored
feat(internet): add options to url() (#1480)
1 parent 64b031a commit 09e8356

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

src/modules/internet/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export type HTTPStatusCodeType =
2020
| 'serverError'
2121
| 'redirection';
2222

23+
export type HTTPProtocolType = 'http' | 'https';
24+
2325
/**
2426
* Module to generate internet related entries.
2527
*/
@@ -226,15 +228,28 @@ export class InternetModule {
226228
}
227229

228230
/**
229-
* Generates a random url.
231+
* Generates a random http(s) url.
232+
*
233+
* @param options Optional options object.
234+
* @param options.appendSlash Whether to append a slash to the end of the url (path). Defaults to a random boolean value.
235+
* @param options.protocol The protocol to use. Defaults to `'https'`.
230236
*
231237
* @example
232238
* faker.internet.url() // 'https://remarkable-hackwork.info'
239+
* faker.internet.url({ appendSlash: true }) // 'https://slow-timer.info/'
240+
* faker.internet.url({ protocol: 'http', appendSlash: false }) // 'http://www.terrible-idea.com'
233241
*
234242
* @since 2.1.5
235243
*/
236-
url(): string {
237-
return `${this.protocol()}://${this.domainName()}`;
244+
url(
245+
options: {
246+
appendSlash?: boolean;
247+
protocol?: HTTPProtocolType;
248+
} = {}
249+
): string {
250+
const { appendSlash = this.faker.datatype.boolean(), protocol = 'https' } =
251+
options;
252+
return `${protocol}://${this.domainName()}${appendSlash ? '/' : ''}`;
238253
}
239254

240255
/**

test/__snapshots__/internet.spec.ts.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ exports[`internet > 42 > port 1`] = `24545`;
5252

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

55-
exports[`internet > 42 > url 1`] = `"http://staid-veil.biz"`;
55+
exports[`internet > 42 > url > noArgs 1`] = `"https://staid-veil.biz"`;
56+
57+
exports[`internet > 42 > url > with slash appended 1`] = `"https://hasty-shin.org/"`;
58+
59+
exports[`internet > 42 > url > without slash appended and with http protocol 1`] = `"http://hasty-shin.org"`;
5660

5761
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"`;
5862

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

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

115-
exports[`internet > 1211 > url 1`] = `"https://jubilant-temple.net"`;
119+
exports[`internet > 1211 > url > noArgs 1`] = `"https://jubilant-temple.net/"`;
120+
121+
exports[`internet > 1211 > url > with slash appended 1`] = `"https://vibrant-infix.org/"`;
122+
123+
exports[`internet > 1211 > url > without slash appended and with http protocol 1`] = `"http://vibrant-infix.org"`;
116124

117125
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"`;
118126

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

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

175-
exports[`internet > 1337 > url 1`] = `"http://neat-chopstick.biz"`;
183+
exports[`internet > 1337 > url > noArgs 1`] = `"https://neat-chopstick.biz"`;
184+
185+
exports[`internet > 1337 > url > with slash appended 1`] = `"https://fair-migration.com/"`;
186+
187+
exports[`internet > 1337 > url > without slash appended and with http protocol 1`] = `"http://fair-migration.com"`;
176188

177189
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"`;
178190

test/internet.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('internet', () => {
1616
'avatar',
1717
'protocol',
1818
'httpMethod',
19-
'url',
2019
'domainName',
2120
'domainSuffix',
2221
'domainWord',
@@ -61,6 +60,15 @@ describe('internet', () => {
6160
t.describe('emoji', (t) => {
6261
t.it('noArgs').it('with options', { types: ['nature'] });
6362
});
63+
64+
t.describe('url', (t) => {
65+
t.it('noArgs')
66+
.it('with slash appended', { appendSlash: true })
67+
.it('without slash appended and with http protocol', {
68+
appendSlash: false,
69+
protocol: 'http',
70+
});
71+
});
6472
});
6573

6674
describe(`random seeded tests for seed ${faker.seed()}`, () => {
@@ -325,6 +333,23 @@ describe('internet', () => {
325333
expect(url).toBeTypeOf('string');
326334
expect(url).toSatisfy(validator.isURL);
327335
});
336+
337+
it('should return a valid url with slash appended at the end', () => {
338+
const url = faker.internet.url({ appendSlash: true });
339+
340+
expect(url).toBeTruthy();
341+
expect(url).toBeTypeOf('string');
342+
expect(url).toSatisfy(validator.isURL);
343+
expect(url.endsWith('/')).toBeTruthy();
344+
});
345+
346+
it('should return a valid url with given protocol', () => {
347+
const url = faker.internet.url({ protocol: 'http' });
348+
349+
expect(url).toBeTruthy();
350+
expect(url).toBeTypeOf('string');
351+
expect(url).toSatisfy(validator.isURL);
352+
});
328353
});
329354

330355
describe('domainName()', () => {

0 commit comments

Comments
 (0)