Skip to content

Commit

Permalink
feat(internet): HTTP random status code (#945)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
7 people committed May 21, 2022
1 parent b0cdf29 commit 05f555b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
6 changes: 5 additions & 1 deletion 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';

/**
Expand All @@ -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[]>;
}>;
12 changes: 12 additions & 0 deletions 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],
};
2 changes: 2 additions & 0 deletions src/locales/en/internet/index.ts
Expand Up @@ -8,13 +8,15 @@ 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,
domain_suffix,
emoji,
example_email,
free_email,
http_status_code,
};

export default internet;
31 changes: 31 additions & 0 deletions src/modules/internet/index.ts
Expand Up @@ -13,6 +13,13 @@ export type EmojiType =
| 'symbol'
| 'flag';

export type HTTPStatusCodeType =
| 'informational'
| 'success'
| 'clientError'
| 'serverError'
| 'redirection';

/**
* Module to generate internet related entries.
*/
Expand Down Expand Up @@ -180,6 +187,30 @@ export class Internet {
return this.faker.helpers.arrayElement(httpMethods);
}

/**
* 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.
*
Expand Down
36 changes: 36 additions & 0 deletions test/internet.spec.ts
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -90,6 +93,7 @@ const functionNames = [
'userName',
'protocol',
'httpMethod',
'httpStatusCode',
'url',
'domainName',
'domainSuffix',
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 05f555b

Please sign in to comment.