Skip to content

Commit 9565997

Browse files
author
QuickSander
committed
feat: Handle client errors status code reply
1 parent 32313c8 commit 9565997

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

test/basics.test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('Get power state', function () {
155155
it('replies "true" to Homebridge on valid HTTP GET device response "1"', function () {
156156
// 2. Act
157157
// Call collected HTTP response callback to simulate device response.
158-
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, undefined, '1');
158+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, '1');
159159

160160
// 3. Assert
161161
expect(this.homebridgeCallback.firstCall.args[1]).equals(true);
@@ -165,7 +165,7 @@ describe('Get power state', function () {
165165
it('replies "false" to Homebridge on valid HTTP GET device response "0"', function () {
166166
// 2. Act
167167
// Call collected HTTP response callback to simulate device response.
168-
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, undefined, '0');
168+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, '0');
169169

170170
// 3. Assert
171171
expect(this.homebridgeCallback.firstCall.args[1]).equals(false);
@@ -178,13 +178,26 @@ describe('Get power state', function () {
178178

179179
// 2. Act
180180
// Call collected HTTP response callback to simulate device response.
181-
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, undefined, '{"switch": "on"}');
181+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, '{"switch": "on"}');
182182

183183
// 3. Assert
184184
expect(this.homebridgeCallback.firstCall.args[1]).equals(true);
185185
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['power is currently %s', 'ON']);
186186
});
187187

188+
it('replies an Error object with message "Got HTTP error code 404." to Homebridge on HTTP GET device response status code 404', function () {
189+
// 1. Arrange
190+
const HTTP_ERROR_STATUS_CODE = 404;
191+
192+
// 2. Act
193+
// Call collected HTTP response callback to simulate device response.
194+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: HTTP_ERROR_STATUS_CODE}, 'Dummy error');
195+
196+
// 3. Assert
197+
expect(this.homebridgeCallback.firstCall.args[0]).to.be.instanceOf(Error).and.have.property('message', 'Received HTTP error code '+HTTP_ERROR_STATUS_CODE+': "Dummy error"');
198+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['getPowerState() returned HTTP error code: %s: "%s"', HTTP_ERROR_STATUS_CODE, 'Dummy error']);
199+
});
200+
188201
});
189202

190203

0 commit comments

Comments
 (0)