Server IP : 85.214.239.14 / Your IP : 3.141.46.29 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/task/3/root/proc/3/root/lib/node_modules/pm2/node_modules/needle/test/ |
Upload File : |
var helpers = require('./helpers'), should = require('should'), needle = require('./../'), server; var port = 7707; describe('Basic Auth', function() { before(function(done) { server = helpers.server({ port: port }, done); }) after(function(done) { server.close(done); }) ///////////////// helpers var get_auth = function(header) { var token = header.split(/\s+/).pop(); return token && Buffer.from(token, 'base64').toString().split(':'); } describe('when neither username or password are passed', function() { it('doesnt send any Authorization headers', function(done) { needle.get('localhost:' + port, { parse: true }, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.not.containEql('authorization'); done(); }) }) }) describe('when username is an empty string, and password is a valid string', function() { var opts = { username: '', password: 'foobar', parse: true }; it('doesnt send any Authorization headers', function(done) { needle.get('localhost:' + port, { parse: true }, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.not.containEql('authorization'); done(); }) }) }); describe('when username is a valid string, but no username is passed', function() { var opts = { username: 'foobar', parse: true }; it('sends Authorization header', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.containEql('authorization'); done(); }) }) it('Basic Auth only includes username, without colon', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; var auth = get_auth(sent_headers['authorization']); auth[0].should.equal('foobar'); auth.should.have.lengthOf(1); done(); }) }) }) describe('when username is a valid string, and password is null', function() { var opts = { username: 'foobar', password: null, parse: true }; it('sends Authorization header', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.containEql('authorization'); done(); }) }) it('Basic Auth only includes both username and password', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; var auth = get_auth(sent_headers['authorization']); auth[0].should.equal('foobar'); auth[1].should.equal(''); done(); }) }) }) describe('when username is a valid string, and password is an empty string', function() { var opts = { username: 'foobar', password: '', parse: true }; it('sends Authorization header', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.containEql('authorization'); done(); }) }) it('Basic Auth only includes both username and password', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; var auth = get_auth(sent_headers['authorization']); auth[0].should.equal('foobar'); auth[1].should.equal(''); auth.should.have.lengthOf(2); done(); }) }) }) describe('when username AND password are non empty strings', function() { var opts = { username: 'foobar', password: 'jakub', parse: true }; it('sends Authorization header', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.containEql('authorization'); done(); }) }) it('Basic Auth only includes both user and password', function(done) { needle.get('localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; var auth = get_auth(sent_headers['authorization']); auth[0].should.equal('foobar'); auth[1].should.equal('jakub'); auth.should.have.lengthOf(2); done(); }) }) }) describe('URL with @ but not username/pass', function() { it('doesnt send Authorization header', function(done) { var url = 'localhost:' + port + '/abc/@def/xyz.zip'; needle.get(url, {}, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.not.containEql('authorization'); done(); }) }) it('sends user:pass headers if passed via options', function(done) { var url = 'localhost:' + port + '/abc/@def/xyz.zip'; needle.get(url, { username: 'foo' }, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.containEql('authorization'); sent_headers['authorization'].should.eql('Basic Zm9v') done(); }) }) }) describe('when username/password are included in URL', function() { var opts = { parse: true }; it('sends Authorization header', function(done) { needle.get('foobar:jakub@localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; Object.keys(sent_headers).should.containEql('authorization'); done(); }) }) it('Basic Auth only includes both user and password', function(done) { needle.get('foobar:jakub@localhost:' + port, opts, function(err, resp) { var sent_headers = resp.body.headers; var auth = get_auth(sent_headers['authorization']); auth[0].should.equal('foobar'); auth[1].should.equal('jakub'); auth.should.have.lengthOf(2); done(); }) }) }) })