Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(internet): HTTP random status code #945

Merged
merged 18 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/definitions/internet.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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