/
Twilio.spec.js
169 lines (162 loc) · 6.11 KB
/
Twilio.spec.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
import nock from "nock";
import Twilio from "../../../src/";
describe("client", () => {
let client;
describe("initializing", () => {
it("should use the first arg for the username as well", () => {
client = new Twilio("ACXXXXXXXX", "test-password");
expect(client.username).toEqual("ACXXXXXXXX");
expect(client.accountSid).toEqual("ACXXXXXXXX");
});
it("should use the first arg for the username as well and the option as the accountSid", () => {
client = new Twilio("SKXXXXXXXX", "test-password", {
accountSid: "ACXXXXXXXX",
});
expect(client.username).toEqual("SKXXXXXXXX");
expect(client.accountSid).toEqual("ACXXXXXXXX");
});
it("should throw given an invalid accountSid", () => {
expect(() => new Twilio("ADXXXXXXXX", "test-password")).toThrow(
"must start with"
);
expect(() => new Twilio("SKXXXXXXXX", "test-password")).toThrow(
"API Key"
);
});
});
describe("setting region and edge", () => {
beforeEach(() => {
client = new Twilio("ACXXXXXXXX", "test-password");
});
describe("setting the region", () => {
it("should use no region or edge by default", () => {
const scope = nock("https://api.twilio.com")
.get("/")
.reply(200, "test response");
return client
.request({ method: "GET", uri: "https://api.twilio.com" })
.then(() => scope.done());
});
it("should use the default region if only edge is defined", () => {
const scope = nock("https://api.edge.us1.twilio.com")
.get("/")
.reply(200, "test response");
client.edge = "edge";
return client
.request({ method: "GET", uri: "https://api.twilio.com" })
.then(() => scope.done());
});
it("should use the provided region if only edge is defined and there is a provided region", () => {
const scope = nock("https://api.edge.region.twilio.com")
.get("/")
.reply(200, "test response");
client.edge = "edge";
return client
.request({ method: "GET", uri: "https://api.region.twilio.com" })
.then(() => scope.done());
});
it("should set the region properly if only the region is specified", () => {
const scope = nock("https://api.region.twilio.com")
.get("/")
.reply(200, "test response");
client.region = "region";
return client
.request({ method: "GET", uri: "https://api.twilio.com" })
.then(() => scope.done());
});
it("should set the region and edge properly", () => {
const scope = nock("https://api.edge.region.twilio.com")
.get("/")
.reply(200, "test response");
client.edge = "edge";
client.region = "region";
return client
.request({ method: "GET", uri: "https://api.twilio.com" })
.then(() => scope.done());
});
it("should set the region and edge properly when an edge is already included", () => {
const scope = nock("https://api.edge2.region.twilio.com")
.get("/")
.reply(200, "test response");
client.edge = "edge2";
return client
.request({
method: "GET",
uri: "https://api.edge1.region.twilio.com",
})
.then(() => scope.done());
});
it("should set the region and edge properly when a region is already included", () => {
const scope = nock("https://api.edge.region2.twilio.com")
.get("/")
.reply(200, "test response");
client.region = "region2";
return client
.request({ method: "GET", uri: "https://api.edge.region.twilio.com" })
.then(() => scope.done());
});
it("should set the region properly when a region is already included", () => {
const scope = nock("https://api.region2.twilio.com")
.get("/")
.reply(200, "test response");
client.region = "region2";
return client
.request({ method: "GET", uri: "https://api.region.twilio.com" })
.then(() => scope.done());
});
it("should set the region properly on a custom domain", () => {
const scope = nock("https://api.region2.domain.com")
.get("/")
.reply(200, "test response");
client.region = "region2";
return client
.request({ method: "GET", uri: "https://api.domain.com" })
.then(() => scope.done());
});
it("should set the region properly when a port is included", () => {
const scope = nock("https://api.region.twilio.com:123")
.get("/")
.reply(200, "test response");
client.region = "region";
return client
.request({ method: "GET", uri: "https://api.twilio.com:123" })
.then(() => scope.done());
});
});
});
describe("adding user agent extensions", () => {
it("sets the user-agent by default", () => {
const client = new Twilio("ACXXXXXXXX", "test-password");
const scope = nock("https://api.twilio.com", {
reqheaders: {
"User-Agent":
/^twilio-node\/[0-9.]+(-rc\.[0-9]+)?\s\(\w+\s\w+\)\snode\/[^\s]+$/,
},
})
.get("/")
.reply(200, "test response");
return client
.request({ method: "GET", uri: "https://api.twilio.com" })
.then(() => scope.done());
});
it("allows for user-agent extensions", () => {
const client = new Twilio("ACXXXXXXXX", "test-password", {
userAgentExtensions: [
"twilio-run/2.0.0-test",
"@twilio-labs/plugin-serverless/1.1.0-test",
],
});
const scope = nock("https://api.twilio.com", {
reqheaders: {
"User-Agent":
/^twilio-node\/[0-9.]+(-rc\.[0-9]+)?\s\(\w+\s\w+\)\snode\/[^\s]+ (twilio-run\/2.0.0-test @twilio-labs\/plugin-serverless\/1.1.0-test)$/,
},
})
.get("/")
.reply(200, "test response");
return client
.request({ method: "GET", uri: "https://api.twilio.com" })
.then(() => scope.done());
});
});
});