Skip to content

Commit 05f555b

Browse files
johge201ST-DDTpkuczynskijohge201Sofia Bertmar
authored
feat(internet): HTTP random status code (#945)
Co-authored-by: ST-DDT <ST-DDT@gmx.de> Co-authored-by: Piotr Kuczynski <piotr.kuczynski@gmail.com> Co-authored-by: johge201 <johge201@student.liu.se> Co-authored-by: Sofia Bertmar <sofbe892@student.liu.se> Co-authored-by: Sofia Bertmar <58030654+sofbe@users.noreply.github.com> Co-authored-by: Shinigami <chrissi92@hotmail.de>
1 parent b0cdf29 commit 05f555b

5 files changed

Lines changed: 86 additions & 1 deletion

File tree

src/definitions/internet.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EmojiType } from '../modules/internet';
1+
import type { EmojiType, HTTPStatusCodeType } from '../modules/internet';
22
import type { LocaleEntry } from './definitions';
33

44
/**
@@ -21,4 +21,8 @@ export type InternetDefinitions = LocaleEntry<{
2121
* List of all fully-qualified emojis.
2222
*/
2323
emoji: Record<EmojiType, string[]>;
24+
/**
25+
* List of some HTTP status codes.
26+
*/
27+
http_status_code: Record<HTTPStatusCodeType, number[]>;
2428
}>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
2+
3+
export default {
4+
informational: [100, 101, 102, 103],
5+
success: [200, 201, 202, 203, 204, 205, 206, 207, 208, 226],
6+
redirection: [300, 301, 302, 303, 304, 305, 306, 307, 308],
7+
clientError: [
8+
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414,
9+
415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451,
10+
],
11+
serverError: [500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511],
12+
};

src/locales/en/internet/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import domain_suffix from './domain_suffix';
88
import emoji from './emoji';
99
import example_email from './example_email';
1010
import free_email from './free_email';
11+
import http_status_code from './http_status_code';
1112

1213
const internet: InternetDefinitions = {
1314
avatar_uri,
1415
domain_suffix,
1516
emoji,
1617
example_email,
1718
free_email,
19+
http_status_code,
1820
};
1921

2022
export default internet;

src/modules/internet/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export type EmojiType =
1313
| 'symbol'
1414
| 'flag';
1515

16+
export type HTTPStatusCodeType =
17+
| 'informational'
18+
| 'success'
19+
| 'clientError'
20+
| 'serverError'
21+
| 'redirection';
22+
1623
/**
1724
* Module to generate internet related entries.
1825
*/
@@ -180,6 +187,30 @@ export class Internet {
180187
return this.faker.helpers.arrayElement(httpMethods);
181188
}
182189

190+
/**
191+
* Generates a random HTTP status code.
192+
*
193+
* @param options Options object.
194+
* @param options.types A list of the HTTP status code types that should be used.
195+
*
196+
* @example
197+
* faker.internet.httpStatusCode() // 200
198+
* faker.internet.httpStatusCode({ types: ['success', 'serverError'] }) // 500
199+
*/
200+
httpStatusCode(
201+
options: { types?: ReadonlyArray<HTTPStatusCodeType> } = {}
202+
): number {
203+
const {
204+
types = Object.keys(
205+
this.faker.definitions.internet.http_status_code
206+
) as Array<HTTPStatusCodeType>,
207+
} = options;
208+
const httpStatusCodeType = this.faker.helpers.arrayElement(types);
209+
return this.faker.helpers.arrayElement(
210+
this.faker.definitions.internet.http_status_code[httpStatusCodeType]
211+
);
212+
}
213+
183214
/**
184215
* Generates a random url.
185216
*

test/internet.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const seededRuns = [
1414
userName: 'Garnett.Schinner73',
1515
protocol: 'http',
1616
httpMethod: 'POST',
17+
httpStatusCode: 207,
1718
url: 'http://stable-vehicle.biz',
1819
domainName: 'harmonious-shift.org',
1920
domainSuffix: 'info',
@@ -39,6 +40,7 @@ const seededRuns = [
3940
userName: 'Devyn21',
4041
protocol: 'http',
4142
httpMethod: 'POST',
43+
httpStatusCode: 205,
4244
url: 'http://neat-chopsticks.biz',
4345
domainName: 'fabulous-might.com',
4446
domainSuffix: 'biz',
@@ -64,6 +66,7 @@ const seededRuns = [
6466
userName: 'Tito_Koch22',
6567
protocol: 'https',
6668
httpMethod: 'PATCH',
69+
httpStatusCode: 505,
6770
url: 'https://joyous-temperature.net',
6871
domainName: 'verifiable-infection.org',
6972
domainSuffix: 'org',
@@ -90,6 +93,7 @@ const functionNames = [
9093
'userName',
9194
'protocol',
9295
'httpMethod',
96+
'httpStatusCode',
9397
'url',
9498
'domainName',
9599
'domainSuffix',
@@ -340,6 +344,38 @@ describe('internet', () => {
340344
});
341345
});
342346

347+
describe('httpStatusCode', () => {
348+
it('should return a random HTTP status code', () => {
349+
const httpStatusCode = faker.internet.httpStatusCode();
350+
351+
expect(httpStatusCode).toBeTruthy();
352+
expect(httpStatusCode).toBeTypeOf('number');
353+
expect(httpStatusCode).toBeLessThanOrEqual(600);
354+
});
355+
356+
it('should return a correct status code for multiple classes', () => {
357+
const httpStatusCode = faker.internet.httpStatusCode({
358+
types: ['informational', 'success', 'redirection'],
359+
});
360+
361+
expect(httpStatusCode).toBeTruthy();
362+
expect(httpStatusCode).toBeTypeOf('number');
363+
expect(httpStatusCode).toBeGreaterThanOrEqual(100);
364+
expect(httpStatusCode).toBeLessThan(400);
365+
});
366+
367+
it('should return a correct status code for a single class', () => {
368+
const httpStatusCode = faker.internet.httpStatusCode({
369+
types: ['serverError'],
370+
});
371+
372+
expect(httpStatusCode).toBeTruthy();
373+
expect(httpStatusCode).toBeTypeOf('number');
374+
expect(httpStatusCode).toBeGreaterThanOrEqual(500);
375+
expect(httpStatusCode).toBeLessThan(600);
376+
});
377+
});
378+
343379
describe('url()', () => {
344380
it('should return a valid url', () => {
345381
const url = faker.internet.url();

0 commit comments

Comments
 (0)