Sha256: f6e951a0dfb9f16a0c17e68a7994232403b5c2a2636efa1a383cf3f1efeb3288

Contents?: true

Size: 1.75 KB

Versions: 19

Compression:

Stored size: 1.75 KB

Contents

var assert = require('assert')
  , http = require('http')
  , request = require('../index')
  ;

var numBasicRequests = 0;

var basicServer = http.createServer(function (req, res) {
  console.error('Basic auth server: ', req.method, req.url);
  numBasicRequests++;

  var ok;

  if (req.headers.authorization) {
    if (req.headers.authorization == 'Basic ' + new Buffer('test:testing2').toString('base64')) {
      ok = true;
    } else {
      // Bad auth header, don't send back WWW-Authenticate header
      ok = false;
    }
  } else {
    // No auth header, send back WWW-Authenticate header
    ok = false;
    res.setHeader('www-authenticate', 'Basic realm="Private"');
  }

  if (ok) {
    console.log('request ok');
    res.end('ok');
  } else {
    console.log('status=401');
    res.statusCode = 401;
    res.end('401');
  }
});

basicServer.listen(6767);

request({
  'method': 'GET',
  'uri': 'http://localhost:6767/test/',
  'auth': {
    'user': 'test',
    'pass': 'testing2',
    'sendImmediately': false
  }
}, function(error, response, body) {
  assert.equal(response.statusCode, 200);
  assert.equal(numBasicRequests, 2);

  // If we don't set sendImmediately = false, request will send basic auth
  request({
    'method': 'GET',
    'uri': 'http://localhost:6767/test2/',
    'auth': {
      'user': 'test',
      'pass': 'testing2'
    }
  }, function(error, response, body) {
    assert.equal(response.statusCode, 200);
    assert.equal(numBasicRequests, 3);

    request({
      'method': 'GET',
      'uri': 'http://test:testing2@localhost:6767/test2/'
    }, function(error, response, body) {
      assert.equal(response.statusCode, 200);
      assert.equal(numBasicRequests, 4);

      console.log('All tests passed');
      basicServer.close();
    });
  });
});

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ela-4.1.6 node_modules/request/tests/test-basic-auth.js
ela-4.1.5 node_modules/request/tests/test-basic-auth.js
ela-4.1.4 node_modules/request/tests/test-basic-auth.js
ela-4.1.3 node_modules/request/tests/test-basic-auth.js
ela-4.1.2 node_modules/request/tests/test-basic-auth.js
ela-4.1.1 node_modules/request/tests/test-basic-auth.js
ela-4.1.0 node_modules/request/tests/test-basic-auth.js
ela-4.0.0 node_modules/request/tests/test-basic-auth.js
ela-3.4.3 node_modules/request/tests/test-basic-auth.js
ela-3.4.2 node_modules/request/tests/test-basic-auth.js
ela-3.4.0 node_modules/request/tests/test-basic-auth.js
ela-3.3.1 node_modules/request/tests/test-basic-auth.js
ela-3.3.0 node_modules/request/tests/test-basic-auth.js
ela-3.2.0 node_modules/request/tests/test-basic-auth.js
ela-3.1.1 node_modules/request/tests/test-basic-auth.js
ela-3.1.0 node_modules/request/tests/test-basic-auth.js
ela-3.0.0 node_modules/request/tests/test-basic-auth.js
ela-2.0.0 node_modules/request/tests/test-basic-auth.js
ela-1.1.0 node_modules/request/tests/test-basic-auth.js