Skip to content

Instantly share code, notes, and snippets.

@abuiles
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abuiles/37d16042057ecfabe4e5 to your computer and use it in GitHub Desktop.
Save abuiles/37d16042057ecfabe4e5 to your computer and use it in GitHub Desktop.
describe('proxy with subdomain', function() {
beforeEach(function() {
nockProxy = {
called: null,
method: null,
url: null
};
return subject.start({
proxy: 'http://api.lvh.me',
host: '0.0.0.0',
port: '1337',
baseURL: '/'
});
});
function apiTest(app, method, url, done) {
var req = request(app);
return req[method].call(req, url)
.set('accept', 'text/json')
.end(function(err) {
debugger
if (err) {
return done(err);
}
assert(nockProxy.called, 'proxy receives the request');
assert.equal(nockProxy.method, method.toUpperCase());
assert.equal(nockProxy.url, url);
done();
});
}
it('proxies GET', function(done) {
nock('http://api.lvh.me')
.get('/api/get')
.reply(200, function() {
nockProxy.called = true;
nockProxy.method = 'GET';
nockProxy.url = '/api/get';
return '';
});
apiTest(subject.app, 'get', '/api/get', done);
});
it('proxies PUT', function(done) {
nock('http://api.lvh.me')
.put('/api/put')
.reply(204, function() {
nockProxy.called = true;
nockProxy.method = 'PUT';
nockProxy.url = '/api/put';
return '';
});
apiTest(subject.app, 'put', '/api/put', done);
});
it('proxies POST', function(done) {
nock('http://api.lvh.me')
.post('/api/post')
.reply(201, function() {
nockProxy.called = true;
nockProxy.method = 'POST';
nockProxy.url = '/api/post';
return '';
});
apiTest(subject.app, 'post', '/api/post', done);
});
it('proxies DELETE', function(done) {
nock('http://api.lvh.me')
.delete('/api/delete')
.reply(204, function() {
nockProxy.called = true;
nockProxy.method = 'DELETE';
nockProxy.url = '/api/delete';
return '';
});
apiTest(subject.app, 'delete', '/api/delete', done);
});
// test for #1263
it('proxies when accept contains */*', function(done) {
nock('http://api.lvh.me')
.get('/api/get')
.reply(200, function() {
nockProxy.called = true;
nockProxy.method = 'GET';
nockProxy.url = '/api/get';
return '';
});
request(subject.app)
.get('/api/get')
.set('accept', 'application/json, */*')
.end(function(err) {
if (err) {
return done(err);
}
assert(nockProxy.called, 'proxy receives the request');
done();
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment