forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternet.unit.js
203 lines (167 loc) · 6.97 KB
/
internet.unit.js
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
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
}
describe("internet.js", function () {
describe("email()", function () {
it("returns an email", function () {
sinon.stub(faker.internet, 'userName').returns('Aiden.Harann55');
var email = faker.internet.email("Aiden.Harann55");
var res = email.split("@");
res = res[0];
assert.equal(res, 'Aiden.Harann55');
faker.internet.userName.restore();
});
});
describe("exampleEmail", function () {
it("returns an email with the correct name", function () {
sinon.stub(faker.internet, 'userName').returns('Aiden.Harann55');
var email = faker.internet.email("Aiden.Harann55");
var res = email.split("@");
res = res[0];
assert.equal(res, 'Aiden.Harann55');
faker.internet.userName.restore();
});
it("uses the example.[org|com|net] host", function () {
var email = faker.internet.exampleEmail();
assert.ok(email.match(/@example\.(org|com|net)$/));
});
});
describe("userName()", function () {
it("occasionally returns a single firstName", function () {
sinon.stub(faker.random, 'number').returns(0);
sinon.spy(faker.name, 'firstName');
var username = faker.internet.userName();
assert.ok(username);
assert.ok(faker.name.firstName.called);
faker.random.number.restore();
faker.name.firstName.restore();
});
it("occasionally returns a firstName with a period or hyphen and a lastName", function () {
sinon.stub(faker.random, 'number').returns(1);
sinon.spy(faker.name, 'firstName');
sinon.spy(faker.name, 'lastName');
sinon.spy(faker.random, 'arrayElement');
var username = faker.internet.userName();
assert.ok(username);
assert.ok(faker.name.firstName.called);
assert.ok(faker.name.lastName.called);
assert.ok(faker.random.arrayElement.calledWith(['.', '_']));
faker.random.number.restore();
faker.name.firstName.restore();
faker.name.lastName.restore();
faker.random.arrayElement.restore();
});
});
describe("domainName()", function () {
it("returns a domainWord plus a random suffix", function () {
sinon.stub(faker.internet, 'domainWord').returns('bar');
sinon.stub(faker.internet, 'domainSuffix').returns('net');
var domain_name = faker.internet.domainName();
assert.equal(domain_name, 'bar.net');
faker.internet.domainWord.restore();
faker.internet.domainSuffix.restore();
});
});
describe("domainWord()", function () {
it("returns a lower-case firstName", function () {
sinon.stub(faker.name, 'firstName').returns('FOO');
var domain_word = faker.internet.domainWord();
assert.ok(domain_word);
assert.strictEqual(domain_word, 'foo');
faker.name.firstName.restore();
});
describe("when the firstName used contains a apostrophe", function () {
sinon.stub(faker.name, 'firstName').returns('d\'angelo');
var domain_word = faker.internet.domainWord();
it("should remove the apostrophe", function () {
assert.strictEqual(domain_word, 'dangelo');
});
faker.name.firstName.restore();
});
});
describe('protocol()', function () {
it('returns a valid protocol', function () {
var protocol = faker.internet.protocol();
assert.ok(protocol);
});
it('should occasionally return http', function () {
sinon.stub(faker.random, 'number').returns(0);
var protocol = faker.internet.protocol();
assert.ok(protocol);
assert.strictEqual(protocol, 'http');
faker.random.number.restore();
});
it('should occasionally return https', function () {
sinon.stub(faker.random, 'number').returns(1);
var protocol = faker.internet.protocol();
assert.ok(protocol);
assert.strictEqual(protocol, 'https');
faker.random.number.restore();
});
});
describe('url()', function () {
it('returns a valid url', function () {
sinon.stub(faker.internet,'protocol').returns('http');
sinon.stub(faker.internet, 'domainWord').returns('bar');
sinon.stub(faker.internet, 'domainSuffix').returns('net');
var url = faker.internet.url();
assert.ok(url);
assert.strictEqual(url,'http://bar.net');
});
});
describe("ip()", function () {
it("returns a random IP address with four parts", function () {
var ip = faker.internet.ip();
var parts = ip.split('.');
assert.equal(parts.length, 4);
});
});
describe("ipv6()", function () {
it("returns a random IPv6 address with eight parts", function () {
var ip = faker.internet.ipv6();
var parts = ip.split(':');
assert.equal(parts.length, 8);
});
});
describe("userAgent()", function () {
it("returns a valid user-agent", function () {
var ua = faker.internet.userAgent();
assert.ok(ua);
});
it('is deterministic', function () {
faker.seed(1);
var ua1 = faker.internet.userAgent();
faker.seed(1);
var ua2 = faker.internet.userAgent();
assert.equal(ua1, ua2);
});
});
describe("color()", function () {
it("returns a valid hex value (like #ffffff)", function () {
var color = faker.internet.color(100, 100, 100);
assert.ok(color.match(/^#[a-f0-9]{6}$/));
});
});
describe("mac()", function () {
it("returns a random MAC address with 6 hexadecimal digits", function () {
var mac = faker.internet.mac();
assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
});
it("uses the dash separator if we pass it in as our separator", function () {
var mac = faker.internet.mac('-');
assert.ok(mac.match(/^([a-f0-9]{2}-){5}[a-f0-9]{2}$/));
});
it("uses no separator if we pass in an empty string", function() {
var mac = faker.internet.mac('');
assert.ok(mac.match(/^[a-f0-9]{12}$/));
});
it("uses the default colon (:) if we provide an unacceptable separator", function() {
var mac = faker.internet.mac('!');
assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
mac = faker.internet.mac('&');
assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
});
});
});