Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions templates/swagger/node.js.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,33 @@ module.exports = function (RED) {

{{#isSecure}}
{{#isSecureToken}}
if (this.service.secureTokenIsQuery) {
client.setToken(this.service.credentials.secureTokenValue,
this.service.secureTokenHeaderOrQueryName, true);
} else {
client.setToken(this.service.credentials.secureTokenValue,
this.service.secureTokenHeaderOrQueryName, false);
if (this.service && this.service.credentials && this.service.credentials.secureTokenValue) {
if (this.service.secureTokenIsQuery) {
client.setToken(this.service.credentials.secureTokenValue,
this.service.secureTokenHeaderOrQueryName, true);
} else {
client.setToken(this.service.credentials.secureTokenValue,
this.service.secureTokenHeaderOrQueryName, false);
}
}
{{/isSecureToken}}

{{#isSecureApiKey}}
if (this.service.secureApiKeyIsQuery) {
client.setApiKey(this.service.credentials.secureApiKeyValue,
this.service.secureApiKeyHeaderOrQueryName, true);
} else {
client.setApiKey(this.service.credentials.secureApiKeyValue,
this.service.secureApiKeyHeaderOrQueryName, false);
if (this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
if (this.service.secureApiKeyIsQuery) {
client.setApiKey(this.service.credentials.secureApiKeyValue,
this.service.secureApiKeyHeaderOrQueryName, true);
} else {
client.setApiKey(this.service.credentials.secureApiKeyValue,
this.service.secureApiKeyHeaderOrQueryName, false);
}
}
{{/isSecureApiKey}}

{{#isSecureBasic}}
client.setBasicAuth(this.service.credentials.username, this.service.credentials.password);
if (this.service && this.service.credentials) {
client.setBasicAuth(this.service.credentials.username, this.service.credentials.password);
}
{{/isSecureBasic}}
{{/isSecure}}

Expand Down Expand Up @@ -75,7 +81,7 @@ module.exports = function (RED) {
if (!errorFlag) {
node.status({ fill: "blue", shape: "dot", text: "{{&className}}.status.requesting" });
result.then(function (response) {
if (response.body !== null) {
if (response.body !== null && response.body !== undefined) {
msg.payload = response.body;
}
node.send(msg);
Expand Down
218 changes: 218 additions & 0 deletions test/nodegen/node-red-contrib-swagger-petstore/node_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,168 @@ describe('node-red-contrib-swagger-petstore', function () {
done();
});
});
it('should handle addPet()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "addPet"},
{id: "n2", type: "swagger-petstore-service"},
{id: "n3", type: "helper"}];
helper.load(swaggerNode, flow, function () {
var n1 = helper.getNode('n1');
var n3 = helper.getNode('n3');
n3.on('input', function (msg) {
try {
msg.payload.should.eql({
"id": 4513,
"category": {
"id": 4649,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 2525,
"name": "string"
}
],
"status": "available"
});
done();
} catch (e) {
done(e);
}
});
n1.receive({
payload: {
"id": 4513,
"category": {
"id": 4649,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 2525,
"name": "string"
}
],
"status": "available"
}
});
});
});
it('should handle updatePet()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "updatePet"},
{id: "n2", type: "swagger-petstore-service"},
{id: "n3", type: "helper"}];
helper.load(swaggerNode, flow, function () {
var n1 = helper.getNode('n1');
var n3 = helper.getNode('n3');
n3.on('input', function (msg) {
try {
msg.payload.should.eql({
"id": 4513,
"category": {
"id": 5963,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 3341,
"name": "string"
}
],
"status": "available"
});
done();
} catch (e) {
done(e);
}
});
n1.receive({
payload: {
"id": 4513,
"category": {
"id": 5963,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 3341,
"name": "string"
}
],
"status": "available"
}
});
});
});
it('should handle findPetsByStatus()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "findPetsByStatus", findPetsByStatus_status: "available"},
{id: "n2", type: "swagger-petstore-service"},
{id: "n3", type: "helper"}];
helper.load(swaggerNode, flow, function () {
var n1 = helper.getNode('n1');
var n3 = helper.getNode('n3');
n3.on('input', function (msg) {
try {
msg.payload.should.containEql({
"id": 4513,
"category": {
"id": 5963,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 3341,
"name": "string"
}
],
"status": "available"
});
done();
} catch (e) {
done(e);
}
});
n1.receive({});
});
});
it('should handle updatePetWithForm()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "updatePetWithForm", updatePetWithForm_petId: "4513", updatePetWithForm_name: "pending doggie", updatePetWithForm_status: "pending"},
{id: "n2", type: "swagger-petstore-service"},
{id: "n3", type: "helper"}];
helper.load(swaggerNode, flow, function () {
var n1 = helper.getNode('n1');
var n3 = helper.getNode('n3');
n3.on('input', function (msg) {
try {
msg.should.have.property('payload', 'foo');
msg.should.have.property('topic', 'bar');
done();
} catch (e) {
done(e);
}
});
n1.receive({ payload: "foo", topic: "bar" });
});
});
it('should handle getInventory()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "getInventory"},
{id: "n2", type: "swagger-petstore-service"},
Expand All @@ -54,5 +216,61 @@ describe('node-red-contrib-swagger-petstore', function () {
n1.receive({});
});
});
it('should handle createUser()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "createUser"},
{id: "n2", type: "swagger-petstore-service"},
{id: "n3", type: "helper"}];
helper.load(swaggerNode, flow, function () {
var n1 = helper.getNode('n1');
var n3 = helper.getNode('n3');
n3.on('input', function (msg) {
try {
msg.payload.should.eql({
"id": 8110,
"username": "My user name",
"firstName": "My first name",
"lastName": "My last name",
"email": "My e-mail address",
"password": "My password",
"phone": "My phone number",
"userStatus": 0
});
done();
} catch (e) {
done(e);
}
});
n1.receive({
payload: {
"id": 8110,
"username": "My user name",
"firstName": "My first name",
"lastName": "My last name",
"email": "My e-mail address",
"password": "My password",
"phone": "My phone number",
"userStatus": 0
}
});
});
});
it('should handle loginUser()', function (done) {
var flow = [{id: "n1", type: "swagger-petstore", wires: [["n3"]], service: "n2", method: "loginUser", loginUser_username: "My user name", loginUser_password: "My password"},
{id: "n2", type: "swagger-petstore-service"},
{id: "n3", type: "helper"}];
helper.load(swaggerNode, flow, function () {
var n1 = helper.getNode('n1');
var n3 = helper.getNode('n3');
n3.on('input', function (msg) {
try {
msg.payload.should.startWith('logged in user session:');
done();
} catch (e) {
done(e);
}
});
n1.receive({});
});
});
});