Skip to content

Commit

Permalink
add host change tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLakatos committed Jan 17, 2020
1 parent d12243f commit f005aee
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var config = {
CODE: process.env.CODE || '',
APP_ID: process.env.APP_ID || '',
PRIVATE_KEY: process.env.PRIVATE_KEY || '',
DEBUG: process.env.DEBUG === 'true'
DEBUG: process.env.DEBUG === 'true',
REST_HOST: process.env.REST_HOST || 'rest.nexmo.com',
API_HOST: process.env.API_HOST || 'api.nexmo.com'
};

module.exports = config;
50 changes: 50 additions & 0 deletions examples/ex-change-api-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module.exports = function(callback, config) {

var Nexmo = require('../lib/Nexmo');

var nexmo = new Nexmo({
apiKey: config.API_KEY,
apiSecret: config.API_SECRET
}, {
debug: config.DEBUG,
apiHost: config.API_HOST
});

nexmo.applications.create({
name: 'My nexmo-node Example V2 App',
capabilities: {
voice: {
webhooks: {
answer_url: {
address: "https://example.com",
http_method: "GET"
},
event_url: {
address: "https://example.com",
http_method: "POST"
}
}
},
messages: {
webhooks: {
inbound_url: {
address: "https://example.com",
http_method: "POST"
},
status_url: {
address: "https://example.com",
http_method: "POST"
}
}
},
rtc: {
webhooks: {
event_url: {
address: "https://example.com",
http_method: "POST"
}
}
}
}
}, callback);
};
14 changes: 14 additions & 0 deletions examples/ex-change-rest-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function(callback, config) {

var Nexmo = require('../lib/Nexmo');

var nexmo = new Nexmo({
apiKey: config.API_KEY,
apiSecret: config.API_SECRET
}, {
debug: config.DEBUG,
restHost: config.REST_HOST
});

nexmo.message.sendSms(config.FROM_NUMBER, config.TO_NUMBER, 'testing', callback);
};
2 changes: 2 additions & 0 deletions examples/run-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function runExample(exampleFile, callback) {
// By default all examples are run.
// Use this array to run a select number of examples.
exampleFiles = [
// 'ex-change-api-host.js',
// 'ex-change-rest-host.js',
// 'ex-check-balance.js',
// 'ex-create-update-delete-app.js',
// 'ex-dtmf-to-call.js',
Expand Down
118 changes: 118 additions & 0 deletions test/App-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ describe("applications.create", function() {
null
);
});

it("should support host override", function() {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
applications = new App(creds, options);
applications.create({}, emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "POST",
host: "api.example.com",
path: App.PATH,
headers: {
"Content-Type": "application/json",
Authorization: "Basic "
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback,
emptyCallback,
false,
null
);
});
});

describe("applications.update", function() {
Expand Down Expand Up @@ -265,6 +295,36 @@ describe("applications.update", function() {
null
);
});

it("should support host override", function() {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
applications = new App(creds, options);
applications.update("app_id", {}, emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "PUT",
host: "api.example.com",
path: `${App.PATH}/app_id`,
headers: {
"Content-Type": "application/json",
Authorization: "Basic "
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback,
emptyCallback,
false,
null
);
});
});

describe("applications.get", function() {
Expand Down Expand Up @@ -349,6 +409,37 @@ describe("applications.get", function() {
null
);
});

it("should support host override", function() {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
let applications = new App(creds, options);
applications.get("app_id", emptyCallback, true);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "GET",
host: "api.example.com",
path: `${App.PATH}/app_id`,
body: undefined,
headers: {
"Content-Type": "application/json",
Authorization: "Basic "
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback,
emptyCallback,
false,
null
);
});
});

describe("applications.delete", function() {
Expand Down Expand Up @@ -381,6 +472,33 @@ describe("applications.delete", function() {
emptyCallback
);
});

it("should allow host override", function() {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
let applications = new App(creds, options);
applications.delete("app_id", emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "DELETE",
host: "api.example.com",
path: `${App.PATH}/app_id`,
headers: {
"Content-Type": "application/json",
Authorization: "Basic "
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback
);
});
});

describe("applications._convertMethodSignature", function() {
Expand Down
78 changes: 78 additions & 0 deletions test/CallResource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,84 @@ describe("CallsResource", () => {
calls = new CallsResource(creds, options);
});

it("should support host override for calls.create", () => {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
calls = new CallsResource(creds, options);
calls.create({}, emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "POST",
host: "api.example.com",
headers: {
"Content-Type": "application/json"
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback
);
});

it("should support host override for calls.update", () => {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
calls = new CallsResource(creds, options);
calls.update("call_id", {}, emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "PUT",
host: "api.example.com",
path: `${CallsResource.PATH}/call_id`,
headers: {
"Content-Type": "application/json"
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback
);
});

it("should support host override for calls.update", () => {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
calls = new CallsResource(creds, options);
calls.get("call_id", emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
method: "GET",
host: "api.example.com",
body: undefined,
path: `${CallsResource.PATH}/call_id`,
headers: {
"Content-Type": "application/json"
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback
);
});

it("should allow a call to be created", () => {
var params = {
to: {
Expand Down
27 changes: 27 additions & 0 deletions test/DtmfResource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,31 @@ describe("DtmfResource", () => {
emptyCallback
);
});

it("should support host override", () => {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
let dtmf = new DtmfResource(creds, options);
const callId = "2342342-lkjhlkjh-32423";
dtmf.send(callId, {}, emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(
{},
{
path: DtmfResource.PATH.replace("{call_uuid}", callId),
method: "PUT",
host: "api.example.com",
headers: {
"Content-Type": "application/json"
}
}
);
expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback
);
});
});
27 changes: 27 additions & 0 deletions test/FilesResource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ describe("FileResource", () => {
files = new FilesResource(creds, options);
});

it("should support host override", () => {
let httpClientStub = sinon.createStubInstance(HttpClient);
let options = {
httpClient: httpClientStub,
apiHost: "api.example.com"
};
let files = new FilesResource(creds, options);
const fileId = "2342342-lkjhlkjh-32423";
files.get(fileId, emptyCallback);

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
method: "GET",
body: undefined,
host: "api.example.com",
path: `${FilesResource.PATH}/${fileId}`,
headers: {
"Content-Type": "application/octet-stream",
Authorization: "Bearer "
}
});

expect(httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs),
emptyCallback
);
});

it("should get a single file using a file ID", () => {
const fileId = "2342342-lkjhlkjh-32423";
files.get(fileId, emptyCallback);
Expand Down
Loading

0 comments on commit f005aee

Please sign in to comment.