|
| 1 | +const authProxy = require('../../lib/handlers/auth-proxy') |
| 2 | +const nock = require('nock') |
| 3 | +const express = require('express') |
| 4 | +const request = require('supertest') |
| 5 | +const { expect } = require('chai') |
| 6 | + |
| 7 | +describe('Auth Proxy', () => { |
| 8 | + describe('An auth proxy with 2 destinations', () => { |
| 9 | + let app |
| 10 | + before(() => { |
| 11 | + nock('http://server-a.org').persist() |
| 12 | + .get(/./).reply(200, addRequestDetails('a')) |
| 13 | + nock('https://server-b.org').persist() |
| 14 | + .get(/./).reply(200, addRequestDetails('b')) |
| 15 | + |
| 16 | + app = express() |
| 17 | + authProxy(app, { |
| 18 | + '/server/a': 'http://server-a.org', |
| 19 | + '/server/b': 'https://server-b.org/foo/bar' |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + describe('responding to /server/a', () => { |
| 24 | + let response |
| 25 | + before(() => { |
| 26 | + return request(app).get('/server/a') |
| 27 | + .then(res => { response = res }) |
| 28 | + }) |
| 29 | + |
| 30 | + it('proxies to http://server-a.org/', () => { |
| 31 | + const { server, path } = response.body |
| 32 | + expect(server).to.equal('a') |
| 33 | + expect(path).to.equal('/') |
| 34 | + }) |
| 35 | + |
| 36 | + it('returns status code 200', () => { |
| 37 | + expect(response).to.have.property('statusCode', 200) |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('responding to /server/a/my/path?query=string', () => { |
| 42 | + let response |
| 43 | + before(() => { |
| 44 | + return request(app).get('/server/a/my/path?query=string') |
| 45 | + .then(res => { response = res }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('proxies to http://server-a.org/my/path?query=string', () => { |
| 49 | + const { server, path } = response.body |
| 50 | + expect(server).to.equal('a') |
| 51 | + expect(path).to.equal('/my/path?query=string') |
| 52 | + }) |
| 53 | + |
| 54 | + it('returns status code 200', () => { |
| 55 | + expect(response).to.have.property('statusCode', 200) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + describe('responding to /server/b', () => { |
| 60 | + let response |
| 61 | + before(() => { |
| 62 | + return request(app).get('/server/b') |
| 63 | + .then(res => { response = res }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('proxies to http://server-b.org/foo/bar', () => { |
| 67 | + const { server, path } = response.body |
| 68 | + expect(server).to.equal('b') |
| 69 | + expect(path).to.equal('/foo/bar') |
| 70 | + }) |
| 71 | + |
| 72 | + it('returns status code 200', () => { |
| 73 | + expect(response).to.have.property('statusCode', 200) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe('responding to /server/b/my/path?query=string', () => { |
| 78 | + let response |
| 79 | + before(() => { |
| 80 | + return request(app).get('/server/b/my/path?query=string') |
| 81 | + .then(res => { response = res }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('proxies to http://server-b.org/foo/bar/my/path?query=string', () => { |
| 85 | + const { server, path } = response.body |
| 86 | + expect(server).to.equal('b') |
| 87 | + expect(path).to.equal('/foo/bar/my/path?query=string') |
| 88 | + }) |
| 89 | + |
| 90 | + it('returns status code 200', () => { |
| 91 | + expect(response).to.have.property('statusCode', 200) |
| 92 | + }) |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +function addRequestDetails (server) { |
| 98 | + return function (path) { |
| 99 | + return { server, path, headers: this.req.headers } |
| 100 | + } |
| 101 | +} |
0 commit comments