Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit 3a7c2e3

Browse files
sf-csarovvernak2539
authored andcommitted
add support for global request options (#88)
1 parent 1a17768 commit 3a7c2e3

5 files changed

Lines changed: 108 additions & 126 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
.*.swp
55
testing.js
66
coverage/
7+
package-lock.json

lib/fuel-rest.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const request = require('request');
1212
const version = require('../package.json').version;
1313

1414
const clone = require('lodash.clone');
15+
const merge = require('lodash.merge');
1516
const isPlainObject = require('lodash.isplainobject');
1617

1718
class FuelRest {
@@ -45,6 +46,7 @@ class FuelRest {
4546
},
4647
options.headers
4748
);
49+
this.globalReqOptions = options.globalReqOptions || {};
4850
}
4951
/**
5052
* Method that makes the api request
@@ -73,7 +75,14 @@ class FuelRest {
7375
});
7476
}
7577
_processRequest(options, callback) {
76-
this.AuthClient.getAccessToken(clone(options.auth))
78+
79+
const requestOptions = merge(
80+
{},
81+
this.globalReqOptions,
82+
options.auth
83+
);
84+
85+
this.AuthClient.getAccessToken(requestOptions)
7786
.then(tokenInfo => {
7887
if (!tokenInfo.accessToken) {
7988
let error = new Error('No access token');
@@ -109,8 +118,12 @@ class FuelRest {
109118
.catch(err => callback(err, null));
110119
}
111120
_makeRequest(consolidatedOpts, callback) {
112-
const requestOptions = consolidatedOpts.req;
113-
121+
const requestOptions = merge(
122+
{},
123+
this.globalReqOptions,
124+
consolidatedOpts.req
125+
);
126+
114127
request(requestOptions, (err, res, body) => {
115128
let parsedBody;
116129
let isResponseJson;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
"sinon": "~6.1.3"
3333
},
3434
"dependencies": {
35-
"fuel-auth": "^3.0.2",
35+
"fuel-auth": "3.1.0",
3636
"lodash.clone": "~4.5.0",
3737
"lodash.isplainobject": "~4.0.4",
38+
"lodash.merge": "^4.6.1",
3839
"request": "~2.88.0"
3940
}
4041
}

test/specs/fn-proxy-support.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2018, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
'use strict';
8+
const expect = require('chai').expect;
9+
const FuelRest = require('../../lib/fuel-rest');
10+
const routes = require('../config').routes;
11+
const http = require('http');
12+
13+
const port = 4550;
14+
const proxyPort = 8888;
15+
const proxyErrorPort = 1234;
16+
const localhost = `http://127.0.0.1:${port}`;
17+
const proxyResponseBody = 'Hello Node JS Server Response';
18+
const requestOptions = {
19+
method: 'GET',
20+
uri: routes.get
21+
};
22+
const server = http.createServer(function (request, response) {
23+
response.writeHead(200, {'Content-Type': 'application/json'});
24+
response.write(proxyResponseBody);
25+
response.end();
26+
});
27+
const accessToken = 'testForRest';
28+
const expiration = 111111111111;
29+
30+
describe('Proxy support', function () {
31+
32+
let restClient, initOptions;
33+
34+
before(done => server.listen(proxyPort, done));
35+
36+
beforeEach(() => {
37+
initOptions = {
38+
auth: {
39+
clientId: 'testing',
40+
clientSecret: 'testing'
41+
},
42+
restEndpoint: localhost,
43+
globalReqOptions: {
44+
proxy: {
45+
host: '127.0.0.1',
46+
protocol: 'http:'
47+
}
48+
}
49+
};
50+
});
51+
52+
it('should respond the proxyResponseBody if proxy option passed correctly', done => {
53+
initOptions.globalReqOptions.proxy.port = proxyPort;
54+
restClient = new FuelRest(initOptions);
55+
restClient.AuthClient.accessToken = accessToken;
56+
restClient.AuthClient.expiration = expiration;
57+
restClient.apiRequest(requestOptions, (err, data) => {
58+
expect(data.body).to.equal(proxyResponseBody);
59+
done();
60+
});
61+
});
62+
63+
it('should error if proxy option passed incorrectly', done => {
64+
initOptions.globalReqOptions.proxy.port = proxyErrorPort;
65+
restClient = new FuelRest(initOptions);
66+
restClient.AuthClient.accessToken = accessToken;
67+
restClient.AuthClient.expiration = expiration;
68+
restClient.apiRequest(requestOptions, (err) => {
69+
expect(err.code).to.equal('ECONNREFUSED');
70+
expect(err.port).to.equal(proxyErrorPort);
71+
done();
72+
});
73+
});
74+
75+
after(() => server.close());
76+
77+
});

0 commit comments

Comments
 (0)