forked from tzuryby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
internet_test.ts
216 lines (196 loc) · 5.64 KB
/
internet_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import {
assert,
assertEquals,
assertStrictEquals,
sinon,
} from "./support/test_deps.ts";
import { faker } from "../mod.ts";
const { test } = Deno;
test({
name: "email() returns an email",
fn(): void {
sinon.stub(faker.internet, "userName").returns("Aiden.Harann55");
const email = faker.internet.email("Aiden.Harann55");
const res = email.split("@")[0];
assertEquals(res, "Aiden.Harann55");
faker.internet.userName.restore();
},
});
test({
name: "exampleEmail() returns an email with the correct name",
fn(): void {
sinon.stub(faker.internet, "userName").returns("Aiden.Harann55");
const email = faker.internet.exampleEmail("Aiden.Harann55");
const res = email.split("@")[0];
assertEquals(res, "Aiden.Harann55");
faker.internet.userName.restore();
},
});
test({
name: "exampleEmail() uses the example.[org|com|net] host",
fn(): void {
const email = faker.internet.exampleEmail();
assert(email.match(/@example\.(org|com|net)$/));
},
});
test({
name: "userName() occasionally returns a single firstName",
fn(): void {
sinon.stub(faker.random, "number").returns(0);
sinon.spy(faker.name, "firstName");
const username = faker.internet.userName();
assert(username);
assert(faker.name.firstName.called);
faker.random.number.restore();
faker.name.firstName.restore();
},
});
test({
name:
"userName() occasionally returns a firstName with a period or hyphen and a lastName",
fn(): void {
sinon.stub(faker.random, "number").returns(1);
sinon.spy(faker.name, "firstName");
sinon.spy(faker.name, "lastName");
sinon.spy(faker.random, "arrayElement");
const username = faker.internet.userName();
assert(username);
assert(faker.name.firstName.called);
assert(faker.name.lastName.called);
assert(faker.random.arrayElement.calledWith([".", "_"]));
faker.random.number.restore();
faker.name.firstName.restore();
faker.name.lastName.restore();
faker.random.arrayElement.restore();
},
});
test({
name: "domainName() returns a domainWord plus a random suffix",
fn(): void {
sinon.stub(faker.internet, "domainWord").returns("bar");
sinon.stub(faker.internet, "domainSuffix").returns("net");
const domain_name = faker.internet.domainName();
assertEquals(domain_name, "bar.net");
faker.internet.domainWord.restore();
faker.internet.domainSuffix.restore();
},
});
test({
name: "domainWord() returns a lower-case firstName",
fn(): void {
sinon.stub(faker.name, "firstName").returns("FOO");
const domain_word = faker.internet.domainWord();
assert(domain_word);
assertStrictEquals(domain_word, "foo");
faker.name.firstName.restore();
},
});
test({
name:
"domainWord(), when the firstName contains a apostrophe, should remove the apostrophe",
fn(): void {
sinon.stub(faker.name, "firstName").returns("d'angelo");
const domain_word = faker.internet.domainWord();
assertStrictEquals(domain_word, "dangelo");
faker.name.firstName.restore();
},
});
test({
name: "protocol() returns a valid protocol",
fn(): void {
const protocol = faker.internet.protocol();
assert(protocol);
},
});
test({
name: "protocol() should occasionally return http",
fn(): void {
sinon.stub(faker.random, "number").returns(0);
const protocol = faker.internet.protocol();
assert(protocol);
assertStrictEquals(protocol, "http");
faker.random.number.restore();
},
});
test({
name: "protocol() should occasionally return https",
fn(): void {
sinon.stub(faker.random, "number").returns(1);
const protocol = faker.internet.protocol();
assert(protocol);
assertStrictEquals(protocol, "https");
faker.random.number.restore();
},
});
test({
name: "url() returns a valid url",
fn(): void {
sinon.stub(faker.internet, "protocol").returns("http");
sinon.stub(faker.internet, "domainWord").returns("bar");
sinon.stub(faker.internet, "domainSuffix").returns("net");
const url = faker.internet.url();
assert(url);
assertStrictEquals(url, "http://bar.net");
},
});
test({
name: "ip() returns a random IP address with four parts",
fn(): void {
const ip = faker.internet.ip();
const parts = ip.split(".");
assertEquals(parts.length, 4);
},
});
test({
name: "ipv6() returns a random IPv6 address with eight parts",
fn(): void {
const ip = faker.internet.ipv6();
const parts = ip.split(":");
assertEquals(parts.length, 8);
},
});
test({
name: "userAgent() returns a valid user-agent",
fn(): void {
const ua = faker.internet.userAgent();
assert(ua);
},
});
test({
name: "color() returns a valid hex value (like #ffffff)",
fn(): void {
const color = faker.internet.color(100, 100, 100);
assert(color.match(/^#[a-f0-9]{6}$/));
},
});
test({
name: "mac() returns a random MAC address with 6 hexadecimal digits",
fn(): void {
const mac = faker.internet.mac();
assert(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
},
});
test({
name: "mac() uses the dash separator if we pass it in as our separator",
fn(): void {
const mac = faker.internet.mac("-");
assert(mac.match(/^([a-f0-9]{2}-){5}[a-f0-9]{2}$/));
},
});
test({
name: "mac() uses no separator if we pass in an empty string",
fn(): void {
const mac = faker.internet.mac("");
assert(mac.match(/^[a-f0-9]{12}$/));
},
});
test({
name:
"mac() uses the default colon (:) if we provide an unacceptable separator",
fn(): void {
let mac = faker.internet.mac("!");
assert(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
mac = faker.internet.mac("&");
assert(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
},
});