From 235d9db9b96c20cbc9a468daddc7629a4a97fa02 Mon Sep 17 00:00:00 2001 From: johge201 Date: Wed, 11 May 2022 14:34:00 +0200 Subject: [PATCH 01/13] feat(internet): add http status code generator --- package.json | 1 + src/definitions/internet.ts | 6 +++++- src/locales/en/internet/index.ts | 2 ++ src/locales/en/internet/status_code.ts | 8 +++++++ src/modules/internet/index.ts | 29 ++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/locales/en/internet/status_code.ts diff --git a/package.json b/package.json index 1f084336747..52bf60f9631 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ }, "devDependencies": { "@algolia/client-search": "~4.13.0", + "@faker-js/faker": "^6.3.1", "@types/markdown-it": "~12.2.3", "@types/node": "~17.0.31", "@types/prettier": "~2.6.0", diff --git a/src/definitions/internet.ts b/src/definitions/internet.ts index 96a79a76ac1..967c765f1bb 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; + /** + * List of some HTTP status codes. + */ + status_code: Record; }>; diff --git a/src/locales/en/internet/index.ts b/src/locales/en/internet/index.ts index 9da00b435a1..7f6eaddf433 100644 --- a/src/locales/en/internet/index.ts +++ b/src/locales/en/internet/index.ts @@ -8,11 +8,13 @@ import domain_suffix from './domain_suffix'; import emoji from './emoji'; import example_email from './example_email'; import free_email from './free_email'; +import status_code from './status_code'; const internet: InternetDefinitions = { avatar_uri, domain_suffix, emoji, + status_code, example_email, free_email, }; diff --git a/src/locales/en/internet/status_code.ts b/src/locales/en/internet/status_code.ts new file mode 100644 index 00000000000..e3766d631be --- /dev/null +++ b/src/locales/en/internet/status_code.ts @@ -0,0 +1,8 @@ +// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status + +export default { + success: ['200', '201', '202', '203', '204'], + redirection: ['300', '301', '302', '303', '304'], + clientError: ['400', '401', '402', '403', '404', '418'], + serverError: ['500', '501', '502', '503', '504'], +}; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index bcc5daf3cda..f15993d483e 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -13,6 +13,12 @@ export type EmojiType = | 'symbol' | 'flag'; +export type HTTPStatusCodeType = + | 'success' + | 'clientError' + | 'serverError' + | 'redirection'; + /** * Module to generate internet related entries. */ @@ -455,4 +461,27 @@ export class Internet { this.faker.definitions.internet.emoji[emojiType] ); } + + /** + * Generates a random status code. + * + * @param options Options object. + * @param options.types A list of the HTTP status code types that should be used. + * @example + * faker.internet.status_code() // '200' + * faker.internet.status_code({ types: ['success', 'serverError'] }) // '400' + */ + status_code( + options: { types?: ReadonlyArray } = {} + ): string { + const { + types = Object.keys( + this.faker.definitions.internet.status_code + ) as Array, + } = options; + const httpStatusCodeType = this.faker.helpers.arrayElement(types); + return this.faker.helpers.arrayElement( + this.faker.definitions.internet.status_code[httpStatusCodeType] + ); + } } From be56f6c4a0f4c9fdd41e1893adca5c241b8ef5d9 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Wed, 11 May 2022 15:52:54 +0200 Subject: [PATCH 02/13] feat(internet): add tests for random HTTP status code --- src/locales/en/internet/index.ts | 2 +- src/locales/ro/address/index.ts | 2 +- src/modules/internet/index.ts | 6 +++--- test/internet.spec.ts | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/locales/en/internet/index.ts b/src/locales/en/internet/index.ts index 7f6eaddf433..55ef090f86f 100644 --- a/src/locales/en/internet/index.ts +++ b/src/locales/en/internet/index.ts @@ -14,9 +14,9 @@ const internet: InternetDefinitions = { avatar_uri, domain_suffix, emoji, - status_code, example_email, free_email, + status_code, }; export default internet; diff --git a/src/locales/ro/address/index.ts b/src/locales/ro/address/index.ts index 5698ded14d5..2ff1dcfd176 100644 --- a/src/locales/ro/address/index.ts +++ b/src/locales/ro/address/index.ts @@ -25,10 +25,10 @@ const address: AddressDefinitions = { secondary_address, state, state_abbr, - streets, street_address, street_name, street_suffix, + streets, }; export default address; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index f15993d483e..5c79a707d59 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -468,10 +468,10 @@ export class Internet { * @param options Options object. * @param options.types A list of the HTTP status code types that should be used. * @example - * faker.internet.status_code() // '200' - * faker.internet.status_code({ types: ['success', 'serverError'] }) // '400' + * faker.internet.statusCode() // '200' + * faker.internet.statusCode({ types: ['success', 'serverError'] }) // '400' */ - status_code( + statusCode( options: { types?: ReadonlyArray } = {} ): string { const { diff --git a/test/internet.spec.ts b/test/internet.spec.ts index c12284e221d..c89127589bf 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -27,6 +27,7 @@ const seededRuns = [ mac: '5c:f2:bc:99:27:21', password: 'Dl2fkYYKLsZdepz', emoji: '🕸️', + statusCode: '303', }, }, { @@ -52,6 +53,7 @@ const seededRuns = [ mac: '48:23:48:70:53:89', password: '9V05TL7RY9fmECg', emoji: '💇🏼‍♀️', + statusCode: '302', }, }, { @@ -77,6 +79,7 @@ const seededRuns = [ mac: 'e7:ec:32:f0:a2:a3', password: 'yLuj60b5iHB0bhn', emoji: '🇮🇸', + statusCode: '502', }, }, ]; @@ -102,6 +105,7 @@ const functionNames = [ 'mac', 'password', 'emoji', + 'statusCode', ]; describe('internet', () => { @@ -590,6 +594,16 @@ describe('internet', () => { expect(emoji.length).toBeGreaterThanOrEqual(1); }); }); + + describe('statusCode', () => { + it('should return a random HTTP status code', () => { + const statusCode = faker.internet.statusCode(); + + expect(statusCode).toBeTruthy(); + expect(statusCode).toBeTypeOf('string'); + expect(statusCode).toHaveLength(3); + }); + }); } }); }); From 8ca0ec8d250f424ed14367530185d86a64505b63 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Wed, 11 May 2022 16:28:01 +0200 Subject: [PATCH 03/13] fix: remove accidental changes --- package.json | 1 - src/locales/ro/address/index.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 52bf60f9631..1f084336747 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,6 @@ }, "devDependencies": { "@algolia/client-search": "~4.13.0", - "@faker-js/faker": "^6.3.1", "@types/markdown-it": "~12.2.3", "@types/node": "~17.0.31", "@types/prettier": "~2.6.0", diff --git a/src/locales/ro/address/index.ts b/src/locales/ro/address/index.ts index 2ff1dcfd176..5698ded14d5 100644 --- a/src/locales/ro/address/index.ts +++ b/src/locales/ro/address/index.ts @@ -25,10 +25,10 @@ const address: AddressDefinitions = { secondary_address, state, state_abbr, + streets, street_address, street_name, street_suffix, - streets, }; export default address; From b165e4a82adc1da0fad42013df00f2082cbc23b1 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Thu, 12 May 2022 08:48:59 +0200 Subject: [PATCH 04/13] fix: change string to number for HTTP status codes --- src/definitions/internet.ts | 2 +- src/locales/en/internet/status_code.ts | 8 ++++---- src/modules/internet/index.ts | 2 +- test/internet.spec.ts | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/definitions/internet.ts b/src/definitions/internet.ts index 967c765f1bb..33bf5caeb35 100644 --- a/src/definitions/internet.ts +++ b/src/definitions/internet.ts @@ -24,5 +24,5 @@ export type InternetDefinitions = LocaleEntry<{ /** * List of some HTTP status codes. */ - status_code: Record; + status_code: Record; }>; diff --git a/src/locales/en/internet/status_code.ts b/src/locales/en/internet/status_code.ts index e3766d631be..5cb3005fc76 100644 --- a/src/locales/en/internet/status_code.ts +++ b/src/locales/en/internet/status_code.ts @@ -1,8 +1,8 @@ // Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status export default { - success: ['200', '201', '202', '203', '204'], - redirection: ['300', '301', '302', '303', '304'], - clientError: ['400', '401', '402', '403', '404', '418'], - serverError: ['500', '501', '502', '503', '504'], + success: [200, 201, 202, 203, 204], + redirection: [300, 301, 302, 303, 304], + clientError: [400, 401, 402, 403, 404, 418], + serverError: [500, 501, 502, 503, 504], }; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 5c79a707d59..1b956ee83e0 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -473,7 +473,7 @@ export class Internet { */ statusCode( options: { types?: ReadonlyArray } = {} - ): string { + ): number { const { types = Object.keys( this.faker.definitions.internet.status_code diff --git a/test/internet.spec.ts b/test/internet.spec.ts index c89127589bf..8338468e3e2 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -27,7 +27,7 @@ const seededRuns = [ mac: '5c:f2:bc:99:27:21', password: 'Dl2fkYYKLsZdepz', emoji: '🕸️', - statusCode: '303', + statusCode: 303, }, }, { @@ -53,7 +53,7 @@ const seededRuns = [ mac: '48:23:48:70:53:89', password: '9V05TL7RY9fmECg', emoji: '💇🏼‍♀️', - statusCode: '302', + statusCode: 302, }, }, { @@ -79,7 +79,7 @@ const seededRuns = [ mac: 'e7:ec:32:f0:a2:a3', password: 'yLuj60b5iHB0bhn', emoji: '🇮🇸', - statusCode: '502', + statusCode: 502, }, }, ]; @@ -600,8 +600,8 @@ describe('internet', () => { const statusCode = faker.internet.statusCode(); expect(statusCode).toBeTruthy(); - expect(statusCode).toBeTypeOf('string'); - expect(statusCode).toHaveLength(3); + expect(statusCode).toBeTypeOf('number'); + expect(statusCode).toBeLessThanOrEqual(600); }); }); } From 077902d0778c22ca0d96505f760a2a94988dabb4 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Thu, 12 May 2022 10:21:32 +0200 Subject: [PATCH 05/13] fix: updated documentation --- src/modules/internet/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 1b956ee83e0..26f4414f27f 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -468,8 +468,8 @@ export class Internet { * @param options Options object. * @param options.types A list of the HTTP status code types that should be used. * @example - * faker.internet.statusCode() // '200' - * faker.internet.statusCode({ types: ['success', 'serverError'] }) // '400' + * faker.internet.statusCode() // 200 + * faker.internet.statusCode({ types: ['success', 'serverError'] }) // 400 */ statusCode( options: { types?: ReadonlyArray } = {} From fe04821db20f61a17e91d1dca8daa05a94718fe2 Mon Sep 17 00:00:00 2001 From: johge201 Date: Thu, 12 May 2022 14:18:35 +0200 Subject: [PATCH 06/13] test: add test to validate status type --- test/internet.spec.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/internet.spec.ts b/test/internet.spec.ts index 8338468e3e2..7732c8ad5e1 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -603,6 +603,28 @@ describe('internet', () => { expect(statusCode).toBeTypeOf('number'); expect(statusCode).toBeLessThanOrEqual(600); }); + + it('should return a correct status code for multiple classes', () => { + const statusCode = faker.internet.statusCode({ + types: ['success', 'redirection'], + }); + + expect(statusCode).toBeTruthy(); + expect(statusCode).toBeTypeOf('number'); + expect(statusCode).toBeGreaterThanOrEqual(200); + expect(statusCode).toBeLessThan(400); + }); + + it('should return a correct status code for a single class', () => { + const statusCode = faker.internet.statusCode({ + types: ['serverError'], + }); + + expect(statusCode).toBeTruthy(); + expect(statusCode).toBeTypeOf('number'); + expect(statusCode).toBeGreaterThanOrEqual(500); + expect(statusCode).toBeLessThan(600); + }); }); } }); From 287aac66a6ec47f35dd8e19ad81281981d3b10b3 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar <58030654+sofbe@users.noreply.github.com> Date: Thu, 12 May 2022 14:52:21 +0200 Subject: [PATCH 07/13] Update src/modules/internet/index.ts Co-authored-by: ST-DDT --- src/modules/internet/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 26f4414f27f..bbc3ea91d3d 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -469,7 +469,7 @@ export class Internet { * @param options.types A list of the HTTP status code types that should be used. * @example * faker.internet.statusCode() // 200 - * faker.internet.statusCode({ types: ['success', 'serverError'] }) // 400 + * faker.internet.statusCode({ types: ['success', 'serverError'] }) // 500 */ statusCode( options: { types?: ReadonlyArray } = {} From 3a191dc0048d94f5b05dfc437600f616021a8527 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Mon, 16 May 2022 10:36:31 +0200 Subject: [PATCH 08/13] fix: renamed to httpStatusCode and add information status code --- .../{status_code.ts => http_status_code.ts} | 1 + src/locales/en/internet/index.ts | 4 +- src/locales/ro/address/index.ts | 2 +- src/modules/internet/index.ts | 11 ++--- test/internet.spec.ts | 40 +++++++++---------- 5 files changed, 30 insertions(+), 28 deletions(-) rename src/locales/en/internet/{status_code.ts => http_status_code.ts} (86%) diff --git a/src/locales/en/internet/status_code.ts b/src/locales/en/internet/http_status_code.ts similarity index 86% rename from src/locales/en/internet/status_code.ts rename to src/locales/en/internet/http_status_code.ts index 5cb3005fc76..e93d548363f 100644 --- a/src/locales/en/internet/status_code.ts +++ b/src/locales/en/internet/http_status_code.ts @@ -1,6 +1,7 @@ // Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status export default { + informational: [100, 101, 102, 103], success: [200, 201, 202, 203, 204], redirection: [300, 301, 302, 303, 304], clientError: [400, 401, 402, 403, 404, 418], diff --git a/src/locales/en/internet/index.ts b/src/locales/en/internet/index.ts index 55ef090f86f..6ee266fbb74 100644 --- a/src/locales/en/internet/index.ts +++ b/src/locales/en/internet/index.ts @@ -8,7 +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 status_code from './status_code'; +import http_status_code from './http_status_code'; const internet: InternetDefinitions = { avatar_uri, @@ -16,7 +16,7 @@ const internet: InternetDefinitions = { emoji, example_email, free_email, - status_code, + http_status_code, }; export default internet; diff --git a/src/locales/ro/address/index.ts b/src/locales/ro/address/index.ts index 5698ded14d5..2ff1dcfd176 100644 --- a/src/locales/ro/address/index.ts +++ b/src/locales/ro/address/index.ts @@ -25,10 +25,10 @@ const address: AddressDefinitions = { secondary_address, state, state_abbr, - streets, street_address, street_name, street_suffix, + streets, }; export default address; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index bbc3ea91d3d..dadd19c9d20 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -14,6 +14,7 @@ export type EmojiType = | 'flag'; export type HTTPStatusCodeType = + | 'informational' | 'success' | 'clientError' | 'serverError' @@ -468,20 +469,20 @@ export class Internet { * @param options Options object. * @param options.types A list of the HTTP status code types that should be used. * @example - * faker.internet.statusCode() // 200 - * faker.internet.statusCode({ types: ['success', 'serverError'] }) // 500 + * faker.internet.httpStatusCode() // 200 + * faker.internet.httpStatusCode({ types: ['success', 'serverError'] }) // 500 */ - statusCode( + httpStatusCode( options: { types?: ReadonlyArray } = {} ): number { const { types = Object.keys( - this.faker.definitions.internet.status_code + this.faker.definitions.internet.http_status_code ) as Array, } = options; const httpStatusCodeType = this.faker.helpers.arrayElement(types); return this.faker.helpers.arrayElement( - this.faker.definitions.internet.status_code[httpStatusCodeType] + this.faker.definitions.internet.http_status_code[httpStatusCodeType] ); } } diff --git a/test/internet.spec.ts b/test/internet.spec.ts index 7732c8ad5e1..785295680da 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -27,7 +27,7 @@ const seededRuns = [ mac: '5c:f2:bc:99:27:21', password: 'Dl2fkYYKLsZdepz', emoji: '🕸️', - statusCode: 303, + httpStatusCode: 203, }, }, { @@ -53,7 +53,7 @@ const seededRuns = [ mac: '48:23:48:70:53:89', password: '9V05TL7RY9fmECg', emoji: '💇🏼‍♀️', - statusCode: 302, + httpStatusCode: 202, }, }, { @@ -79,7 +79,7 @@ const seededRuns = [ mac: 'e7:ec:32:f0:a2:a3', password: 'yLuj60b5iHB0bhn', emoji: '🇮🇸', - statusCode: 502, + httpStatusCode: 502, }, }, ]; @@ -105,7 +105,7 @@ const functionNames = [ 'mac', 'password', 'emoji', - 'statusCode', + 'httpStatusCode', ]; describe('internet', () => { @@ -595,35 +595,35 @@ describe('internet', () => { }); }); - describe('statusCode', () => { + describe('httpStatusCode', () => { it('should return a random HTTP status code', () => { - const statusCode = faker.internet.statusCode(); + const httpStatusCode = faker.internet.httpStatusCode(); - expect(statusCode).toBeTruthy(); - expect(statusCode).toBeTypeOf('number'); - expect(statusCode).toBeLessThanOrEqual(600); + expect(httpStatusCode).toBeTruthy(); + expect(httpStatusCode).toBeTypeOf('number'); + expect(httpStatusCode).toBeLessThanOrEqual(600); }); it('should return a correct status code for multiple classes', () => { - const statusCode = faker.internet.statusCode({ - types: ['success', 'redirection'], + const httpStatusCode = faker.internet.httpStatusCode({ + types: ['informational', 'success', 'redirection'], }); - expect(statusCode).toBeTruthy(); - expect(statusCode).toBeTypeOf('number'); - expect(statusCode).toBeGreaterThanOrEqual(200); - expect(statusCode).toBeLessThan(400); + 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 statusCode = faker.internet.statusCode({ + const httpStatusCode = faker.internet.httpStatusCode({ types: ['serverError'], }); - expect(statusCode).toBeTruthy(); - expect(statusCode).toBeTypeOf('number'); - expect(statusCode).toBeGreaterThanOrEqual(500); - expect(statusCode).toBeLessThan(600); + expect(httpStatusCode).toBeTruthy(); + expect(httpStatusCode).toBeTypeOf('number'); + expect(httpStatusCode).toBeGreaterThanOrEqual(500); + expect(httpStatusCode).toBeLessThan(600); }); }); } From 958c5eeb739e89b4a808723fba19b814237dafcf Mon Sep 17 00:00:00 2001 From: Sofia Bertmar <58030654+sofbe@users.noreply.github.com> Date: Mon, 16 May 2022 12:19:16 +0200 Subject: [PATCH 09/13] Update src/modules/internet/index.ts Co-authored-by: ST-DDT --- src/modules/internet/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index dadd19c9d20..e77f14607c9 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -468,6 +468,7 @@ export class Internet { * * @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 From 1b017dc05de48833a3ae71a8365074ebb8372309 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Mon, 16 May 2022 12:32:37 +0200 Subject: [PATCH 10/13] fix: moved httpStatusCode() below httpMethod() --- src/modules/internet/index.ts | 48 +++++++++++------------ test/internet.spec.ts | 72 +++++++++++++++++------------------ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index e77f14607c9..57534ba8948 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -187,6 +187,30 @@ export class Internet { return this.faker.helpers.arrayElement(httpMethods); } + /** + * Generates a random 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 } = {} + ): number { + const { + types = Object.keys( + this.faker.definitions.internet.http_status_code + ) as Array, + } = 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. * @@ -462,28 +486,4 @@ export class Internet { this.faker.definitions.internet.emoji[emojiType] ); } - - /** - * Generates a random 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 } = {} - ): number { - const { - types = Object.keys( - this.faker.definitions.internet.http_status_code - ) as Array, - } = options; - const httpStatusCodeType = this.faker.helpers.arrayElement(types); - return this.faker.helpers.arrayElement( - this.faker.definitions.internet.http_status_code[httpStatusCodeType] - ); - } } diff --git a/test/internet.spec.ts b/test/internet.spec.ts index 785295680da..a53d39e7772 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: 203, url: 'http://stable-vehicle.biz', domainName: 'harmonious-shift.org', domainSuffix: 'info', @@ -27,7 +28,6 @@ const seededRuns = [ mac: '5c:f2:bc:99:27:21', password: 'Dl2fkYYKLsZdepz', emoji: '🕸️', - httpStatusCode: 203, }, }, { @@ -40,6 +40,7 @@ const seededRuns = [ userName: 'Devyn21', protocol: 'http', httpMethod: 'POST', + httpStatusCode: 202, url: 'http://neat-chopsticks.biz', domainName: 'fabulous-might.com', domainSuffix: 'biz', @@ -53,7 +54,6 @@ const seededRuns = [ mac: '48:23:48:70:53:89', password: '9V05TL7RY9fmECg', emoji: '💇🏼‍♀️', - httpStatusCode: 202, }, }, { @@ -66,6 +66,7 @@ const seededRuns = [ userName: 'Tito_Koch22', protocol: 'https', httpMethod: 'PATCH', + httpStatusCode: 502, url: 'https://joyous-temperature.net', domainName: 'verifiable-infection.org', domainSuffix: 'org', @@ -79,7 +80,6 @@ const seededRuns = [ mac: 'e7:ec:32:f0:a2:a3', password: 'yLuj60b5iHB0bhn', emoji: '🇮🇸', - httpStatusCode: 502, }, }, ]; @@ -93,6 +93,7 @@ const functionNames = [ 'userName', 'protocol', 'httpMethod', + 'httpStatusCode', 'url', 'domainName', 'domainSuffix', @@ -105,7 +106,6 @@ const functionNames = [ 'mac', 'password', 'emoji', - 'httpStatusCode', ]; describe('internet', () => { @@ -344,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(); @@ -594,38 +626,6 @@ describe('internet', () => { expect(emoji.length).toBeGreaterThanOrEqual(1); }); }); - - 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); - }); - }); } }); }); From c9bf58327295316c239f19c55c87970813bfa586 Mon Sep 17 00:00:00 2001 From: Sofia Bertmar Date: Tue, 17 May 2022 11:14:30 +0200 Subject: [PATCH 11/13] fix: add all http status codes --- src/locales/en/internet/http_status_code.ts | 11 +++++++---- test/internet.spec.ts | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/locales/en/internet/http_status_code.ts b/src/locales/en/internet/http_status_code.ts index e93d548363f..8027f6f3d82 100644 --- a/src/locales/en/internet/http_status_code.ts +++ b/src/locales/en/internet/http_status_code.ts @@ -2,8 +2,11 @@ export default { informational: [100, 101, 102, 103], - success: [200, 201, 202, 203, 204], - redirection: [300, 301, 302, 303, 304], - clientError: [400, 401, 402, 403, 404, 418], - serverError: [500, 501, 502, 503, 504], + 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/test/internet.spec.ts b/test/internet.spec.ts index a53d39e7772..313be914a1b 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -14,7 +14,7 @@ const seededRuns = [ userName: 'Garnett.Schinner73', protocol: 'http', httpMethod: 'POST', - httpStatusCode: 203, + httpStatusCode: 207, url: 'http://stable-vehicle.biz', domainName: 'harmonious-shift.org', domainSuffix: 'info', @@ -40,7 +40,7 @@ const seededRuns = [ userName: 'Devyn21', protocol: 'http', httpMethod: 'POST', - httpStatusCode: 202, + httpStatusCode: 205, url: 'http://neat-chopsticks.biz', domainName: 'fabulous-might.com', domainSuffix: 'biz', @@ -66,7 +66,7 @@ const seededRuns = [ userName: 'Tito_Koch22', protocol: 'https', httpMethod: 'PATCH', - httpStatusCode: 502, + httpStatusCode: 505, url: 'https://joyous-temperature.net', domainName: 'verifiable-infection.org', domainSuffix: 'org', From 4bc3ebc0308ee85cd6ba6b30022bd4652ea5ed78 Mon Sep 17 00:00:00 2001 From: johge201 <77795252+johge201@users.noreply.github.com> Date: Wed, 18 May 2022 08:11:19 +0200 Subject: [PATCH 12/13] Update src/definitions/internet.ts Co-authored-by: ST-DDT --- src/definitions/internet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/definitions/internet.ts b/src/definitions/internet.ts index 33bf5caeb35..a7801066445 100644 --- a/src/definitions/internet.ts +++ b/src/definitions/internet.ts @@ -24,5 +24,5 @@ export type InternetDefinitions = LocaleEntry<{ /** * List of some HTTP status codes. */ - status_code: Record; + http_status_code: Record; }>; From c36155c5593b5f3659c3a9706e145c5254332064 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Wed, 18 May 2022 21:28:22 +0200 Subject: [PATCH 13/13] Update src/modules/internet/index.ts Co-authored-by: Piotr Kuczynski --- src/modules/internet/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 085dfa84fbd..f08ff32105e 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -188,7 +188,7 @@ export class Internet { } /** - * Generates a random status code. + * 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.