Skip to content

Commit

Permalink
feat(internet): Add RFC 5737 testing IPv4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-phillips-nz committed Oct 10, 2023
1 parent 350dfbf commit f774023
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,12 +989,33 @@ export class InternetModule {
/**
* Generates a random IPv4 address.
*
* @param options Optional options object.
* @param options.testRange The test-net range to use. Defaults to undefined, which generates any IP address.
*
* @example
* faker.internet.ipv4() // '245.108.222.0'
* faker.internet.ipv4({ testRange: 1 }) // '192.0.2.32'
* faker.internet.ipv4({ testRange: 2 }) // '198.51.100.59'
* faker.internet.ipv4({ testRange: 3 }) // '203.0.113.97'
*
* @since 6.1.1
*/
ipv4(): string {
ipv4(
options: {
/**
* Which TEST-NET range to use
*
* @default undefined (generates any random IP address)
*/
testRange?: undefined | 1 | 2 | 3;
} = {}
): string {
const { testRange = undefined } = options;
if (testRange) {
const ranges = ['192.0.2', '198.51.100', '203.0.113'];
return `${ranges[testRange - 1]}.${this.faker.number.int(255)}`;
}

return Array.from({ length: 4 }, () => this.faker.number.int(255)).join(
'.'
);
Expand Down
24 changes: 21 additions & 3 deletions test/modules/__snapshots__/internet.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ exports[`internet > 42 > httpStatusCode > with options 1`] = `410`;

exports[`internet > 42 > ip 1`] = `"203.243.46.187"`;

exports[`internet > 42 > ipv4 1`] = `"95.203.243.46"`;
exports[`internet > 42 > ipv4 > noArgs 1`] = `"95.203.243.46"`;

exports[`internet > 42 > ipv4 > with test-net range 1 1`] = `"192.0.2.95"`;

exports[`internet > 42 > ipv4 > with test-net range 2 1`] = `"198.51.100.95"`;

exports[`internet > 42 > ipv4 > with test-net range 3 1`] = `"203.0.113.95"`;

exports[`internet > 42 > ipv6 1`] = `"8be4:abdd:3932:1ad7:d3fe:01ff:ce40:4f4d"`;

Expand Down Expand Up @@ -224,7 +230,13 @@ exports[`internet > 1211 > httpStatusCode > with options 1`] = `429`;

exports[`internet > 1211 > ip 1`] = `"adb4:2f0e:3f4a:973f:ab0a:eefc:e96d:fcf4"`;

exports[`internet > 1211 > ipv4 1`] = `"237.117.228.199"`;
exports[`internet > 1211 > ipv4 > noArgs 1`] = `"237.117.228.199"`;

exports[`internet > 1211 > ipv4 > with test-net range 1 1`] = `"192.0.2.237"`;

exports[`internet > 1211 > ipv4 > with test-net range 2 1`] = `"198.51.100.237"`;

exports[`internet > 1211 > ipv4 > with test-net range 3 1`] = `"203.0.113.237"`;

exports[`internet > 1211 > ipv6 1`] = `"eadb:42f0:e3f4:a973:fab0:aeef:ce96:dfcf"`;

Expand Down Expand Up @@ -366,7 +378,13 @@ exports[`internet > 1337 > httpStatusCode > with options 1`] = `407`;

exports[`internet > 1337 > ip 1`] = `"143.40.54.71"`;

exports[`internet > 1337 > ipv4 1`] = `"67.143.40.54"`;
exports[`internet > 1337 > ipv4 > noArgs 1`] = `"67.143.40.54"`;

exports[`internet > 1337 > ipv4 > with test-net range 1 1`] = `"192.0.2.67"`;

exports[`internet > 1337 > ipv4 > with test-net range 2 1`] = `"198.51.100.67"`;

exports[`internet > 1337 > ipv4 > with test-net range 3 1`] = `"203.0.113.67"`;

exports[`internet > 1337 > ipv6 1`] = `"5c34:6ba0:75bd:57f5:a62b:82d7:2af3:9cbb"`;

Expand Down
51 changes: 50 additions & 1 deletion test/modules/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('internet', () => {
'domainSuffix',
'domainWord',
'ip',
'ipv4',
'ipv6',
'port',
'userAgent'
Expand Down Expand Up @@ -154,6 +153,13 @@ describe('internet', () => {
protocol: 'http',
});
});

t.describe('ipv4', (t) => {
t.it('noArgs')
.it('with test-net range 1', { testRange: 1 })
.it('with test-net range 2', { testRange: 2 })
.it('with test-net range 3', { testRange: 3 });
});
});

describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
Expand Down Expand Up @@ -614,6 +620,49 @@ describe('internet', () => {
expect(+part).toBeLessThanOrEqual(255);
}
});
it('should return a random testing-safe IPv4 with four parts, from test-net range 1', () => {
const ip = faker.internet.ipv4({ testRange: 1 });

expect(ip).toBeTruthy();
expect(ip).toBeTypeOf('string');
expect(ip).toSatisfy((value: string) => validator.isIP(value, 4));

const parts = ip.split('.');

expect(parts).toHaveLength(4);
expect(parts.slice(0, 3).join('.')).toBe('192.0.2');
expect(parts[3]).toMatch(/^\d+$/);
expect(+parts[3]).toBeGreaterThanOrEqual(0);
expect(+parts[3]).toBeLessThanOrEqual(255);
});
it('should return a random testing-safe IPv4 with four parts, from test-net range 2', () => {
const ip = faker.internet.ipv4({ testRange: 2 });

expect(ip).toBeTruthy();
expect(ip).toBeTypeOf('string');
expect(ip).toSatisfy((value: string) => validator.isIP(value, 4));

const parts = ip.split('.');

expect(parts.slice(0, 3).join('.')).toBe('198.51.100');
expect(parts[3]).toMatch(/^\d+$/);
expect(+parts[3]).toBeGreaterThanOrEqual(0);
expect(+parts[3]).toBeLessThanOrEqual(255);
});
it('should return a random testing-safe IPv4 with four parts, from test-net range 3', () => {
const ip = faker.internet.ipv4({ testRange: 3 });

expect(ip).toBeTruthy();
expect(ip).toBeTypeOf('string');
expect(ip).toSatisfy((value: string) => validator.isIP(value, 4));

const parts = ip.split('.');

expect(parts.slice(0, 3).join('.')).toBe('203.0.113');
expect(parts[3]).toMatch(/^\d+$/);
expect(+parts[3]).toBeGreaterThanOrEqual(0);
expect(+parts[3]).toBeLessThanOrEqual(255);
});
});

describe('ipv6()', () => {
Expand Down

0 comments on commit f774023

Please sign in to comment.