diff options
| author | johge201 <[email protected]> | 2022-05-21 16:15:20 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-21 16:15:20 +0200 |
| commit | 05f555bc7e304afaa657586ae88f2173507e084f (patch) | |
| tree | 4abbb28774d092c9369b7adc170fcea2f1400c21 | |
| parent | b0cdf29d3a7470b1211e724b68e243b8c96a7f4c (diff) | |
| download | faker-05f555bc7e304afaa657586ae88f2173507e084f.tar.xz faker-05f555bc7e304afaa657586ae88f2173507e084f.zip | |
feat(internet): HTTP random status code (#945)
Co-authored-by: ST-DDT <[email protected]>
Co-authored-by: Piotr Kuczynski <[email protected]>
Co-authored-by: johge201 <[email protected]>
Co-authored-by: Sofia Bertmar <[email protected]>
Co-authored-by: Sofia Bertmar <[email protected]>
Co-authored-by: Shinigami <[email protected]>
| -rw-r--r-- | src/definitions/internet.ts | 6 | ||||
| -rw-r--r-- | src/locales/en/internet/http_status_code.ts | 12 | ||||
| -rw-r--r-- | src/locales/en/internet/index.ts | 2 | ||||
| -rw-r--r-- | src/modules/internet/index.ts | 31 | ||||
| -rw-r--r-- | test/internet.spec.ts | 36 |
5 files changed, 86 insertions, 1 deletions
diff --git a/src/definitions/internet.ts b/src/definitions/internet.ts index 96a79a76..a7801066 100644 --- a/src/definitions/internet.ts +++ b/src/definitions/internet.ts @@ -1,4 +1,4 @@ -import type { EmojiType } from '../modules/internet'; +import type { EmojiType, HTTPStatusCodeType } from '../modules/internet'; import type { LocaleEntry } from './definitions'; /** @@ -21,4 +21,8 @@ export type InternetDefinitions = LocaleEntry<{ * List of all fully-qualified emojis. */ emoji: Record<EmojiType, string[]>; + /** + * List of some HTTP status codes. + */ + http_status_code: Record<HTTPStatusCodeType, number[]>; }>; diff --git a/src/locales/en/internet/http_status_code.ts b/src/locales/en/internet/http_status_code.ts new file mode 100644 index 00000000..8027f6f3 --- /dev/null +++ b/src/locales/en/internet/http_status_code.ts @@ -0,0 +1,12 @@ +// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status + +export default { + informational: [100, 101, 102, 103], + success: [200, 201, 202, 203, 204, 205, 206, 207, 208, 226], + redirection: [300, 301, 302, 303, 304, 305, 306, 307, 308], + clientError: [ + 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, + 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, + ], + serverError: [500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511], +}; diff --git a/src/locales/en/internet/index.ts b/src/locales/en/internet/index.ts index 9da00b43..6ee266fb 100644 --- a/src/locales/en/internet/index.ts +++ b/src/locales/en/internet/index.ts @@ -8,6 +8,7 @@ import domain_suffix from './domain_suffix'; import emoji from './emoji'; import example_email from './example_email'; import free_email from './free_email'; +import http_status_code from './http_status_code'; const internet: InternetDefinitions = { avatar_uri, @@ -15,6 +16,7 @@ const internet: InternetDefinitions = { emoji, example_email, free_email, + http_status_code, }; export default internet; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 7e0baaec..f08ff321 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -13,6 +13,13 @@ export type EmojiType = | 'symbol' | 'flag'; +export type HTTPStatusCodeType = + | 'informational' + | 'success' + | 'clientError' + | 'serverError' + | 'redirection'; + /** * Module to generate internet related entries. */ @@ -181,6 +188,30 @@ export class Internet { } /** + * Generates a random HTTP status code. + * + * @param options Options object. + * @param options.types A list of the HTTP status code types that should be used. + * + * @example + * faker.internet.httpStatusCode() // 200 + * faker.internet.httpStatusCode({ types: ['success', 'serverError'] }) // 500 + */ + httpStatusCode( + options: { types?: ReadonlyArray<HTTPStatusCodeType> } = {} + ): number { + const { + types = Object.keys( + this.faker.definitions.internet.http_status_code + ) as Array<HTTPStatusCodeType>, + } = options; + const httpStatusCodeType = this.faker.helpers.arrayElement(types); + return this.faker.helpers.arrayElement( + this.faker.definitions.internet.http_status_code[httpStatusCodeType] + ); + } + + /** * Generates a random url. * * @example diff --git a/test/internet.spec.ts b/test/internet.spec.ts index c12284e2..313be914 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -14,6 +14,7 @@ const seededRuns = [ userName: 'Garnett.Schinner73', protocol: 'http', httpMethod: 'POST', + httpStatusCode: 207, url: 'http://stable-vehicle.biz', domainName: 'harmonious-shift.org', domainSuffix: 'info', @@ -39,6 +40,7 @@ const seededRuns = [ userName: 'Devyn21', protocol: 'http', httpMethod: 'POST', + httpStatusCode: 205, url: 'http://neat-chopsticks.biz', domainName: 'fabulous-might.com', domainSuffix: 'biz', @@ -64,6 +66,7 @@ const seededRuns = [ userName: 'Tito_Koch22', protocol: 'https', httpMethod: 'PATCH', + httpStatusCode: 505, url: 'https://joyous-temperature.net', domainName: 'verifiable-infection.org', domainSuffix: 'org', @@ -90,6 +93,7 @@ const functionNames = [ 'userName', 'protocol', 'httpMethod', + 'httpStatusCode', 'url', 'domainName', 'domainSuffix', @@ -340,6 +344,38 @@ describe('internet', () => { }); }); + describe('httpStatusCode', () => { + it('should return a random HTTP status code', () => { + const httpStatusCode = faker.internet.httpStatusCode(); + + expect(httpStatusCode).toBeTruthy(); + expect(httpStatusCode).toBeTypeOf('number'); + expect(httpStatusCode).toBeLessThanOrEqual(600); + }); + + it('should return a correct status code for multiple classes', () => { + const httpStatusCode = faker.internet.httpStatusCode({ + types: ['informational', 'success', 'redirection'], + }); + + expect(httpStatusCode).toBeTruthy(); + expect(httpStatusCode).toBeTypeOf('number'); + expect(httpStatusCode).toBeGreaterThanOrEqual(100); + expect(httpStatusCode).toBeLessThan(400); + }); + + it('should return a correct status code for a single class', () => { + const httpStatusCode = faker.internet.httpStatusCode({ + types: ['serverError'], + }); + + expect(httpStatusCode).toBeTruthy(); + expect(httpStatusCode).toBeTypeOf('number'); + expect(httpStatusCode).toBeGreaterThanOrEqual(500); + expect(httpStatusCode).toBeLessThan(600); + }); + }); + describe('url()', () => { it('should return a valid url', () => { const url = faker.internet.url(); |
