Skip to content
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

feat: add API response headers to the returned response #692

Merged
merged 1 commit into from
Sep 13, 2021
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
1 change: 1 addition & 0 deletions lib/base/RequestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ RequestClient.prototype.request = function (opts) {
deferred.resolve({
statusCode: response.status,
body: response.data,
headers: response.headers,
});
}).catch((error) => {
_this.lastResponse = undefined;
Expand Down
68 changes: 39 additions & 29 deletions spec/unit/base/RequestClient.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
var mockfs = require('mock-fs');
var proxyquire = require('proxyquire');
var Q = require('q');
'use strict';

const mockfs = require('mock-fs');
const proxyquire = require('proxyquire');
const Q = require('q');

describe('lastResponse and lastRequest defined', function() {
var client;
let client;
let response;
beforeEach(function() {
RequestClientMock = proxyquire('../../../lib/base/RequestClient', {
const RequestClientMock = proxyquire('../../../lib/base/RequestClient', {
axios: function(options) {
var deferred = Q.defer();
deferred.resolve({status: 200, data: 'voltron'});
const deferred = Q.defer();
deferred.resolve({status: 200, data: 'voltron', headers: {response: 'header'}});
return deferred.promise;
},
});

client = new RequestClientMock();

var options = {
const options = {
method: 'GET',
uri: 'test-uri',
username: 'test-username',
Expand All @@ -25,8 +28,9 @@ describe('lastResponse and lastRequest defined', function() {
data: {'test-data-key': 'test-data-value'},
};

return client.request(options);
response = client.request(options);

return response;
});

it('should have lastResponse and lastRequest on success', function() {
Expand All @@ -37,29 +41,36 @@ describe('lastResponse and lastRequest defined', function() {
expect(client.lastRequest.params).toEqual({'test-param-key': 'test-param-value'});
expect(client.lastRequest.headers).toEqual({
'test-header-key': 'test-header-value',
'Authorization': 'Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk',
'Connection': 'close',
Authorization: 'Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk',
Connection: 'close',
});
expect(client.lastRequest.data).toEqual({'test-data-key': 'test-data-value'});
expect(client.lastResponse).toBeDefined();
expect(client.lastResponse.statusCode).toEqual(200);
expect(client.lastResponse.body).toEqual('voltron');
});

it('should include response properties', function () {
response.then(function (resp) {
expect(resp.statusCode).toBeDefined();
expect(resp.body).toBeDefined();
expect(resp.headers).toBeDefined();
});
});

it('should not include request authorization header in filtered headers', function () {
const filteredHeaderKeys = client.filterLoggingHeaders(client.lastRequest.headers);
expect(filteredHeaderKeys).toEqual(['test-header-key', 'Connection'])
expect(filteredHeaderKeys).toEqual(['test-header-key', 'Connection']);
});

});

describe('lastRequest defined, lastResponse undefined', function() {
var client;
var options;
let client;
let options;
beforeEach(function() {
RequestClientMock = proxyquire('../../../lib/base/RequestClient', {
const RequestClientMock = proxyquire('../../../lib/base/RequestClient', {
axios: function(options) {
var deferred = Q.defer();
const deferred = Q.defer();
deferred.reject('failed');
return deferred.promise;
},
Expand Down Expand Up @@ -87,23 +98,22 @@ describe('lastRequest defined, lastResponse undefined', function() {
expect(client.lastRequest.params).toEqual({'test-param-key': 'test-param-value'});
expect(client.lastRequest.headers).toEqual({
'test-header-key': 'test-header-value',
'Authorization': 'Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk',
'Connection': 'close',
Authorization: 'Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk',
Connection: 'close',
});
expect(client.lastRequest.data).toEqual({'test-data-key': 'test-data-value'});
expect(client.lastResponse).toBeUndefined();
});
});

});

describe('User specified CA bundle', function() {
var client;
var options;
let client;
let options;
beforeEach(function() {
RequestClientMock = proxyquire('../../../lib/base/RequestClient', {
const RequestClientMock = proxyquire('../../../lib/base/RequestClient', {
axios: function (options) {
var deferred = Q.defer();
const deferred = Q.defer();
deferred.reject('failed');
return deferred.promise;
},
Expand All @@ -118,13 +128,13 @@ describe('User specified CA bundle', function() {
password: 'test-password',
headers: {'test-header-key': 'test-header-value'},
params: {'test-param-key': 'test-param-value'},
data: {'test-data-key': 'test-data-value'}
data: {'test-data-key': 'test-data-value'},
};

mockfs({
'/path/to/ca': {
'test-ca.pem': 'test ca data'
}
'test-ca.pem': 'test ca data',
},
});
});

Expand All @@ -151,8 +161,8 @@ describe('User specified CA bundle', function() {
return client.request(options).catch(() => {
mockfs({
'/path/to/ca': {
'test-ca.pem': null
}
'test-ca.pem': null,
},
});
return client.request(options).catch(() => {
expect(client.lastRequest.ca.toString()).toEqual('test ca data');
Expand Down