[Pkg-javascript-commits] [node-stream-http] 18/208: Test streaming

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 9a3a02363b1c26f7db7fd32f0e20847dba70ef88
Author: John Hiesey <john at hiesey.com>
Date:   Thu Jul 2 21:23:43 2015 -0700

    Test streaming
---
 test/server/index.js    | 50 +++++++++++++++++++++++++++++++++++++++++++++++--
 test/tests/streaming.js | 27 ++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/test/server/index.js b/test/server/index.js
index 2bd067b..ad546ec 100644
--- a/test/server/index.js
+++ b/test/server/index.js
@@ -1,14 +1,60 @@
 var express = require('express')
+var fs = require('fs')
 var http = require('http')
 var path = require('path')
+var url = require('url')
 
 var app = express()
 var server = http.createServer(app)
 
+// app.use(function (req, res, next) {
+//   res.header('Access-Control-Allow-Origin', '*')
+
+//   next()
+// })
+
+// Otherwise, use 'application/octet-stream'
+var copiesMimeTypes = {
+	'/basic.txt': 'text/plain'
+}
+
+var maxDelay = 5000 // ms
+
 app.use(function (req, res, next) {
-  res.header('Access-Control-Allow-Origin', '*')
+	var parsed = url.parse(req.url, true)
+
+	if ('copies' in parsed.query) {
+		var totalCopies = parseInt(parsed.query.copies, 10)
+		function fail () {
+			res.statusCode = 500
+			res.end()
+		}
+		fs.readFile(path.join(__dirname, 'static', parsed.pathname), function (err, data) {
+			if (err)
+				return fail()
+
+			var mimeType = copiesMimeTypes[parsed.pathname] || 'application/octet-stream'
+			res.setHeader('Content-Type', mimeType)
+			res.setHeader('Content-Length', data.length * totalCopies)
+			var pieceDelay = maxDelay / totalCopies
+			if (pieceDelay > 100)
+				pieceDelay = 100
+
+			function write (copies) {
+				if (copies === 0) 
+					return res.end()
 
-  next()
+				res.write(data, function (err) {
+					if (err)
+						return fail()
+					setTimeout(write.bind(null, copies - 1), pieceDelay)
+				})
+			}
+			write(totalCopies)
+		})
+		return
+	}
+	next()
 })
 
 app.use(express.static(path.join(__dirname, 'static')))
diff --git a/test/tests/streaming.js b/test/tests/streaming.js
new file mode 100644
index 0000000..41de46c
--- /dev/null
+++ b/test/tests/streaming.js
@@ -0,0 +1,27 @@
+var Buffer = require('buffer').Buffer
+var fs = require('fs');
+var test = require('tape')
+
+var copies = 1000
+
+var referenceOnce = fs.readFileSync(__dirname + '/../server/static/basic.txt');
+var reference = new Buffer(referenceOnce.length * 1000)
+reference.fill(referenceOnce)
+
+var http = require('../..')
+
+test('text streaming', function (t) {
+	http.get('/basic.txt?copies=1000', function (res) {
+		var buffers = []
+
+		res.on('end', function () {
+			t.ok(buffers.length > 5, 'received in multiple parts')
+			t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
+			t.end()
+		})
+
+		res.on('data', function (data) {
+			buffers.push(data)
+		})
+	})
+})
\ No newline at end of file

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