|
| 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