spec/shared/setup.js
function configureNock(options, config) {
  var result = {};
  // deny all real net connections except for localhost
  nock.disableNetConnect();
  nock.enableNetConnect('localhost');

  function createNock(url) {
    var instance = nock(url),
        expectedViaHeader = options.headers['Via'] ||
                            'http://localhost:' + config.proxy.gateway.port,
        expectedHostHeader = options.headers['Host'] || /.*/;

    if (options.proxy === true) {
      // verify that the request was actually proxied
      // optionally support downstream proxies with non-RFC expectations on
      // the Host and Via request headers
      instance.matchHeader('via', expectedViaHeader);
      instance.matchHeader('host', expectedHostHeader);
    }

    // verify the injected header
    instance.matchHeader('x-test-header', 'John Doe');

    return instance;
  }

  rules = [
    createNock('http://api.example.com')
    .get('/api/hello')
    .reply(200, '{ "hello": "world" }')
    .get('/account?id=1&email=2&sort=asc')
    .reply(200, '{ "email": "john.doe@example.com" }')
    .get('/api/notfound')
    .reply(404),

    createNock('http://rewrite.example.com')
    .get('/hello')
    .reply(200, '{ "hello": "world" }')
    .get('/foo/bar')
    .reply(200, '{ "foo": "bar" }'),

    createNock('http://www.example.com')
    .get('/foo/12345/bar')
    .reply(200, '{ "foo": "bar" }')
    .get('/subapp/junction/customer/1')
    .reply(200, '{ "id": 1 }'),

    createNock('https://api.example.biz')
    .get('/issue/8')
    .reply(200, '{ "reporter": "@heygrady" }'),

    createNock('https://secure.example.com')
    .get('/secure/api/hello')
    .reply(200, '{ "hello": "world" }')
    .get('/secure/api/notfound')
    .reply(404),

    createNock('https://authorization.example.com')
    .matchHeader('X-Test-Header-Function', 'Bearer 0123456789abcdef')
    .get('/token')
    .reply(200, '{ "author": "ehtb" }')
  ];
}
function configureNock(options, config) {
  var result = {};
  // deny all real net connections except for localhost
  nock.disableNetConnect();
  nock.enableNetConnect('localhost');

  function createNock(url) {
    var instance = nock(url),
        expectedViaHeader = options.headers['Via'] ||
                            'http://localhost:' + config.proxy.gateway.port,
        expectedHostHeader = options.headers['Host'] || /.*/;

    if (options.proxy === true) {
      // verify that the request was actually proxied
      // optionally support downstream proxies with non-RFC expectations on
      // the Host and Via request headers
      instance.matchHeader('via', expectedViaHeader);
      instance.matchHeader('host', expectedHostHeader);
    }

    // verify the injected header
    instance.matchHeader('x-test-header', 'John Doe');

    return instance;
  }

  rules = [
    createNock('http://api.example.com')
    .get('/api/hello')
    .reply(200, '{ "hello": "world" }')
    .get('/account?id=1&email=2&sort=asc')
    .reply(200, '{ "email": "john.doe@example.com" }')
    .get('/api/notfound')
    .reply(404),

    createNock('http://rewrite.example.com')
    .get('/hello')
    .reply(200, '{ "hello": "world" }')
    .get('/foo/bar')
    .reply(200, '{ "foo": "bar" }'),

    createNock('http://www.example.com')
    .get('/foo/12345/bar')
    .reply(200, '{ "foo": "bar" }')
    .get('/subapp/junction/customer/1')
    .reply(200, '{ "id": 1 }'),

    createNock('https://api.example.biz')
    .get('/issue/8')
    .reply(200, '{ "reporter": "@heygrady" }'),

    createNock('https://secure.example.com')
    .get('/secure/api/hello')
    .reply(200, '{ "hello": "world" }')
    .get('/secure/api/notfound')
    .reply(404),

    createNock('https://authorization.example.com')
    .matchHeader('X-Test-Header-Function', 'Bearer 0123456789abcdef')
    .get('/token')
    .reply(200, '{ "author": "ehtb" }')
  ];
}
function configureNock ( options , config ) { var result = { } ; // deny all real net connections except for localhost nock . disableNetConnect ( ) ; nock . enableNetConnect ( 'localhost' ) ; function createNock ( url ) { var instance = nock ( url ) , expectedViaHeader = options . headers [ 'Via' ] || 'http://localhost:' + config . proxy . gateway . port , expectedHostHeader = options . headers [ 'Host' ] || / .* / ; if ( options . proxy === true ) { // verify that the request was actually proxied // optionally support downstream proxies with non-RFC expectations on // the Host and Via request headers instance . matchHeader ( 'via' , expectedViaHeader ) ; instance . matchHeader ( 'host' , expectedHostHeader ) ; } // verify the injected header instance . matchHeader ( 'x-test-header' , 'John Doe' ) ; return instance ; } rules = [ createNock ( 'http://api.example.com' ) . get ( '/api/hello' ) . reply ( 200 , '{ "hello": "world" }' ) . get ( '/account?id=1&email=2&sort=asc' ) . reply ( 200 , '{ "email": "john.doe@example.com" }' ) . get ( '/api/notfound' ) . reply ( 404 ) , createNock ( 'http://rewrite.example.com' ) . get ( '/hello' ) . reply ( 200 , '{ "hello": "world" }' ) . get ( '/foo/bar' ) . reply ( 200 , '{ "foo": "bar" }' ) , createNock ( 'http://www.example.com' ) . get ( '/foo/12345/bar' ) . reply ( 200 , '{ "foo": "bar" }' ) . get ( '/subapp/junction/customer/1' ) . reply ( 200 , '{ "id": 1 }' ) , createNock ( 'https://api.example.biz' ) . get ( '/issue/8' ) . reply ( 200 , '{ "reporter": "@heygrady" }' ) , createNock ( 'https://secure.example.com' ) . get ( '/secure/api/hello' ) . reply ( 200 , '{ "hello": "world" }' ) . get ( '/secure/api/notfound' ) . reply ( 404 ) , createNock ( 'https://authorization.example.com' ) . matchHeader ( 'X-Test-Header-Function' , 'Bearer 0123456789abcdef' ) . get ( '/token' ) . reply ( 200 , '{ "author": "ehtb" }' ) ] ; }
configures nock globally for a test run

@param  {Object.<string, string>} options  the options for configuring this
test suite
@param  {Object.<string, string>} config   the config of the json-proxy for
this test suite
@returns {Array}  an array of configured nock instances
configures nock globally for a test run