diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 1c7e85cc44b..60e3ac55a46 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -20,6 +20,8 @@ export type HTTPStatusCodeType = | 'serverError' | 'redirection'; +export type HTTPProtocolType = 'http' | 'https'; + /** * Module to generate internet related entries. */ @@ -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 ? '/' : ''}`; } /** diff --git a/test/__snapshots__/internet.spec.ts.snap b/test/__snapshots__/internet.spec.ts.snap index d2c4391163f..500f8d8c88e 100644 --- a/test/__snapshots__/internet.spec.ts.snap +++ b/test/__snapshots__/internet.spec.ts.snap @@ -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"`; @@ -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"`; @@ -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"`; diff --git a/test/internet.spec.ts b/test/internet.spec.ts index cbbf2745ddd..02f0edaa760 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -16,7 +16,6 @@ describe('internet', () => { 'avatar', 'protocol', 'httpMethod', - 'url', 'domainName', 'domainSuffix', 'domainWord', @@ -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()}`, () => { @@ -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()', () => {