|
| 1 | +const chai = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const express = require('express'); |
| 4 | +const chaiHttp = require('chai-http'); |
| 5 | +const { getProxyURL } = require('../src/service/proxyURL'); |
| 6 | +const config = require('../src/config'); |
| 7 | + |
| 8 | +chai.use(chaiHttp); |
| 9 | +chai.should(); |
| 10 | +const expect = chai.expect; |
| 11 | + |
| 12 | +const genSimpleServer = () => { |
| 13 | + const app = express(); |
| 14 | + app.get('/', (req, res) => { |
| 15 | + res.contentType('text/html'); |
| 16 | + res.send(getProxyURL(req)); |
| 17 | + }); |
| 18 | + return app; |
| 19 | +}; |
| 20 | + |
| 21 | +describe('proxyURL', async () => { |
| 22 | + afterEach(() => { |
| 23 | + sinon.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('pulls the request path with no override', async () => { |
| 27 | + const app = genSimpleServer(); |
| 28 | + const res = await chai.request(app).get('/').send(); |
| 29 | + res.should.have.status(200); |
| 30 | + |
| 31 | + // request url without trailing slash |
| 32 | + const reqURL = res.request.url.slice(0, -1); |
| 33 | + expect(res.text).to.equal(reqURL); |
| 34 | + expect(res.text).to.match(/https?:\/\/127.0.0.1:\d+/); |
| 35 | + }); |
| 36 | + |
| 37 | + it('can override providing a proxy value', async () => { |
| 38 | + const proxyURL = 'https://amazing-proxy.path.local'; |
| 39 | + // stub getDomains |
| 40 | + const configGetDomainsStub = sinon.stub(config, 'getDomains').returns({ proxy: proxyURL }); |
| 41 | + |
| 42 | + const app = genSimpleServer(); |
| 43 | + const res = await chai.request(app).get('/').send(); |
| 44 | + res.should.have.status(200); |
| 45 | + |
| 46 | + // the stub worked |
| 47 | + expect(configGetDomainsStub.calledOnce).to.be.true; |
| 48 | + |
| 49 | + expect(res.text).to.equal(proxyURL); |
| 50 | + }); |
| 51 | +}); |
0 commit comments