Repository navigation
add more info about device when command error #1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f9f09c8
add license header
AlvaroVega ad961a1
improve error message about command response
AlvaroVega 00b3da6
add more tests
AlvaroVega 1b98ec7
update CNR
AlvaroVega 65223d2
fix linter
AlvaroVega 0b613d1
update CNR
AlvaroVega 53f6aab
update doc
AlvaroVega 4e0aecb
update CNR
AlvaroVega a89307a
add license header
AlvaroVega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U | ||
| * | ||
| * This file is part of fiware-iotagent-lib | ||
| * | ||
| * fiware-iotagent-lib is free software: you can redistribute it and/or | ||
| * modify it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the License, | ||
| * or (at your option) any later version. | ||
| * | ||
| * fiware-iotagent-lib is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public | ||
| * License along with fiware-iotagent-lib. | ||
| * If not, see http://www.gnu.org/licenses/. | ||
| * | ||
| * For those usages not covered by the GNU Affero General Public License | ||
| * please contact with::[contacto@tid.es] | ||
| * | ||
| * | ||
| * | ||
| */ | ||
|
|
||
| const nock = require('nock'); | ||
| const should = require('should'); | ||
| const request = require('../../../lib/request-shim'); | ||
|
|
||
| describe('HTTP request adapter responses', function () { | ||
| const options = { | ||
| url: 'https://command.example.test/nada', | ||
| method: 'POST', | ||
| body: '{"ping":"RRRR"}', | ||
| headers: { 'content-type': 'application/json' } | ||
| }; | ||
|
|
||
| afterEach(function () { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| [200, 201].forEach(function (status) { | ||
| it('should preserve the HTTP ' + status + ' response when JSON parsing fails', function (done) { | ||
| const endpoint = nock('https://command.example.test') | ||
| .post('/nada', { ping: 'RRRR' }) | ||
| .reply(status, 'Hey ya! Greetings!', { 'Content-Type': 'text/plain' }); | ||
|
|
||
| request(options, function (error, response, body) { | ||
| should.exist(error); | ||
| error.code.should.equal('ERR_BODY_PARSE_FAILURE'); | ||
| should.exist(response); | ||
| response.should.equal(error.response); | ||
| response.statusCode.should.equal(status); | ||
| response.headers['content-type'].should.equal('text/plain'); | ||
| body.should.equal('Hey ya! Greetings!'); | ||
| body.should.equal(response.body); | ||
| endpoint.isDone().should.equal(true); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not invent a response when the connection fails', function (done) { | ||
| const endpoint = nock('https://command.example.test') | ||
| .post('/nada', { ping: 'RRRR' }) | ||
| .replyWithError({ code: 'ECONNREFUSED', message: 'Connection refused' }); | ||
|
|
||
| request(options, function (error, response, body) { | ||
| should.exist(error); | ||
| error.code.should.equal('ECONNREFUSED'); | ||
| should(response).equal(undefined); | ||
| should(body).equal(undefined); | ||
| endpoint.isDone().should.equal(true); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should continue returning parsed JSON for a valid response', function (done) { | ||
| const endpoint = nock('https://command.example.test') | ||
| .post('/nada', { ping: 'RRRR' }) | ||
| .reply(200, { ping: 'OK' }); | ||
|
|
||
| request(options, function (error, response, body) { | ||
| should.not.exist(error); | ||
| response.statusCode.should.equal(200); | ||
| body.should.deepEqual({ ping: 'OK' }); | ||
| body.should.equal(response.body); | ||
| endpoint.isDone().should.equal(true); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U | ||
| * | ||
| * This file is part of iotagent-json | ||
| * | ||
| * iotagent-json is free software: you can redistribute it and/or | ||
| * modify it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the License, | ||
| * or (at your option) any later version. | ||
| * | ||
| * iotagent-json is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public | ||
| * License along with iotagent-json. | ||
| * If not, seehttp://www.gnu.org/licenses/. | ||
| * | ||
| * For those usages not covered by the GNU Affero General Public License | ||
| * please contact with::[contacto@tid.es] | ||
| * | ||
| * | ||
| */ | ||
|
|
||
| const nock = require('nock'); | ||
| const should = require('should'); | ||
| const proxyquire = require('proxyquire'); | ||
| const binding = proxyquire('../../../lib/bindings/HTTPBinding', { | ||
| '../configService': { | ||
| getConfig: () => ({ http: {}, iota: {} }) | ||
| } | ||
| }); | ||
|
|
||
| describe('HTTP command execution responses', function () { | ||
| const device = { | ||
| id: 'device1', | ||
| type: 'Thing', | ||
| service: 'smartcity', | ||
| subservice: '/', | ||
| endpoint: 'http://localhost:9876/command' | ||
| }; | ||
|
|
||
| afterEach(function () { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| it('should report HTTP 200 with a plain-text response as a command error', function (done) { | ||
| const commandDevice = { | ||
| ...device, | ||
| id: 'test125', | ||
| endpoint: 'https://command.example.test/nada' | ||
| }; | ||
| const endpoint = nock('https://command.example.test', { | ||
| reqheaders: { | ||
| 'fiware-service': 'smartcity', | ||
| 'fiware-servicepath': '/', | ||
| 'content-type': 'application/json' | ||
| } | ||
| }) | ||
| .post('/nada', { ping: 'RRRR' }) | ||
| .reply(200, 'Hey ya! Greetings!', { 'Content-Type': 'text/plain' }); | ||
|
|
||
| binding.executeCommand( | ||
| null, | ||
| null, | ||
| commandDevice, | ||
| 'ping', | ||
| '{"ping":"RRRR"}', | ||
| null, | ||
| 'application/json', | ||
| function (error) { | ||
| should.exist(error); | ||
| error.code.should.equal(400); | ||
| error.name.should.equal('HTTP_COMMAND_RESPONSE_ERROR'); | ||
| error.message.should.equal( | ||
| 'Command [ping] failed for device [test125]: endpoint returned HTTP 200; invalid JSON response' | ||
| ); | ||
| endpoint.isDone().should.equal(true); | ||
| done(); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| [ | ||
| { status: 200, body: 'Hey ya!', reason: 'invalid JSON' }, | ||
| { status: 500, body: { error: 'Command failed' }, reason: 'HTTP 500' }, | ||
| { status: 503, body: 'Unavailable', reason: 'HTTP 503' }, | ||
| { status: 204, reason: 'HTTP 204' }, | ||
| { error: { code: 'ECONNREFUSED', message: 'Connection refused' }, reason: 'Connection refused' } | ||
| ].forEach(function (testCase) { | ||
| it('should describe command failure for ' + testCase.reason, function (done) { | ||
| const endpoint = nock('http://localhost:9876').post('/command', { ping: 'MMMM' }); | ||
| if (testCase.error) { | ||
| endpoint.replyWithError(testCase.error); | ||
| } else { | ||
| endpoint.reply(testCase.status, testCase.body); | ||
| } | ||
|
|
||
| binding.executeCommand(null, null, device, 'ping', '{"ping":"MMMM"}', null, 'application/json', function ( | ||
| error | ||
| ) { | ||
| should.exist(error); | ||
| error.code.should.equal(400); | ||
| error.name.should.equal('HTTP_COMMAND_RESPONSE_ERROR'); | ||
| error.message.should.containEql('ping'); | ||
| error.message.should.containEql('device1'); | ||
| error.message.should.containEql(testCase.reason); | ||
| if (testCase.status) { | ||
| error.message.should.containEql('HTTP ' + testCase.status); | ||
| } else { | ||
| error.message.should.not.containEql('HTTP 400'); | ||
| } | ||
| should(nock.isDone()).equal(true); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| [200, 201].forEach(function (status) { | ||
| it('should continue accepting HTTP ' + status + ' with JSON', function (done) { | ||
| nock('http://localhost:9876').post('/command').reply(status, { ping: 'OK' }); | ||
| binding.executeCommand(null, null, device, 'ping', '{"ping":"MMMM"}', null, 'application/json', function ( | ||
| error | ||
| ) { | ||
| should.not.exist(error); | ||
| should(nock.isDone()).equal(true); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License header missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done a89307a