[Pkg-javascript-commits] [node-stream-http] 20/208: Add binary streaming 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 157b71a58d828dfb6025443c5c6948ce5b55e5e5
Author: John Hiesey <john at hiesey.com>
Date:   Fri Jul 3 02:51:54 2015 -0700

    Add binary streaming test
---
 lib/request.js                                 | 21 +++++++++---
 lib/response.js                                |  7 +++-
 test/tests/binary-streaming.js                 | 47 ++++++++++++++++++++++++++
 test/tests/binary.js                           | 10 +++---
 test/tests/{streaming.js => text-streaming.js} | 16 +++++----
 test/tests/{basic.js => text.js}               |  4 +--
 6 files changed, 88 insertions(+), 17 deletions(-)

diff --git a/lib/request.js b/lib/request.js
index 3e5afac..1e3c213 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -136,14 +136,15 @@ ClientRequest.prototype._onFinish = function () {
 			xhr.setRequestHeader(name, headers[name])
 		})
 
+		self._response = null
 		xhr.onreadystatechange = function () {
 			switch (xhr.readyState) {
 				case rStates.LOADING:
-					if (!self._response)
-						self._connect()
-					// falls through
 				case rStates.DONE:
-					self._response._onXHRReadyStateChange()
+					if (!self._response && statusValid(xhr))
+						self._connect()
+					if (self._response)
+						self._response._onXHRReadyStateChange()
 					break
 			}
 		}
@@ -163,6 +164,18 @@ ClientRequest.prototype._onFinish = function () {
 	}
 }
 
+/**
+ * Checks if xhr.status is readable. Even though the spec says it should
+ * be available in readyState 3, accessing it throws an exception in IE8
+ */
+function statusValid (xhr) {
+	try {
+		return (xhr.status !== null)
+	} catch (e) {
+		return false
+	}
+}
+
 ClientRequest.prototype._connect = function () {
 	var self = this
 
diff --git a/lib/response.js b/lib/response.js
index 0e81cfc..0af0b71 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -103,7 +103,12 @@ IncomingMessage.prototype._onXHRReadyStateChange = function () {
 			}
 			// Falls through in IE8	
 		case 'text':
-			response = xhr.responseText
+			try { // This will fail when readyState = 3 in IE9. Switch mode and wait for readyState = 4
+				response = xhr.responseText
+			} catch (e) {
+				self._mode = 'text:vbarray'
+				break
+			}
 			if (response.length > self._pos) {
 				var newData = response.substr(self._pos)
 				if (self._charset === 'x-user-defined') {
diff --git a/test/tests/binary-streaming.js b/test/tests/binary-streaming.js
new file mode 100644
index 0000000..6d5138e
--- /dev/null
+++ b/test/tests/binary-streaming.js
@@ -0,0 +1,47 @@
+var Buffer = require('buffer').Buffer
+var fs = require('fs');
+var test = require('tape')
+var UAParser = require('ua-parser-js')
+
+var http = require('../..')
+
+var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
+var browserName = browser.name
+var browserVersion = browser.major
+// Binary streaming doesn't work in IE10 or below or in Opera
+var skipStreamingCheck = (browserName === 'Opera' ||
+	(browserName === 'IE' && browserVersion <= 10))
+
+// Binary data gets corrupted in IE8 or below
+var skipVerification = (browserName === 'IE' && browserVersion <= 8)
+
+var COPIES = 8
+var MIN_PIECES = 2
+
+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)
+}
+
+test('binary streaming', function (t) {
+	http.get('/browserify.png?copies=' + COPIES, function (res) {
+		var buffers = []
+		res.on('end', function () {
+			if (skipVerification)
+				t.skip('binary data not preserved on IE <= 8')
+			else
+				t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
+
+			if (skipStreamingCheck)
+				t.skip('streaming not available on IE <= 8')
+			else
+				t.ok(buffers.length >= MIN_PIECES, 'received in multiple parts')
+			t.end()
+		})
+
+		res.on('data', function (data) {
+			buffers.push(data)
+		})
+	})
+})
\ No newline at end of file
diff --git a/test/tests/binary.js b/test/tests/binary.js
index 0dd8eae..339f7fe 100644
--- a/test/tests/binary.js
+++ b/test/tests/binary.js
@@ -3,14 +3,16 @@ var fs = require('fs');
 var test = require('tape')
 var UAParser = require('ua-parser-js')
 
-var browserType = (new UAParser()).setUA(navigator.userAgent).getBrowser()
+var http = require('../..')
+
+var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
+var browserName = browser.name
+var browserVersion = browser.major
 // Binary data gets corrupted in IE8 or below
-var skipVerification = (browserType.name === 'IE' && browserType.major <= 8)
+var skipVerification = (browserName === 'IE' && browserVersion <= 8)
 
 var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png');
 
-var http = require('../..')
-
 test('binary download', function (t) {
 	http.get('/browserify.png', function (res) {
 		var buffers = []
diff --git a/test/tests/streaming.js b/test/tests/text-streaming.js
similarity index 68%
rename from test/tests/streaming.js
rename to test/tests/text-streaming.js
index 4a56615..dd83676 100644
--- a/test/tests/streaming.js
+++ b/test/tests/text-streaming.js
@@ -3,18 +3,22 @@ var fs = require('fs');
 var test = require('tape')
 var UAParser = require('ua-parser-js')
 
-var browserType = (new UAParser()).setUA(navigator.userAgent).getBrowser()
-// Streaming doesn't work in IE8 or below
-var skipStreamingCheck = (browserType.name === 'IE' && browserType.major <= 8)
+var http = require('../..')
+
+var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
+var browserName = browser.name
+var browserVersion = browser.major
+// Streaming doesn't work in IE9 or below or in Opera
+var skipStreamingCheck = (browserName === 'Opera' || (browserName === 'IE' && browserVersion <= 9))
 
 var COPIES = 1000
 var MIN_PIECES = 5
 
 var referenceOnce = fs.readFileSync(__dirname + '/../server/static/basic.txt');
 var reference = new Buffer(referenceOnce.length * COPIES)
-reference.fill(referenceOnce)
-
-var http = require('../..')
+for(var i = 0; i < COPIES; i++) {
+	referenceOnce.copy(reference, referenceOnce.length * i)
+}
 
 test('text streaming', function (t) {
 	http.get({
diff --git a/test/tests/basic.js b/test/tests/text.js
similarity index 99%
rename from test/tests/basic.js
rename to test/tests/text.js
index ccc3734..4593a47 100644
--- a/test/tests/basic.js
+++ b/test/tests/text.js
@@ -2,10 +2,10 @@ var Buffer = require('buffer').Buffer
 var fs = require('fs');
 var test = require('tape')
 
-var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt');
-
 var http = require('../..')
 
+var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt');
+
 test('basic functionality', function (t) {
 	http.get('/basic.txt', function (res) {
 		var buffers = []

-- 
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