[Pkg-javascript-commits] [node-stream-http] 27/208: Fix basic auth support and add test

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:24 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stream-http.

commit 98a3dadbdffeb09857617e7a017dc799cb17b4df
Author: John Hiesey <john at hiesey.com>
Date:   Tue Jul 7 14:35:15 2015 -0700

    Fix basic auth support and add test
---
 lib/request.js                   | 11 +++--------
 package.json                     |  1 +
 test/browser/auth.js             | 22 ++++++++++++++++++++++
 test/browser/binary-streaming.js |  4 ++--
 test/browser/binary.js           |  4 ++--
 test/browser/headers.js          |  2 +-
 test/browser/text-streaming.js   |  4 ++--
 test/browser/text.js             |  4 ++--
 test/server/index.js             | 17 ++++++++++++++++-
 9 files changed, 51 insertions(+), 18 deletions(-)

diff --git a/lib/request.js b/lib/request.js
index a32595c..de1ec05 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -33,6 +33,8 @@ var ClientRequest = module.exports = function (opts) {
 	self._url = opts.protocol + '//' + opts.hostname + ':' + opts.port + opts.path
 	self._body = []
 	self._headers = {}
+	if (opts.auth)
+		self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64'))
 	foreach(keys(opts.headers), function (name) {
 		self.setHeader(name, opts.headers[name])
 	})
@@ -82,13 +84,6 @@ ClientRequest.prototype._onFinish = function () {
 
 	var opts = self._opts
 
-	var user, pass
-	if (opts.auth) {
-		var authMatch = opts.auth.match(/^([^:]*):(.*)$/)
-		user = authMatch[0]
-		pass = authMatch[1]
-	}
-
 	var headersObj = self._headers
 	var body
 	if (opts.method in ['PUT', 'POST']) {
@@ -121,7 +116,7 @@ ClientRequest.prototype._onFinish = function () {
 		})
 	} else {
 		var xhr = self._xhr = new window.XMLHttpRequest()
-		xhr.open(self._opts.method, self._url, true, user, pass)
+		xhr.open(self._opts.method, self._url, true)
 
 		// Can't set responseType on really old browsers
 		if ('responseType' in xhr)
diff --git a/package.json b/package.json
index 05fb501..6fc0361 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
     "object-keys": "^1.0.4"
   },
   "devDependencies": {
+    "basic-auth": "^1.0.3",
     "brfs": "^1.4.0",
     "express": "^4.13.0",
     "tape": "^4.0.0",
diff --git a/test/browser/auth.js b/test/browser/auth.js
new file mode 100644
index 0000000..01144da
--- /dev/null
+++ b/test/browser/auth.js
@@ -0,0 +1,22 @@
+var Buffer = require('buffer').Buffer
+var test = require('tape')
+
+var http = require('../..')
+
+test('Authentication', function (t) {
+	http.get({
+		path: '/auth',
+		auth: 'TestUser:trustno1'
+	}, function (res) {
+		var buffers = []
+
+		res.on('end', function () {
+			t.ok(new Buffer('You\'re in!').equals(Buffer.concat(buffers)), 'authentication succeeded')
+			t.end()
+		})
+
+		res.on('data', function (data) {
+			buffers.push(data)
+		})
+	})
+})
\ No newline at end of file
diff --git a/test/browser/binary-streaming.js b/test/browser/binary-streaming.js
index 71f821b..557c62f 100644
--- a/test/browser/binary-streaming.js
+++ b/test/browser/binary-streaming.js
@@ -1,5 +1,5 @@
 var Buffer = require('buffer').Buffer
-var fs = require('fs');
+var fs = require('fs')
 var test = require('tape')
 var UAParser = require('ua-parser-js')
 
@@ -17,7 +17,7 @@ var skipVerification = (browserName === 'IE' && browserVersion <= 8)
 var COPIES = 10
 var MIN_PIECES = 2
 
-var referenceOnce = fs.readFileSync(__dirname + '/../server/static/browserify.png');
+var referenceOnce = fs.readFileSync(__dirname + '/../server/static/browserify.png')
 var reference = new Buffer(referenceOnce.length * COPIES)
 for(var i = 0; i < COPIES; i++) {
 	referenceOnce.copy(reference, referenceOnce.length * i)
diff --git a/test/browser/binary.js b/test/browser/binary.js
index 339f7fe..e116b8d 100644
--- a/test/browser/binary.js
+++ b/test/browser/binary.js
@@ -1,5 +1,5 @@
 var Buffer = require('buffer').Buffer
-var fs = require('fs');
+var fs = require('fs')
 var test = require('tape')
 var UAParser = require('ua-parser-js')
 
@@ -11,7 +11,7 @@ var browserVersion = browser.major
 // Binary data gets corrupted in IE8 or below
 var skipVerification = (browserName === 'IE' && browserVersion <= 8)
 
-var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png');
+var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png')
 
 test('binary download', function (t) {
 	http.get('/browserify.png', function (res) {
diff --git a/test/browser/headers.js b/test/browser/headers.js
index 72dbc6a..2b9e882 100644
--- a/test/browser/headers.js
+++ b/test/browser/headers.js
@@ -1,5 +1,5 @@
 var Buffer = require('buffer').Buffer
-var fs = require('fs');
+var fs = require('fs')
 var test = require('tape')
 
 var http = require('../..')
diff --git a/test/browser/text-streaming.js b/test/browser/text-streaming.js
index dd83676..812ef14 100644
--- a/test/browser/text-streaming.js
+++ b/test/browser/text-streaming.js
@@ -1,5 +1,5 @@
 var Buffer = require('buffer').Buffer
-var fs = require('fs');
+var fs = require('fs')
 var test = require('tape')
 var UAParser = require('ua-parser-js')
 
@@ -14,7 +14,7 @@ var skipStreamingCheck = (browserName === 'Opera' || (browserName === 'IE' && br
 var COPIES = 1000
 var MIN_PIECES = 5
 
-var referenceOnce = fs.readFileSync(__dirname + '/../server/static/basic.txt');
+var referenceOnce = fs.readFileSync(__dirname + '/../server/static/basic.txt')
 var reference = new Buffer(referenceOnce.length * COPIES)
 for(var i = 0; i < COPIES; i++) {
 	referenceOnce.copy(reference, referenceOnce.length * i)
diff --git a/test/browser/text.js b/test/browser/text.js
index 4593a47..5076f84 100644
--- a/test/browser/text.js
+++ b/test/browser/text.js
@@ -1,10 +1,10 @@
 var Buffer = require('buffer').Buffer
-var fs = require('fs');
+var fs = require('fs')
 var test = require('tape')
 
 var http = require('../..')
 
-var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt');
+var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt')
 
 test('basic functionality', function (t) {
 	http.get('/basic.txt', function (res) {
diff --git a/test/server/index.js b/test/server/index.js
index 9d04290..158f342 100644
--- a/test/server/index.js
+++ b/test/server/index.js
@@ -1,3 +1,4 @@
+var basicAuth = require('basic-auth')
 var express = require('express')
 var fs = require('fs')
 var http = require('http')
@@ -20,7 +21,7 @@ var copiesMimeTypes = {
 
 var maxDelay = 5000 // ms
 
-app.use('/testHeaders', function (req, res, next) {
+app.get('/testHeaders', function (req, res) {
 	var parsed = url.parse(req.url, true)
 
 	// Values in query parameters are sent as response headers
@@ -44,6 +45,20 @@ app.use('/testHeaders', function (req, res, next) {
 	res.end()
 })
 
+app.get('/auth', function (req, res) {
+	var user = basicAuth(req)
+
+	if (!user || user.name !== 'TestUser' || user.pass !== 'trustno1') {
+		console.log(user)
+		res.setHeader('WWW-Authenticate', 'Basic realm="example"')
+		res.end('Access denied')
+	} else {
+		res.setHeader('Content-Type', 'text/plain')
+		res.write('You\'re in!')
+		res.end()
+	}
+})
+
 app.use(function (req, res, next) {
 	var parsed = url.parse(req.url, true)
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-http.git



More information about the Pkg-javascript-commits mailing list