[Pkg-javascript-commits] [node-stream-http] 28/208: Add POST tests and fix test failures
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 4232780821bf39fe0458e42ef96a7a56b15842e1
Author: John Hiesey <john at hiesey.com>
Date: Tue Jul 7 16:35:16 2015 -0700
Add POST tests and fix test failures
---
index.js | 3 +-
lib/capability.js | 6 ++++
lib/request.js | 18 +++++++-----
package.json | 3 +-
test/browser/headers.js | 3 +-
test/browser/post-binary.js | 40 +++++++++++++++++++++++++
test/browser/post-text.js | 28 ++++++++++++++++++
test/server/index.js | 5 ++++
test/server/static/polyfill.js | 66 ++++++++++++++++++++++++++++++++++++++++++
9 files changed, 162 insertions(+), 10 deletions(-)
diff --git a/index.js b/index.js
index 304badd..c5e5c55 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,5 @@
var ClientRequest = require('./lib/request')
+var extend = require('xtend')
var url = require('url')
var util = require('util')
@@ -8,7 +9,7 @@ http.request = function (opts, cb) {
if (typeof opts === 'string')
opts = url.parse(opts)
else
- opts = util._extend({}, opts)
+ opts = extend(opts)
// Split opts.host into its components
var hostHostname = opts.host ? opts.host.split(':')[0] : null
diff --git a/lib/capability.js b/lib/capability.js
index 09d8890..39c721f 100644
--- a/lib/capability.js
+++ b/lib/capability.js
@@ -2,6 +2,12 @@ var util = require('util')
exports.fetch = util.isFunction(window.fetch) && util.isFunction(window.ReadableByteStream)
+exports.blobConstructor = false
+try {
+ new Blob([new ArrayBuffer(1)])
+ exports.blobConstructor = true
+} catch (e) {}
+
var xhr = new window.XMLHttpRequest()
xhr.open('GET', '/')
diff --git a/lib/request.js b/lib/request.js
index de1ec05..ef60060 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -86,13 +86,17 @@ ClientRequest.prototype._onFinish = function () {
var headersObj = self._headers
var body
- if (opts.method in ['PUT', 'POST']) {
- if (util.isFunction(window.Blob)) {
- body = new window.Blob(self._body.map(function (buffer) {
- return buffer.toArrayBuffer()
- }), {
- type: (headersObj['content-type'] || {}).value || ''
- })
+ if (opts.method === 'POST' || opts.method === 'PUT') {
+ if (capability.blobConstructor) {
+ try {
+ body = new window.Blob(self._body.map(function (buffer) {
+ return buffer.toArrayBuffer()
+ }), {
+ type: (headersObj['content-type'] || {}).value || ''
+ })
+ } catch (e) {
+ alert(e.message)
+ }
} else {
// get utf8 string
body = Buffer.concat(self._body).toString()
diff --git a/package.json b/package.json
index 6fc0361..9f8d515 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,8 @@
"license": "MIT",
"dependencies": {
"foreach": "^2.0.5",
- "object-keys": "^1.0.4"
+ "object-keys": "^1.0.4",
+ "xtend": "^4.0.0"
},
"devDependencies": {
"basic-auth": "^1.0.3",
diff --git a/test/browser/headers.js b/test/browser/headers.js
index 2b9e882..c902d44 100644
--- a/test/browser/headers.js
+++ b/test/browser/headers.js
@@ -1,5 +1,6 @@
var Buffer = require('buffer').Buffer
var fs = require('fs')
+var keys = require('object-keys')
var test = require('tape')
var http = require('../..')
@@ -35,7 +36,7 @@ test('headers', function (t) {
var body = JSON.parse(Buffer.concat(buffers).toString())
t.equal(body['test-request-header'], 'foo', 'request header 1')
t.equal(body['test-request-header-2'], 'FOO2', 'request header 2')
- t.equal(Object.keys(body).length, 2, 'correct number of request headers')
+ t.equal(keys(body).length, 2, 'correct number of request headers')
t.end()
})
diff --git a/test/browser/post-binary.js b/test/browser/post-binary.js
new file mode 100644
index 0000000..965ebaa
--- /dev/null
+++ b/test/browser/post-binary.js
@@ -0,0 +1,40 @@
+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 request bodies don't work in a bunch of browsers
+var skipVerification = ((browserName === 'Opera' && browserVersion <= 11) ||
+ (browserName === 'IE' && browserVersion <= 10) ||
+ (browserName.indexOf('Safari') !== -1 && browserVersion <= 5))
+
+var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png')
+
+test('post binary', function (t) {
+ var req = http.request({
+ path: '/echo',
+ method: 'POST'
+ }, function (res) {
+ var buffers = []
+
+ res.on('end', function () {
+ if (skipVerification)
+ t.skip('binary data not preserved on this browser')
+ else
+ t.ok(reference.equals(Buffer.concat(buffers)), 'echoed contents match')
+ t.end()
+ })
+
+ res.on('data', function (data) {
+ buffers.push(data)
+ })
+ })
+
+ req.write(reference)
+ req.end()
+})
\ No newline at end of file
diff --git a/test/browser/post-text.js b/test/browser/post-text.js
new file mode 100644
index 0000000..92bdcd2
--- /dev/null
+++ b/test/browser/post-text.js
@@ -0,0 +1,28 @@
+var Buffer = require('buffer').Buffer
+var fs = require('fs')
+var test = require('tape')
+
+var http = require('../..')
+
+var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt')
+
+test('post text', function (t) {
+ var req = http.request({
+ path: '/echo',
+ method: 'POST'
+ }, function (res) {
+ var buffers = []
+
+ res.on('end', function () {
+ t.ok(reference.equals(Buffer.concat(buffers)), 'echoed contents match')
+ t.end()
+ })
+
+ res.on('data', function (data) {
+ buffers.push(data)
+ })
+ })
+
+ req.write(reference)
+ req.end()
+})
\ No newline at end of file
diff --git a/test/server/index.js b/test/server/index.js
index 158f342..c135c19 100644
--- a/test/server/index.js
+++ b/test/server/index.js
@@ -59,6 +59,11 @@ app.get('/auth', function (req, res) {
}
})
+app.post('/echo', function (req, res) {
+ res.setHeader('Content-Type', 'application/octet-stream')
+ req.pipe(res)
+})
+
app.use(function (req, res, next) {
var parsed = url.parse(req.url, true)
diff --git a/test/server/static/polyfill.js b/test/server/static/polyfill.js
index f6a1a9d..12e50e4 100644
--- a/test/server/static/polyfill.js
+++ b/test/server/static/polyfill.js
@@ -7,3 +7,69 @@ if (!String.prototype.trim) {
};
})();
}
+
+// Production steps of ECMA-262, Edition 5, 15.4.4.14
+// Reference: http://es5.github.io/#x15.4.4.14
+if (!Array.prototype.indexOf) {
+ Array.prototype.indexOf = function(searchElement, fromIndex) {
+
+ var k;
+
+ // 1. Let O be the result of calling ToObject passing
+ // the this value as the argument.
+ if (this == null) {
+ throw new TypeError('"this" is null or not defined');
+ }
+
+ var O = Object(this);
+
+ // 2. Let lenValue be the result of calling the Get
+ // internal method of O with the argument "length".
+ // 3. Let len be ToUint32(lenValue).
+ var len = O.length >>> 0;
+
+ // 4. If len is 0, return -1.
+ if (len === 0) {
+ return -1;
+ }
+
+ // 5. If argument fromIndex was passed let n be
+ // ToInteger(fromIndex); else let n be 0.
+ var n = +fromIndex || 0;
+
+ if (Math.abs(n) === Infinity) {
+ n = 0;
+ }
+
+ // 6. If n >= len, return -1.
+ if (n >= len) {
+ return -1;
+ }
+
+ // 7. If n >= 0, then Let k be n.
+ // 8. Else, n<0, Let k be len - abs(n).
+ // If k is less than 0, then let k be 0.
+ k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
+
+ // 9. Repeat, while k < len
+ while (k < len) {
+ // a. Let Pk be ToString(k).
+ // This is implicit for LHS operands of the in operator
+ // b. Let kPresent be the result of calling the
+ // HasProperty internal method of O with argument Pk.
+ // This step can be combined with c
+ // c. If kPresent is true, then
+ // i. Let elementK be the result of calling the Get
+ // internal method of O with the argument ToString(k).
+ // ii. Let same be the result of applying the
+ // Strict Equality Comparison Algorithm to
+ // searchElement and elementK.
+ // iii. If same is true, return k.
+ if (k in O && O[k] === searchElement) {
+ return k;
+ }
+ k++;
+ }
+ return -1;
+ };
+}
--
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