it('should proxy through to the path that is given', function(done) { request(buildApp(openProxyOptions))[methodName]('/https://example.com/blah?query=value&otherQuery=otherValue') .expect(200) .expect(function() { expect(fakeRequest.calls.argsFor(0)[0].url).toBe('https://example.com/blah?query=value&otherQuery=otherValue'); expect(fakeRequest.calls.argsFor(0)[0].method).toBe(verb); }) .end(assert(done)); });
describe('on post,', function() { doCommonTests('POST'); it('should pass the body through', function(done) { request(buildApp(openProxyOptions)) .post('/example.com') .send('boaty mcboatface') .expect(200) .expect(function() { expect(fakeRequest.calls.argsFor(0)[0].body).toEqual(new Buffer('boaty mcboatface')); }) .end(assert(done)); }); });
describe('on post,', function() { it('should error out with routing off', function(done) { request(buildApp(routingOffOptions)) .post('/mochiRoute') .expect(404) .end(assert(done)); }); it('should error out with routing on', function(done) { request(buildApp(routingOnOptions)) .post('/mochiRoute') .expect(404) .end(assert(done)); }); });
it('should return 400 if no url is specified', function(done) { request(buildApp(openProxyOptions)) [methodName]('/') .expect(400) .end(assert(done)) });
it('no params appended when mismatch in regex', function(done) { request(buildApp({ proxyAllDomains: true, appendParamToQueryString: { "example.com": [{ "regexPattern": "something", "params": { "foo": "bar" } }] } }))[methodName]('/example.com/nothing/else') .expect(200) .expect(function() { const hitUrl = new URL(fakeRequest.calls.argsFor(0)[0].url) expect(hitUrl.searchParams.get('foo')).toBeNull(); }) .end(assert(done)); });
it('should not set the headers for other domains', function(done) { request(buildApp({ proxyAllDomains: true, proxyAuth: { 'example2.com': { "headers": [{ name: "Secret-Key", value: "ABCDE12345" }, { name: "Another-Header", value: "XYZ" }] } } }))[methodName]('/example.com/auth') .expect(200) .expect(function() { expect(fakeRequest.calls.argsFor(0)[0].headers['Secret-Key']).toBeUndefined(); expect(fakeRequest.calls.argsFor(0)[0].headers['Another-Header']).toBeUndefined(); expect(fakeRequest.calls.argsFor(0)[0].method).toBe(verb); }) .end(assert(done)); });
it('should pass back headers from the proxied request', function(done) { request(buildApp(openProxyOptions)) [methodName]('/example.com') .expect(200) .expect('fakeheader', 'fakevalue') .end(assert(done)); });
it('append params to the existing querystring for a specified domain', function(done) { request(buildApp({ proxyAllDomains: true, appendParamToQueryString: { "example.com": [{ "regexPattern": ".", "params": { "foo": "bar" } }] } }))[methodName]('/example.com?already=here') .expect(200) .expect(function() { const hitUrl = new URL(fakeRequest.calls.argsFor(0)[0].url) expect(hitUrl.search).toBe('?already=here&foo=bar'); }) .end(assert(done)); });
it('should set the header for that domain', function(done) { request(buildApp({ proxyAllDomains: true, proxyAuth: { 'example.com': { "headers": [{ name: "Secret-Key", value: "ABCDE12345" }, { name: "Another-Header", value: "XYZ" }] } } }))[methodName]('/example.com/auth') .expect(200) .expect(function() { expect(fakeRequest.calls.argsFor(0)[0].headers['Secret-Key']).toBe('ABCDE12345'); expect(fakeRequest.calls.argsFor(0)[0].headers['Another-Header']).toBe('XYZ'); expect(fakeRequest.calls.argsFor(0)[0].method).toBe(verb); }) .end(assert(done)); });
it('append params to the querystring for a specified domain using specified regex', function(done) { request(buildApp({ proxyAllDomains: true, appendParamToQueryString: { "example.com": [{ "regexPattern": "something", "params": { "foo": "bar" } }] } }))[methodName]('/example.com/something/else') .expect(200) .expect(function() { const hitUrl = new URL(fakeRequest.calls.argsFor(0)[0].url) expect(hitUrl.searchParams.get('foo')).toBe('bar'); }) .end(assert(done)); });
it('should add a trailing slash if it isn\'t provided', function(done) { request(buildApp(openProxyOptions)) [methodName]('/example.com') .expect(200) .expect(function() { expect(fakeRequest.calls.argsFor(0)[0].url).toBe('http://example.com/'); expect(fakeRequest.calls.argsFor(0)[0].method).toBe(verb); }) .end(assert(done)); });
it('should add http if it isn\'t provided', function(done) { request(buildApp(openProxyOptions)) [methodName]('/example.com/') .expect(200) .expect(function(err) { expect(fakeRequest.calls.argsFor(0)[0].url).toBe('http://example.com/'); expect(fakeRequest.calls.argsFor(0)[0].method).toBe(verb); }) .end(assert(done)); });
it('should stream back the body of the request made', function(done) { request(buildApp(openProxyOptions)) [methodName]('/example.com') .expect(200, 'blahblah2') .end(assert(done)); });
it('append params to the querystring for a specified domain', function(done) { request(buildApp({ proxyAllDomains: true, appendParamToQueryString: { "example.com": [{ "regexPattern": ".", "params": { "foo": "bar" } }] } }))[methodName]('/example.com') .expect(200) .expect(function() { const hitUrl = new URL(fakeRequest.calls.argsFor(0)[0].url) expect(hitUrl.searchParams.get('foo')).toBe('bar'); }) .end(assert(done)); });
it('doesnt append params to the querystring for other domains', function(done) { request(buildApp({ proxyAllDomains: true, appendParamToQueryString: { "example.com": [{ "regexPattern": ".", "params": { "foo": "bar" } }] } }))[methodName]('/example2.com') .expect(200) .expect(function() { const hitUrl = new URL(fakeRequest.calls.argsFor(0)[0].url) expect(hitUrl.searchParams.get('foo')).toBeNull(); }) .end(assert(done)); });