Compare commits
188 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e60ce5fb99 | |||
| 9390b50f0d | |||
| 01e6e83bf9 | |||
| 00bed7dc11 | |||
| 6bdba6c1ae | |||
| faeca6025b | |||
| 00a4d3a5a8 | |||
| 270011d074 | |||
| 6f81d63645 | |||
| bd3cd9b7ce | |||
| 4c2fd5a65d | |||
| 0887fcf0b4 | |||
| 2a8467dd84 | |||
| 983a99ca6c | |||
| 718f5127ec | |||
| 65aed45b2d | |||
| 05dd964945 | |||
| 4f73bb1a3c | |||
| 663f88323c | |||
| 12a0b3fd2a | |||
| 520cc451e8 | |||
| 5c3b919dab | |||
| 5bc3a7205e | |||
| 80cbe3c5fe | |||
| c3ab1f7bf0 | |||
| 98095e85eb | |||
| 6547015c3d | |||
| 4b8abf52f3 | |||
| 472ed03ea7 | |||
| 16ec2ee996 | |||
| 8cf284cd4b | |||
| 709d1acc3d | |||
| f800fad249 | |||
| 920dd3cd48 | |||
| 433d1d5e46 | |||
| 86bfe01159 | |||
| 8188419852 | |||
| aa4882c7c6 | |||
| d62864934a | |||
| e875fc72b0 | |||
| 38ffb38660 | |||
| 16c1baf178 | |||
| 0ecf194fa4 | |||
| d4631b211c | |||
| bf580d32dd | |||
| 80f74c2107 | |||
| c2e1444670 | |||
| 4fefc64d39 | |||
| f94670b6b4 | |||
| 46c35150fc | |||
| c09a871543 | |||
| 856a572341 | |||
| 4175a65117 | |||
| e2bfae151c | |||
| 8eab4b3e5f | |||
| 46923f37d9 | |||
| 2771d95bde | |||
| 7c2e61fac0 | |||
| 1dc9ea32f5 | |||
| e8ed108767 | |||
| 4cf6771177 | |||
| e54de15542 | |||
| 943f5bc89d | |||
| 1c25d6f3a3 | |||
| b63537f11d | |||
| 11d14fc642 | |||
| 8e1f534a8a | |||
| c1417c5dfa | |||
| 8df10e0d65 | |||
| 5ee30ad5cc | |||
| 9321e56abd | |||
| 881e7c8f3d | |||
| 43761f2799 | |||
| 70f8c7d0b7 | |||
| f304ef13a2 | |||
| e8ef98b286 | |||
| 3af33f862f | |||
| 138d6c1d29 | |||
| f97dd885ea | |||
| f9fb1c724c | |||
| 1b077bf249 | |||
| e342cdbb10 | |||
| 0a04a09f68 | |||
| 8c07b21b82 | |||
| 662b9626be | |||
| abdc5573da | |||
| 525957277a | |||
| 4e7103a5a9 | |||
| 34aa684789 | |||
| 11ac890744 | |||
| 33587da68d | |||
| 5cf0329683 | |||
| 0cc4549d4e | |||
| 64e2d89062 | |||
| 57d0ab245f | |||
| 4b9bbe04b2 | |||
| 92500c71be | |||
| 05c6f4394c | |||
| 9ed7614aed | |||
| bad84ecb3a | |||
| 70b928bbb9 | |||
| 53bf1d38e2 | |||
| ac250a9b04 | |||
| 83f7b7ef32 | |||
| f3df5d7ace | |||
| 345149d708 | |||
| 9c18855feb | |||
| ff601a9eff | |||
| e046049394 | |||
| 333b0437a9 | |||
| 0552e1e4f5 | |||
| 1fbfc4264a | |||
| a0c6d49ba4 | |||
| 1d3fca03fa | |||
| 5e25ed2f65 | |||
| b339f4afa9 | |||
| 4b1419cff7 | |||
| c9183cd2ad | |||
| 0f1f0c6365 | |||
| 581a4d12da | |||
| 945083bd1d | |||
| 2054c18374 | |||
| 218fd54ef0 | |||
| 9d24d5510a | |||
| ff68130d89 | |||
| 6bab67d17a | |||
| 6433ea8cad | |||
| 9ebf801ebb | |||
| 154f6b9e56 | |||
| 6dea87e704 | |||
| 8b9f5a43c1 | |||
| 3c8b44379c | |||
| c36da3bade | |||
| cbc0bd803c | |||
| d6de2ce3a5 | |||
| 4bcff63d46 | |||
| a69310d8fc | |||
| ae48c2c71c | |||
| 570f85ec18 | |||
| 85381f0e3d | |||
| 3bea2a493f | |||
| 016f4be96f | |||
| 9e5e75b5ff | |||
| 031b8aab89 | |||
| dc421eb355 | |||
| 7d1dfd2ab1 | |||
| 75817ba930 | |||
| 2152af414a | |||
| a10d19b3b2 | |||
| a169930a20 | |||
| 43d1322dd6 | |||
| f72d22b2a9 | |||
| bde26be949 | |||
| 8cab16f26e | |||
| 166795fc05 | |||
| ff339bd64c | |||
| 5986071393 | |||
| bbf1fd81f0 | |||
| 6b9d3d107c | |||
| 3fda0048d7 | |||
| bef2b95708 | |||
| efed69e6e2 | |||
| caa40a0950 | |||
| 168dbac90c | |||
| a91ed0db64 | |||
| da4085e33d | |||
| 6d17b406a9 | |||
| 1a4a89a5e4 | |||
| 838aaffac7 | |||
| f70ac39bf4 | |||
| ed525a4a2d | |||
| b162e21d92 | |||
| c8c0966c63 | |||
| efaad2a3c7 | |||
| 9059849673 | |||
| aeabb2cc70 | |||
| 588e5874f3 | |||
| 5fc4858d6d | |||
| 22b3cc7acb | |||
| 5ffc869490 | |||
| cc74bc83c8 | |||
| 5e81240a05 | |||
| 29a2ad2032 | |||
| 6b544c54a3 | |||
| 734974d279 | |||
| 57ce59c207 | |||
| 15181f5586 | |||
| 916ab55402 |
+22
@@ -0,0 +1,22 @@
|
||||
sauce_connect: true
|
||||
browsers:
|
||||
- name: chrome
|
||||
version: 39..latest
|
||||
- name: firefox
|
||||
version: 34..latest
|
||||
- name: safari
|
||||
version: 8..latest
|
||||
- name: MicrosoftEdge
|
||||
version: 13..latest
|
||||
- name: ie
|
||||
version: 11
|
||||
- name: iphone
|
||||
version: '9.3..latest'
|
||||
- name: android
|
||||
version: '6.0..6.0' # TODO: change this back to latest once https://github.com/airtap/browsers/issues/3 is fixed
|
||||
server: ./test/server/index.js
|
||||
scripts:
|
||||
- "/test-polyfill.js"
|
||||
browserify:
|
||||
- options:
|
||||
dedupe: false
|
||||
+1
-1
@@ -2,4 +2,4 @@
|
||||
bundle.js
|
||||
node_modules
|
||||
npm-debug.log
|
||||
.zuulrc
|
||||
.airtaprc
|
||||
|
||||
+5
-1
@@ -1,3 +1,7 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.12"
|
||||
- "node"
|
||||
addons:
|
||||
sauce_connect: true
|
||||
hosts:
|
||||
- airtap.local
|
||||
@@ -1,19 +0,0 @@
|
||||
ui: tape
|
||||
browsers:
|
||||
- name: chrome
|
||||
version: 39..latest
|
||||
- name: firefox
|
||||
version: 34..latest
|
||||
- name: safari
|
||||
version: 5..latest
|
||||
- name: ie
|
||||
version: 8..latest
|
||||
- name: opera
|
||||
version: 11..latest
|
||||
- name: iphone
|
||||
version: 5.1..latest
|
||||
- name: android
|
||||
version: 4.0..latest
|
||||
server: ./test/server/index.js
|
||||
scripts:
|
||||
- "/polyfill.js"
|
||||
@@ -1,77 +1,101 @@
|
||||
# stream-http [](https://travis-ci.org/jhiesey/stream-http)
|
||||
|
||||
[](https://saucelabs.com/u/jhiesey)
|
||||
[](https://saucelabs.com/u/stream-http)
|
||||
|
||||
This module is an implementation of node's native `http` module for the browser.
|
||||
It tries to match node's api and behavior as closely as possible, but some features
|
||||
This module is an implementation of Node's native `http` module for the browser.
|
||||
It tries to match Node's API and behavior as closely as possible, but some features
|
||||
aren't available, since browsers don't give nearly as much control over requests.
|
||||
|
||||
This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify)
|
||||
This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify).
|
||||
|
||||
## What does it do?
|
||||
|
||||
In accordance with its name, `stream-http` tries to provide data to its caller before
|
||||
the request has completed whenever possible.
|
||||
|
||||
Backpressure, allowing the browser to only pull data from the server as fast as it is
|
||||
consumed, is supported in:
|
||||
* Chrome >= 58 (using `fetch` and `WritableStream`)
|
||||
|
||||
The following browsers support true streaming, where only a small amount of the request
|
||||
has to be held in memory at once:
|
||||
* Chrome >= 43 (using the `fetch` api)
|
||||
* Chrome >= 43 (using the `fetch` API)
|
||||
* Firefox >= 9 (using `moz-chunked-arraybuffer` responseType with xhr)
|
||||
|
||||
The following browsers support pseudo-streaming, where the data is available before the
|
||||
request finishes, but the entire response must be held in memory:
|
||||
* Chrome
|
||||
* Safari >= 5, and maybe older
|
||||
* IE >= 10
|
||||
* Most other Webkit-based browsers, including the default Android browser
|
||||
All other supported browsers support pseudo-streaming, where the data is available before
|
||||
the request finishes, but the entire response must be held in memory. This works for both
|
||||
text and binary data.
|
||||
|
||||
All browsers newer than IE8 support binary responses. All of the above browsers that
|
||||
support true streaming or pseudo-streaming support that for binary data as well
|
||||
except for IE10. Old (presto-based) Opera also does not support binary streaming either.
|
||||
### IE note:
|
||||
As of version 3.0.0, IE10 and below are no longer supported. IE11 support will remain for
|
||||
now.
|
||||
|
||||
## How do you use it?
|
||||
|
||||
The intent is to have the same api as the client part of the
|
||||
[node HTTP module](https://nodejs.org/api/http.html). The interfaces are the same wherever
|
||||
practical, although limitations in browsers make an exact clone of the node api impossible.
|
||||
The intent is to have the same API as the client part of the
|
||||
[Node HTTP module](https://nodejs.org/api/http.html). The interfaces are the same wherever
|
||||
practical, although limitations in browsers make an exact clone of the Node API impossible.
|
||||
|
||||
This module implements `http.request`, `http.get`, and most of `http.ClientRequest`
|
||||
and `http.IncomingMessage` in addition to `http.METHODS` and `http.STATUS_CODES`. See the
|
||||
node docs for how these work.
|
||||
Node docs for how these work.
|
||||
|
||||
### Extra features compared to node
|
||||
### Extra features compared to Node
|
||||
|
||||
* The `message.url` property provides access to the final URL after all redirects. This
|
||||
is useful since the browser follows all redirects silently, unlike Node. It is available
|
||||
in Chrome 37 and newer, Firefox 32 and newer, and Safari 9 and newer.
|
||||
|
||||
* The `options.withCredentials` boolean flag, used to indicate if the browser should send
|
||||
cookies or authentication information with a CORS request. Default true.
|
||||
cookies or authentication information with a CORS request. Default false.
|
||||
|
||||
This module has to make some tradeoffs to support binary data and/or streaming. Generally,
|
||||
the module can make a fairly good decision about which underlying browser features to use,
|
||||
but sometimes it helps to get a little input from the user.
|
||||
but sometimes it helps to get a little input from the developer.
|
||||
|
||||
* The `options.mode` field passed into `http.request` or `http.get` can take on one of the
|
||||
following values:
|
||||
* 'default' (or any falsy value, including undefined): Try to provide partial data before
|
||||
the equest completes, but not at the cost of correctness for binary data. In some cases
|
||||
the implementation may be a bit slow.
|
||||
* 'default' (or any falsy value, including `undefined`): Try to provide partial data before
|
||||
the request completes, but not at the cost of correctness for binary data or correctness of
|
||||
the 'content-type' response header. This mode will also avoid slower code paths whenever
|
||||
possible, which is particularly useful when making large requests in a browser like Safari
|
||||
that has a weaker JavaScript engine.
|
||||
* 'allow-wrong-content-type': Provides partial data in more cases than 'default', but
|
||||
at the expense of causing the 'content-type' response header to be incorrectly reported
|
||||
(as 'text/plain; charset=x-user-defined') in some browsers, notably Safari and Chrome 42
|
||||
and older. Preserves binary data whenever possible. In some cases the implementation may
|
||||
also be a bit slow. This was the default in versions of this module before 1.5.
|
||||
* 'prefer-stream': Provide data before the request completes even if binary data (anything
|
||||
that isn't a single-byte ASCII or utf8 character) will be corrupted. Of course, this option
|
||||
is only safe for text data.
|
||||
* 'prefer-fast': Use an implementation that does less processing even if it means that
|
||||
partial data isn't available. This is particularly useful when making large requests in
|
||||
a browser like Safari that has a weaker javascript engine.
|
||||
that isn't a single-byte ASCII or UTF8 character) will be corrupted. Of course, this option
|
||||
is only safe for text data. May also cause the 'content-type' response header to be
|
||||
incorrectly reported (as 'text/plain; charset=x-user-defined').
|
||||
* 'disable-fetch': Force the use of plain XHR regardless of the browser declaring a fetch
|
||||
capability. Preserves the correctness of binary data and the 'content-type' response header.
|
||||
* 'prefer-fast': Deprecated; now a synonym for 'default', which has the same performance
|
||||
characteristics as this mode did in versions before 1.5.
|
||||
|
||||
### Features missing compared to node
|
||||
* `options.requestTimeout` allows setting a timeout in millisecionds for XHR and fetch (if
|
||||
supported by the browser). This is a limit on how long the entire process takes from
|
||||
beginning to end. Note that this is not the same as the node `setTimeout` functions,
|
||||
which apply to pauses in data transfer over the underlying socket, or the node `timeout`
|
||||
option, which applies to opening the connection.
|
||||
|
||||
### Features missing compared to Node
|
||||
|
||||
* `http.Agent` is only a stub
|
||||
* The 'socket', 'connect', 'upgrade', and 'continue' events on `http.ClientRequest`.
|
||||
* Any operations, including `request.setTimeout`, that operate directly on the underlying
|
||||
socket.
|
||||
* Any options that are disallowed for security reasons. This includes setting or getting
|
||||
certian headers.
|
||||
certain headers.
|
||||
* `message.httpVersion`
|
||||
* `message.rawHeaders` is modified by the browser, and may not quite match what is sent by
|
||||
the server.
|
||||
* `message.trailers` and `message.rawTrailers` will remain empty.
|
||||
* Redirects are followed silently by the browser, so it isn't possible to access the 301/302
|
||||
redirect pages.
|
||||
* The `timeout` event/option and `setTimeout` functions, which operate on the underlying
|
||||
socket, are not available. However, see `options.requestTimeout` above.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -79,17 +103,34 @@ the server.
|
||||
http.get('/bundle.js', function (res) {
|
||||
var div = document.getElementById('result');
|
||||
div.innerHTML += 'GET /beep<br>';
|
||||
|
||||
|
||||
res.on('data', function (buf) {
|
||||
div.innerHTML += buf;
|
||||
});
|
||||
|
||||
|
||||
res.on('end', function () {
|
||||
div.innerHTML += '<br>__END__';
|
||||
});
|
||||
})
|
||||
```
|
||||
|
||||
## Running tests
|
||||
|
||||
There are two sets of tests: the tests that run in Node (found in `test/node`) and the tests
|
||||
that run in the browser (found in `test/browser`). Normally the browser tests run on
|
||||
[Sauce Labs](http://saucelabs.com/).
|
||||
|
||||
Running `npm test` will run both sets of tests, but in order for the Sauce Labs tests to run
|
||||
you will need to sign up for an account (free for open source projects) and put the
|
||||
credentials in a [`.airtaprc` file](https://github.com/airtap/airtap/blob/master/doc/airtaprc.md).
|
||||
You will also need to run a [Sauce Connect Proxy](https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy)
|
||||
with the same credentials.
|
||||
|
||||
To run just the Node tests, run `npm run test-node`.
|
||||
|
||||
To run the browser tests locally, run `npm run test-browser-local` and point your browser to
|
||||
the link shown in your terminal.
|
||||
|
||||
## License
|
||||
|
||||
MIT. Copyright (C) John Hiesey and other contributors.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var ClientRequest = require('./lib/request')
|
||||
var response = require('./lib/response')
|
||||
var extend = require('xtend')
|
||||
var statusCodes = require('builtin-status-codes')
|
||||
var url = require('url')
|
||||
@@ -11,22 +12,24 @@ http.request = function (opts, cb) {
|
||||
else
|
||||
opts = extend(opts)
|
||||
|
||||
// Split opts.host into its components
|
||||
var hostHostname = opts.host ? opts.host.split(':')[0] : null
|
||||
var hostPort = opts.host ? parseInt(opts.host.split(':')[1], 10) : null
|
||||
// Normally, the page is loaded from http or https, so not specifying a protocol
|
||||
// will result in a (valid) protocol-relative url. However, this won't work if
|
||||
// the protocol is something else, like 'file:'
|
||||
var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
|
||||
|
||||
opts.method = opts.method || 'GET'
|
||||
var protocol = opts.protocol || defaultProtocol
|
||||
var host = opts.hostname || opts.host
|
||||
var port = opts.port
|
||||
var path = opts.path || '/'
|
||||
|
||||
// Necessary for IPv6 addresses
|
||||
if (host && host.indexOf(':') !== -1)
|
||||
host = '[' + host + ']'
|
||||
|
||||
// This may be a relative url. The browser should always be able to interpret it correctly.
|
||||
opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
|
||||
opts.method = (opts.method || 'GET').toUpperCase()
|
||||
opts.headers = opts.headers || {}
|
||||
opts.path = opts.path || '/'
|
||||
opts.protocol = opts.protocol || window.location.protocol
|
||||
// If the hostname is provided, use the default port for the protocol. If
|
||||
// the url is instead relative, use window.location.port
|
||||
var defaultPort = (opts.hostname || hostHostname) ? (opts.protocol === 'https:' ? 443 : 80) : window.location.port
|
||||
opts.hostname = opts.hostname || hostHostname || window.location.hostname
|
||||
opts.port = opts.port || hostPort || defaultPort
|
||||
|
||||
if (opts.withCredentials === undefined)
|
||||
opts.withCredentials = true
|
||||
|
||||
// Also valid opts.auth, opts.mode
|
||||
|
||||
@@ -42,14 +45,41 @@ http.get = function get (opts, cb) {
|
||||
return req
|
||||
}
|
||||
|
||||
http.ClientRequest = ClientRequest
|
||||
http.IncomingMessage = response.IncomingMessage
|
||||
|
||||
http.Agent = function () {}
|
||||
http.Agent.defaultMaxSockets = 4
|
||||
|
||||
http.globalAgent = new http.Agent()
|
||||
|
||||
http.STATUS_CODES = statusCodes
|
||||
|
||||
http.METHODS = [
|
||||
'CHECKOUT',
|
||||
'CONNECT',
|
||||
'COPY',
|
||||
'DELETE',
|
||||
'GET',
|
||||
'HEAD',
|
||||
'LOCK',
|
||||
'M-SEARCH',
|
||||
'MERGE',
|
||||
'MKACTIVITY',
|
||||
'MKCOL',
|
||||
'MOVE',
|
||||
'NOTIFY',
|
||||
'OPTIONS',
|
||||
'PATCH',
|
||||
'POST',
|
||||
'PROPFIND',
|
||||
'PROPPATCH',
|
||||
'PURGE',
|
||||
'PUT',
|
||||
'DELETE' // TODO: include the methods from RFC 2616 and 2518?
|
||||
]
|
||||
'REPORT',
|
||||
'SEARCH',
|
||||
'SUBSCRIBE',
|
||||
'TRACE',
|
||||
'UNLOCK',
|
||||
'UNSUBSCRIBE'
|
||||
]
|
||||
+42
-16
@@ -1,15 +1,37 @@
|
||||
exports.fetch = isFunction(window.fetch) && isFunction(window.ReadableByteStream)
|
||||
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream)
|
||||
|
||||
exports.blobConstructor = false
|
||||
try {
|
||||
new Blob([new ArrayBuffer(1)])
|
||||
exports.blobConstructor = true
|
||||
} catch (e) {}
|
||||
exports.writableStream = isFunction(global.WritableStream)
|
||||
|
||||
var xhr = new window.XMLHttpRequest()
|
||||
xhr.open('GET', '/')
|
||||
exports.abortController = isFunction(global.AbortController)
|
||||
|
||||
// The xhr request to example.com may violate some restrictive CSP configurations,
|
||||
// so if we're running in a browser that supports `fetch`, avoid calling getXHR()
|
||||
// and assume support for certain features below.
|
||||
var xhr
|
||||
function getXHR () {
|
||||
// Cache the xhr value
|
||||
if (xhr !== undefined) return xhr
|
||||
|
||||
if (global.XMLHttpRequest) {
|
||||
xhr = new global.XMLHttpRequest()
|
||||
// If XDomainRequest is available (ie only, where xhr might not work
|
||||
// cross domain), use the page location. Otherwise use example.com
|
||||
// Note: this doesn't actually make an http request.
|
||||
try {
|
||||
xhr.open('GET', global.XDomainRequest ? '/' : 'https://example.com')
|
||||
} catch(e) {
|
||||
xhr = null
|
||||
}
|
||||
} else {
|
||||
// Service workers don't have XHR
|
||||
xhr = null
|
||||
}
|
||||
return xhr
|
||||
}
|
||||
|
||||
function checkTypeSupport (type) {
|
||||
var xhr = getXHR()
|
||||
if (!xhr) return false
|
||||
try {
|
||||
xhr.responseType = type
|
||||
return xhr.responseType === type
|
||||
@@ -17,17 +39,21 @@ function checkTypeSupport (type) {
|
||||
return false
|
||||
}
|
||||
|
||||
var haveArrayBuffer = isFunction(window.ArrayBuffer)
|
||||
var haveSlice = haveArrayBuffer && isFunction(window.ArrayBuffer.prototype.slice)
|
||||
// If fetch is supported, then arraybuffer will be supported too. Skip calling
|
||||
// checkTypeSupport(), since that calls getXHR().
|
||||
exports.arraybuffer = exports.fetch || checkTypeSupport('arraybuffer')
|
||||
|
||||
exports.arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer')
|
||||
exports.msstream = haveSlice && checkTypeSupport('ms-stream')
|
||||
exports.mozchunkedarraybuffer = haveArrayBuffer && checkTypeSupport('moz-chunked-arraybuffer')
|
||||
exports.overrideMimeType = isFunction(xhr.overrideMimeType)
|
||||
exports.vbArray = isFunction(window.VBArray)
|
||||
// These next two tests unavoidably show warnings in Chrome. Since fetch will always
|
||||
// be used if it's available, just return false for these to avoid the warnings.
|
||||
exports.msstream = !exports.fetch && checkTypeSupport('ms-stream')
|
||||
exports.mozchunkedarraybuffer = !exports.fetch && checkTypeSupport('moz-chunked-arraybuffer')
|
||||
|
||||
// If fetch is supported, then overrideMimeType will be supported too. Skip calling
|
||||
// getXHR().
|
||||
exports.overrideMimeType = exports.fetch || (getXHR() ? isFunction(getXHR().overrideMimeType) : false)
|
||||
|
||||
function isFunction (value) {
|
||||
return typeof value === 'function'
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
xhr = null // Help gc
|
||||
|
||||
+136
-57
@@ -1,16 +1,13 @@
|
||||
// var Base64 = require('Base64')
|
||||
var capability = require('./capability')
|
||||
var foreach = require('foreach')
|
||||
var keys = require('object-keys')
|
||||
var response = require('./response')
|
||||
var stream = require('stream')
|
||||
var inherits = require('inherits')
|
||||
var response = require('./response')
|
||||
var stream = require('readable-stream')
|
||||
|
||||
var IncomingMessage = response.IncomingMessage
|
||||
var rStates = response.readyStates
|
||||
|
||||
function decideMode (preferBinary) {
|
||||
if (capability.fetch) {
|
||||
function decideMode (preferBinary, useFetch) {
|
||||
if (capability.fetch && useFetch) {
|
||||
return 'fetch'
|
||||
} else if (capability.mozchunkedarraybuffer) {
|
||||
return 'moz-chunked-arraybuffer'
|
||||
@@ -18,8 +15,6 @@ function decideMode (preferBinary) {
|
||||
return 'ms-stream'
|
||||
} else if (capability.arraybuffer && preferBinary) {
|
||||
return 'arraybuffer'
|
||||
} else if (capability.vbArray && preferBinary) {
|
||||
return 'text:vbarray'
|
||||
} else {
|
||||
return 'text'
|
||||
}
|
||||
@@ -30,29 +25,35 @@ var ClientRequest = module.exports = function (opts) {
|
||||
stream.Writable.call(self)
|
||||
|
||||
self._opts = 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('Authorization', 'Basic ' + Buffer.from(opts.auth).toString('base64'))
|
||||
Object.keys(opts.headers).forEach(function (name) {
|
||||
self.setHeader(name, opts.headers[name])
|
||||
})
|
||||
|
||||
var preferBinary
|
||||
if (opts.mode === 'prefer-streaming') {
|
||||
// If streaming is a high priority but binary compatibility isn't
|
||||
preferBinary = false
|
||||
} else if (opts.mode === 'prefer-fast') {
|
||||
// If binary is preferred for speed
|
||||
var useFetch = true
|
||||
if (opts.mode === 'disable-fetch' || ('requestTimeout' in opts && !capability.abortController)) {
|
||||
// If the use of XHR should be preferred. Not typically needed.
|
||||
useFetch = false
|
||||
preferBinary = true
|
||||
} else if (!opts.mode || opts.mode === 'default') {
|
||||
// By default, use binary if text streaming may corrupt data
|
||||
} else if (opts.mode === 'prefer-streaming') {
|
||||
// If streaming is a high priority but binary compatibility and
|
||||
// the accuracy of the 'content-type' header aren't
|
||||
preferBinary = false
|
||||
} else if (opts.mode === 'allow-wrong-content-type') {
|
||||
// If streaming is more important than preserving the 'content-type' header
|
||||
preferBinary = !capability.overrideMimeType
|
||||
} else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') {
|
||||
// Use binary if text streaming may corrupt data or the content-type header, or for speed
|
||||
preferBinary = true
|
||||
} else {
|
||||
throw new Error('Invalid value for opts.mode')
|
||||
}
|
||||
self._mode = decideMode(preferBinary)
|
||||
self._mode = decideMode(preferBinary, useFetch)
|
||||
self._fetchTimer = null
|
||||
|
||||
self.on('finish', function () {
|
||||
self._onFinish()
|
||||
@@ -63,15 +64,24 @@ inherits(ClientRequest, stream.Writable)
|
||||
|
||||
ClientRequest.prototype.setHeader = function (name, value) {
|
||||
var self = this
|
||||
self._headers[name.toLowerCase()] = {
|
||||
var lowerName = name.toLowerCase()
|
||||
// This check is not necessary, but it prevents warnings from browsers about setting unsafe
|
||||
// headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but
|
||||
// http-browserify did it, so I will too.
|
||||
if (unsafeHeaders.indexOf(lowerName) !== -1)
|
||||
return
|
||||
|
||||
self._headers[lowerName] = {
|
||||
name: name,
|
||||
value: value
|
||||
}
|
||||
}
|
||||
|
||||
ClientRequest.prototype.getHeader = function (name) {
|
||||
var self = this
|
||||
return self._headers[name.toLowerCase()].value
|
||||
var header = this._headers[name.toLowerCase()]
|
||||
if (header)
|
||||
return header.value
|
||||
return null
|
||||
}
|
||||
|
||||
ClientRequest.prototype.removeHeader = function (name) {
|
||||
@@ -82,44 +92,68 @@ ClientRequest.prototype.removeHeader = function (name) {
|
||||
ClientRequest.prototype._onFinish = function () {
|
||||
var self = this
|
||||
|
||||
if (self._destroyed)
|
||||
return
|
||||
var opts = self._opts
|
||||
|
||||
var headersObj = self._headers
|
||||
var body
|
||||
if (opts.method === 'POST' || opts.method === 'PUT') {
|
||||
if (capability.blobConstructor) {
|
||||
body = new window.Blob(self._body.map(function (buffer) {
|
||||
return buffer.toArrayBuffer()
|
||||
}), {
|
||||
type: (headersObj['content-type'] || {}).value || ''
|
||||
var body = null
|
||||
if (opts.method !== 'GET' && opts.method !== 'HEAD') {
|
||||
body = new Blob(self._body, {
|
||||
type: (headersObj['content-type'] || {}).value || ''
|
||||
});
|
||||
}
|
||||
|
||||
// create flattened list of headers
|
||||
var headersList = []
|
||||
Object.keys(headersObj).forEach(function (keyName) {
|
||||
var name = headersObj[keyName].name
|
||||
var value = headersObj[keyName].value
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(function (v) {
|
||||
headersList.push([name, v])
|
||||
})
|
||||
} else {
|
||||
// get utf8 string
|
||||
body = Buffer.concat(self._body).toString()
|
||||
headersList.push([name, value])
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (self._mode === 'fetch') {
|
||||
var headers = keys(headersObj).map(function (name) {
|
||||
return [headersObj[name].name, headersObj[name].value]
|
||||
})
|
||||
var signal = null
|
||||
var fetchTimer = null
|
||||
if (capability.abortController) {
|
||||
var controller = new AbortController()
|
||||
signal = controller.signal
|
||||
self._fetchAbortController = controller
|
||||
|
||||
window.fetch(self._url, {
|
||||
if ('requestTimeout' in opts && opts.requestTimeout !== 0) {
|
||||
self._fetchTimer = global.setTimeout(function () {
|
||||
self.emit('requestTimeout')
|
||||
if (self._fetchAbortController)
|
||||
self._fetchAbortController.abort()
|
||||
}, opts.requestTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
global.fetch(self._opts.url, {
|
||||
method: self._opts.method,
|
||||
headers: headers,
|
||||
body: body,
|
||||
headers: headersList,
|
||||
body: body || undefined,
|
||||
mode: 'cors',
|
||||
credentials: opts.withCredentials ? 'include' : 'omit'
|
||||
credentials: opts.withCredentials ? 'include' : 'same-origin',
|
||||
signal: signal
|
||||
}).then(function (response) {
|
||||
self._fetchResponse = response
|
||||
self._connect()
|
||||
}).then(undefined, function (reason) {
|
||||
self.emit('error', reason)
|
||||
}, function (reason) {
|
||||
global.clearTimeout(self._fetchTimer)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', reason)
|
||||
})
|
||||
} else {
|
||||
var xhr = self._xhr = new window.XMLHttpRequest()
|
||||
var xhr = self._xhr = new global.XMLHttpRequest()
|
||||
try {
|
||||
xhr.open(self._opts.method, self._url, true)
|
||||
xhr.open(self._opts.method, self._opts.url, true)
|
||||
} catch (err) {
|
||||
process.nextTick(function () {
|
||||
self.emit('error', err)
|
||||
@@ -129,7 +163,7 @@ ClientRequest.prototype._onFinish = function () {
|
||||
|
||||
// Can't set responseType on really old browsers
|
||||
if ('responseType' in xhr)
|
||||
xhr.responseType = self._mode.split(':')[0]
|
||||
xhr.responseType = self._mode
|
||||
|
||||
if ('withCredentials' in xhr)
|
||||
xhr.withCredentials = !!opts.withCredentials
|
||||
@@ -137,8 +171,15 @@ ClientRequest.prototype._onFinish = function () {
|
||||
if (self._mode === 'text' && 'overrideMimeType' in xhr)
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined')
|
||||
|
||||
foreach(keys(headersObj), function (name) {
|
||||
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
|
||||
if ('requestTimeout' in opts) {
|
||||
xhr.timeout = opts.requestTimeout
|
||||
xhr.ontimeout = function () {
|
||||
self.emit('requestTimeout')
|
||||
}
|
||||
}
|
||||
|
||||
headersList.forEach(function (header) {
|
||||
xhr.setRequestHeader(header[0], header[1])
|
||||
})
|
||||
|
||||
self._response = null
|
||||
@@ -159,6 +200,8 @@ ClientRequest.prototype._onFinish = function () {
|
||||
}
|
||||
|
||||
xhr.onerror = function () {
|
||||
if (self._destroyed)
|
||||
return
|
||||
self.emit('error', new Error('XHR error'))
|
||||
}
|
||||
|
||||
@@ -174,12 +217,14 @@ 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
|
||||
* Checks if xhr.status is readable and non-zero, indicating no error.
|
||||
* 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)
|
||||
var status = xhr.status
|
||||
return (status !== null && status !== 0)
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
@@ -188,7 +233,7 @@ function statusValid (xhr) {
|
||||
ClientRequest.prototype._onXHRProgress = function () {
|
||||
var self = this
|
||||
|
||||
if (!statusValid(self._xhr) || self._failed)
|
||||
if (!statusValid(self._xhr) || self._destroyed)
|
||||
return
|
||||
|
||||
if (!self._response)
|
||||
@@ -200,7 +245,14 @@ ClientRequest.prototype._onXHRProgress = function () {
|
||||
ClientRequest.prototype._connect = function () {
|
||||
var self = this
|
||||
|
||||
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
|
||||
if (self._destroyed)
|
||||
return
|
||||
|
||||
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode, self._fetchTimer)
|
||||
self._response.on('error', function(err) {
|
||||
self.emit('error', err)
|
||||
})
|
||||
|
||||
self.emit('response', self._response)
|
||||
}
|
||||
|
||||
@@ -211,10 +263,16 @@ ClientRequest.prototype._write = function (chunk, encoding, cb) {
|
||||
cb()
|
||||
}
|
||||
|
||||
ClientRequest.prototype.abort = function () {
|
||||
ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function () {
|
||||
var self = this
|
||||
self._destroyed = true
|
||||
global.clearTimeout(self._fetchTimer)
|
||||
if (self._response)
|
||||
self._response._destroyed = true
|
||||
if (self._xhr)
|
||||
self._xhr.abort()
|
||||
else if (self._fetchAbortController)
|
||||
self._fetchAbortController.abort()
|
||||
}
|
||||
|
||||
ClientRequest.prototype.end = function (data, encoding, cb) {
|
||||
@@ -224,13 +282,34 @@ ClientRequest.prototype.end = function (data, encoding, cb) {
|
||||
data = undefined
|
||||
}
|
||||
|
||||
if (data)
|
||||
stream.Writable.push.call(self, data, encoding)
|
||||
|
||||
stream.Writable.prototype.end.call(self, cb)
|
||||
stream.Writable.prototype.end.call(self, data, encoding, cb)
|
||||
}
|
||||
|
||||
ClientRequest.prototype.flushHeaders = function () {}
|
||||
ClientRequest.prototype.setTimeout = function () {}
|
||||
ClientRequest.prototype.setNoDelay = function () {}
|
||||
ClientRequest.prototype.setSocketKeepAlive = function () {}
|
||||
|
||||
// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
|
||||
var unsafeHeaders = [
|
||||
'accept-charset',
|
||||
'accept-encoding',
|
||||
'access-control-request-headers',
|
||||
'access-control-request-method',
|
||||
'connection',
|
||||
'content-length',
|
||||
'cookie',
|
||||
'cookie2',
|
||||
'date',
|
||||
'dnt',
|
||||
'expect',
|
||||
'host',
|
||||
'keep-alive',
|
||||
'origin',
|
||||
'referer',
|
||||
'te',
|
||||
'trailer',
|
||||
'transfer-encoding',
|
||||
'upgrade',
|
||||
'via'
|
||||
]
|
||||
|
||||
+84
-40
@@ -1,7 +1,6 @@
|
||||
var capability = require('./capability')
|
||||
var foreach = require('foreach')
|
||||
var stream = require('stream')
|
||||
var inherits = require('inherits')
|
||||
var stream = require('readable-stream')
|
||||
|
||||
var rStates = exports.readyStates = {
|
||||
UNSENT: 0,
|
||||
@@ -11,7 +10,7 @@ var rStates = exports.readyStates = {
|
||||
DONE: 4
|
||||
}
|
||||
|
||||
var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode, fetchTimer) {
|
||||
var self = this
|
||||
stream.Readable.call(self)
|
||||
|
||||
@@ -21,47 +20,101 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
self.trailers = {}
|
||||
self.rawTrailers = []
|
||||
|
||||
// Fake the 'close' event, but only once 'end' fires
|
||||
self.on('end', function () {
|
||||
// The nextTick is necessary to prevent the 'request' module from causing an infinite loop
|
||||
process.nextTick(function () {
|
||||
self.emit('close')
|
||||
})
|
||||
})
|
||||
|
||||
if (mode === 'fetch') {
|
||||
self._fetchResponse = response
|
||||
|
||||
self.url = response.url
|
||||
self.statusCode = response.status
|
||||
self.statusMessage = response.statusText
|
||||
// backwards compatible version of for (<item> of <iterable>):
|
||||
// for (var <item>,_i,_it = <iterable>[Symbol.iterator](); <item> = (_i = _it.next()).value,!_i.done;)
|
||||
for (var header, _i, _it = response.headers[Symbol.iterator](); header = (_i = _it.next()).value, !_i.done;) {
|
||||
self.headers[header[0].toLowerCase()] = header[1]
|
||||
self.rawHeaders.push(header[0], header[1])
|
||||
}
|
||||
|
||||
response.headers.forEach(function (header, key){
|
||||
self.headers[key.toLowerCase()] = header
|
||||
self.rawHeaders.push(key, header)
|
||||
})
|
||||
|
||||
// TODO: this doesn't respect backpressure. Once WritableStream is available, this can be fixed
|
||||
if (capability.writableStream) {
|
||||
var writable = new WritableStream({
|
||||
write: function (chunk) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (self._destroyed) {
|
||||
reject()
|
||||
} else if(self.push(Buffer.from(chunk))) {
|
||||
resolve()
|
||||
} else {
|
||||
self._resumeFetch = resolve
|
||||
}
|
||||
})
|
||||
},
|
||||
close: function () {
|
||||
global.clearTimeout(fetchTimer)
|
||||
if (!self._destroyed)
|
||||
self.push(null)
|
||||
},
|
||||
abort: function (err) {
|
||||
if (!self._destroyed)
|
||||
self.emit('error', err)
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
response.body.pipeTo(writable).catch(function (err) {
|
||||
global.clearTimeout(fetchTimer)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', err)
|
||||
})
|
||||
return
|
||||
} catch (e) {} // pipeTo method isn't defined. Can't find a better way to feature test this
|
||||
}
|
||||
// fallback for when writableStream or pipeTo aren't available
|
||||
var reader = response.body.getReader()
|
||||
function read () {
|
||||
reader.read().then(function (result) {
|
||||
if (self._destroyed)
|
||||
return
|
||||
if (result.done) {
|
||||
global.clearTimeout(fetchTimer)
|
||||
self.push(null)
|
||||
return
|
||||
}
|
||||
self.push(new Buffer(result.value))
|
||||
self.push(Buffer.from(result.value))
|
||||
read()
|
||||
}).catch(function (err) {
|
||||
global.clearTimeout(fetchTimer)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', err)
|
||||
})
|
||||
}
|
||||
read()
|
||||
|
||||
} else {
|
||||
self._xhr = xhr
|
||||
self._pos = 0
|
||||
|
||||
self.url = xhr.responseURL
|
||||
self.statusCode = xhr.status
|
||||
self.statusMessage = xhr.statusText
|
||||
var headers = xhr.getAllResponseHeaders().split(/\r?\n/)
|
||||
foreach(headers, function (header) {
|
||||
headers.forEach(function (header) {
|
||||
var matches = header.match(/^([^:]+):\s*(.*)/)
|
||||
if (matches) {
|
||||
var key = matches[1].toLowerCase()
|
||||
if (self.headers[key] !== undefined)
|
||||
if (key === 'set-cookie') {
|
||||
if (self.headers[key] === undefined) {
|
||||
self.headers[key] = []
|
||||
}
|
||||
self.headers[key].push(matches[2])
|
||||
} else if (self.headers[key] !== undefined) {
|
||||
self.headers[key] += ', ' + matches[2]
|
||||
else
|
||||
} else {
|
||||
self.headers[key] = matches[2]
|
||||
}
|
||||
self.rawHeaders.push(matches[1], matches[2])
|
||||
}
|
||||
})
|
||||
@@ -83,7 +136,15 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
|
||||
inherits(IncomingMessage, stream.Readable)
|
||||
|
||||
IncomingMessage.prototype._read = function () {}
|
||||
IncomingMessage.prototype._read = function () {
|
||||
var self = this
|
||||
|
||||
var resolve = self._resumeFetch
|
||||
if (resolve) {
|
||||
self._resumeFetch = null
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
|
||||
IncomingMessage.prototype._onXHRProgress = function () {
|
||||
var self = this
|
||||
@@ -92,29 +153,12 @@ IncomingMessage.prototype._onXHRProgress = function () {
|
||||
|
||||
var response = null
|
||||
switch (self._mode) {
|
||||
case 'text:vbarray': // For IE9
|
||||
if (xhr.readyState !== rStates.DONE)
|
||||
break
|
||||
try {
|
||||
// This fails in IE8
|
||||
response = new window.VBArray(xhr.responseBody).toArray()
|
||||
} catch (e) {}
|
||||
if (response !== null) {
|
||||
self.push(new Buffer(response))
|
||||
break
|
||||
}
|
||||
// Falls through in IE8
|
||||
case 'text':
|
||||
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
|
||||
}
|
||||
response = xhr.responseText
|
||||
if (response.length > self._pos) {
|
||||
var newData = response.substr(self._pos)
|
||||
if (self._charset === 'x-user-defined') {
|
||||
var buffer = new Buffer(newData.length)
|
||||
var buffer = Buffer.alloc(newData.length)
|
||||
for (var i = 0; i < newData.length; i++)
|
||||
buffer[i] = newData.charCodeAt(i) & 0xff
|
||||
|
||||
@@ -126,25 +170,25 @@ IncomingMessage.prototype._onXHRProgress = function () {
|
||||
}
|
||||
break
|
||||
case 'arraybuffer':
|
||||
if (xhr.readyState !== rStates.DONE)
|
||||
if (xhr.readyState !== rStates.DONE || !xhr.response)
|
||||
break
|
||||
response = xhr.response
|
||||
self.push(new Buffer(new Uint8Array(response)))
|
||||
self.push(Buffer.from(new Uint8Array(response)))
|
||||
break
|
||||
case 'moz-chunked-arraybuffer': // take whole
|
||||
response = xhr.response
|
||||
if (xhr.readyState !== rStates.LOADING || !response)
|
||||
break
|
||||
self.push(new Buffer(new Uint8Array(response)))
|
||||
self.push(Buffer.from(new Uint8Array(response)))
|
||||
break
|
||||
case 'ms-stream':
|
||||
response = xhr.response
|
||||
if (xhr.readyState !== rStates.LOADING)
|
||||
break
|
||||
var reader = new window.MSStreamReader()
|
||||
var reader = new global.MSStreamReader()
|
||||
reader.onprogress = function () {
|
||||
if (reader.result.byteLength > self._pos) {
|
||||
self.push(new Buffer(new Uint8Array(reader.result.slice(self._pos))))
|
||||
self.push(Buffer.from(new Uint8Array(reader.result.slice(self._pos))))
|
||||
self._pos = reader.result.byteLength
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+6981
File diff suppressed because it is too large
Load Diff
+25
-13
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "1.1.0",
|
||||
"version": "3.0.0",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
@@ -8,26 +8,38 @@
|
||||
"url": "git://github.com/jhiesey/stream-http.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm run test-node && npm run test-browser",
|
||||
"test": "npm run test-node && ([ -n \"${TRAVIS_PULL_REQUEST}\" -a \"${TRAVIS_PULL_REQUEST}\" != 'false' ] || npm run test-browser)",
|
||||
"test-node": "tape test/node/*.js",
|
||||
"test-browser": "zuul -- test/browser/*.js",
|
||||
"test-browser-local": "zuul --local 8080 --no-coverage -- test/browser/*.js"
|
||||
"test-browser": "airtap --loopback airtap.local -- test/browser/*.js",
|
||||
"test-browser-local": "airtap --no-instrument --local 8080 -- test/browser/*.js"
|
||||
},
|
||||
"author": "John Hiesey",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/jhiesey/stream-http/issues"
|
||||
},
|
||||
"homepage": "https://github.com/jhiesey/stream-http#readme",
|
||||
"keywords": [
|
||||
"http",
|
||||
"stream",
|
||||
"streaming",
|
||||
"xhr",
|
||||
"http-browserify"
|
||||
],
|
||||
"dependencies": {
|
||||
"builtin-status-codes": "~1.0.0",
|
||||
"foreach": "^2.0.5",
|
||||
"builtin-status-codes": "^3.0.0",
|
||||
"inherits": "^2.0.1",
|
||||
"object-keys": "1.0.4",
|
||||
"readable-stream": "^3.0.6",
|
||||
"xtend": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"basic-auth": "^1.0.3",
|
||||
"brfs": "^1.4.0",
|
||||
"express": "^4.13.0",
|
||||
"tape": "^4.0.0",
|
||||
"ua-parser-js": "^0.7.7",
|
||||
"zuul": "^3.1.0"
|
||||
"airtap": "^0.1.0",
|
||||
"basic-auth": "^2.0.1",
|
||||
"brfs": "^2.0.1",
|
||||
"cookie-parser": "^1.4.3",
|
||||
"express": "^4.16.3",
|
||||
"tape": "^4.9.0",
|
||||
"ua-parser-js": "^0.7.18",
|
||||
"webworkify": "^1.5.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
test('abort before response', function (t) {
|
||||
var req = http.get('/basic.txt', function (res) {
|
||||
t.fail('unexpected response')
|
||||
})
|
||||
req.abort()
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('abort on response', function (t) {
|
||||
var req = http.get('/basic.txt', function (res) {
|
||||
req.abort()
|
||||
t.end()
|
||||
|
||||
res.on('end', function () {
|
||||
t.fail('unexpected end')
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
t.fail('unexpected data')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('abort on data', function (t) {
|
||||
var req = http.get('/browserify.png?copies=5', function (res) {
|
||||
var firstData = true
|
||||
var failOnData = false
|
||||
|
||||
res.on('end', function () {
|
||||
t.fail('unexpected end')
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
if (failOnData)
|
||||
t.fail('unexpected data')
|
||||
else if (firstData) {
|
||||
firstData = false
|
||||
req.abort()
|
||||
t.end()
|
||||
process.nextTick(function () {
|
||||
// Wait for any data that may have been queued
|
||||
// in the stream before considering data events
|
||||
// as errors
|
||||
failOnData = true
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -5,18 +5,7 @@ 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)
|
||||
|
||||
// IE8 tends to throw up modal dialogs complaining about scripts running too long
|
||||
// Since streaming doesn't actually work there anyway, just use one copy
|
||||
var COPIES = skipVerification ? 1 : 20
|
||||
var COPIES = 20
|
||||
var MIN_PIECES = 2
|
||||
|
||||
var referenceOnce = fs.readFileSync(__dirname + '/../server/static/browserify.png')
|
||||
@@ -26,18 +15,14 @@ for(var i = 0; i < COPIES; i++) {
|
||||
}
|
||||
|
||||
test('binary streaming', function (t) {
|
||||
http.get('/browserify.png?copies=' + COPIES, function (res) {
|
||||
http.get({
|
||||
path: '/browserify.png?copies=' + COPIES,
|
||||
mode: 'allow-wrong-content-type'
|
||||
}, 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.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
|
||||
t.ok(buffers.length >= MIN_PIECES, 'received in multiple parts')
|
||||
t.end()
|
||||
})
|
||||
|
||||
@@ -50,14 +35,11 @@ test('binary streaming', function (t) {
|
||||
test('large binary request without streaming', function (t) {
|
||||
http.get({
|
||||
path: '/browserify.png?copies=' + COPIES,
|
||||
mode: 'prefer-fast',
|
||||
mode: 'default',
|
||||
}, 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')
|
||||
t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
|
||||
t.end()
|
||||
})
|
||||
|
||||
|
||||
+1
-10
@@ -5,12 +5,6 @@ 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 data gets corrupted in IE8 or below
|
||||
var skipVerification = (browserName === 'IE' && browserVersion <= 8)
|
||||
|
||||
var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png')
|
||||
|
||||
test('binary download', function (t) {
|
||||
@@ -18,10 +12,7 @@ test('binary download', function (t) {
|
||||
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')
|
||||
t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
|
||||
t.end()
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
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('get body empty', function (t) {
|
||||
var req = http.request({
|
||||
path: '/verifyEmpty',
|
||||
method: 'GET'
|
||||
}, function (res) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
console.log(Buffer.concat(buffers).toString('utf8'))
|
||||
t.ok(Buffer.from('empty').equals(Buffer.concat(buffers)), 'response body indicates request body was empty')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
|
||||
req.write(reference)
|
||||
req.end()
|
||||
})
|
||||
@@ -0,0 +1,25 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
test('cookie', function (t) {
|
||||
var cookie = 'hello=world'
|
||||
window.document.cookie = cookie
|
||||
|
||||
http.get({
|
||||
path: '/cookie',
|
||||
withCredentials: false
|
||||
}, function (res) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
t.ok(new Buffer(cookie).equals(Buffer.concat(buffers)), 'hello cookie echoed')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,37 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
test('disable fetch', function (t) {
|
||||
var originalFetch
|
||||
if (typeof fetch === 'function') {
|
||||
originalFetch = fetch
|
||||
}
|
||||
|
||||
var fetchCalled = false
|
||||
fetch = function (input, options) {
|
||||
fetchCalled = true
|
||||
if (originalFetch) {
|
||||
return originalFetch(input, options)
|
||||
}
|
||||
}
|
||||
|
||||
http.get({
|
||||
path: '/browserify.png',
|
||||
mode: 'disable-fetch'
|
||||
}, function (res) {
|
||||
t.ok(!fetchCalled, 'fetch was not called')
|
||||
|
||||
if (originalFetch) {
|
||||
fetch = originalFetch
|
||||
}
|
||||
|
||||
res.on('end', function () {
|
||||
t.ok(res.headers['content-type'] === 'image/png', 'content-type was set correctly')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function () {})
|
||||
})
|
||||
})
|
||||
+73
-13
@@ -1,19 +1,10 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var keys = require('object-keys')
|
||||
var test = require('tape')
|
||||
var UAParser = require('ua-parser-js')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
function indexOf (arr, item) {
|
||||
var len = arr.length
|
||||
for (var i = 0; i < len; i++) {
|
||||
if (arr[i] === item)
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
test('headers', function (t) {
|
||||
http.get({
|
||||
path: '/testHeaders?Response-Header=bar&Response-Header-2=BAR2',
|
||||
@@ -28,10 +19,10 @@ test('headers', function (t) {
|
||||
if (lowerKey.indexOf('test-') === 0)
|
||||
rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
|
||||
}
|
||||
var header1Pos = indexOf(rawHeaders, 'test-response-header')
|
||||
var header1Pos = rawHeaders.indexOf('test-response-header')
|
||||
t.ok(header1Pos >= 0, 'raw response header 1 present')
|
||||
t.equal(rawHeaders[header1Pos + 1], 'bar', 'raw response header value 1')
|
||||
var header2Pos = indexOf(rawHeaders, 'test-response-header-2')
|
||||
var header2Pos = rawHeaders.indexOf('test-response-header-2')
|
||||
t.ok(header2Pos >= 0, 'raw response header 2 present')
|
||||
t.equal(rawHeaders[header2Pos + 1], 'BAR2', 'raw response header value 2')
|
||||
t.equal(rawHeaders.length, 4, 'correct number of raw headers')
|
||||
@@ -45,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(keys(body).length, 2, 'correct number of request headers')
|
||||
t.equal(Object.keys(body).length, 2, 'correct number of request headers')
|
||||
t.end()
|
||||
})
|
||||
|
||||
@@ -53,4 +44,73 @@ test('headers', function (t) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('arrays of headers', function (t) {
|
||||
http.get({
|
||||
path: '/testHeaders?Response-Header=bar&Response-Header=BAR2',
|
||||
headers: {
|
||||
'Test-Request-Header': ['foo', 'FOO2']
|
||||
}
|
||||
}, function (res) {
|
||||
var rawHeaders = []
|
||||
for (var i = 0; i < res.rawHeaders.length; i += 2) {
|
||||
var lowerKey = res.rawHeaders[i].toLowerCase()
|
||||
if (lowerKey.indexOf('test-') === 0)
|
||||
rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
|
||||
}
|
||||
t.equal(rawHeaders[0], 'test-response-header', 'raw response header present')
|
||||
t.equal(rawHeaders[1], 'bar, BAR2', 'raw response header value')
|
||||
t.equal(rawHeaders.length, 2, 'correct number of raw headers')
|
||||
|
||||
t.equal(res.headers['test-response-header'], 'bar, BAR2', 'response header')
|
||||
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
var body = JSON.parse(Buffer.concat(buffers).toString())
|
||||
t.equal(body['test-request-header'], 'foo,FOO2', 'request headers')
|
||||
t.equal(Object.keys(body).length, 1, 'correct number of request headers')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('content-type response header', function (t) {
|
||||
http.get('/testHeaders', function (res) {
|
||||
t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
|
||||
var browserName = browser.name
|
||||
var browserVersion = browser.major
|
||||
var browserMinorVersion = browser.minor || 0
|
||||
// The content-type header is broken when 'prefer-streaming' or 'allow-wrong-content-type'
|
||||
// is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome, safari 6-10.0, and the stock Android browser
|
||||
// Note that Safari 10.0 on iOS 10.3 doesn't need to override the mime type, so the content-type is preserved.
|
||||
var wrongMimeType = ((browserName === 'Chrome' && browserVersion <= 42) ||
|
||||
((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6 && (browserVersion < 10 || (browserVersion == 10 && browserMinorVersion == 0)))
|
||||
|| (browserName === 'Android Browser'))
|
||||
|
||||
test('content-type response header with forced streaming', function (t) {
|
||||
http.get({
|
||||
path: '/testHeaders',
|
||||
mode: 'prefer-streaming'
|
||||
}, function (res) {
|
||||
if (wrongMimeType) {
|
||||
// allow both the 'wrong' and correct mime type, since sometimes it's impossible to tell which to expect
|
||||
// from the browser version alone (e.g. Safari 10.0 on iOS 10.2 vs iOS 10.3)
|
||||
var contentType = res.headers['content-type']
|
||||
var correct = (contentType === 'text/plain; charset=x-user-defined') || (contentType === 'application/json')
|
||||
t.ok(correct, 'content-type either preserved or overridden')
|
||||
} else
|
||||
t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,20 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
|
||||
var http = require('../../..')
|
||||
|
||||
module.exports = function (self) {
|
||||
self.addEventListener('message', function (ev) {
|
||||
var url = ev.data
|
||||
http.get(url, function (res) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
self.postMessage(Buffer.concat(buffers).buffer)
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -5,16 +5,6 @@ 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 === 'Safari' && browserVersion <= 5) ||
|
||||
(browserName === 'WebKit' && browserVersion <= 534) || // Old mobile safari
|
||||
(browserName === 'Android Browser' && browserVersion <= 4))
|
||||
|
||||
var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png')
|
||||
|
||||
test('post binary', function (t) {
|
||||
@@ -25,10 +15,7 @@ test('post binary', function (t) {
|
||||
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.ok(reference.equals(Buffer.concat(buffers)), 'echoed contents match')
|
||||
t.end()
|
||||
})
|
||||
|
||||
|
||||
@@ -25,4 +25,24 @@ test('post text', function (t) {
|
||||
|
||||
req.write(reference)
|
||||
req.end()
|
||||
})
|
||||
|
||||
test('post text with data in end()', 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.end(reference)
|
||||
})
|
||||
@@ -8,8 +8,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))
|
||||
// Streaming doesn't work in IE9 or below
|
||||
var skipStreamingCheck = (browserName === 'IE' && browserVersion <= 9)
|
||||
|
||||
var COPIES = 1000
|
||||
var MIN_PIECES = 5
|
||||
@@ -28,10 +28,7 @@ test('text streaming', function (t) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
if (skipStreamingCheck)
|
||||
t.skip('streaming not available on IE <= 8')
|
||||
else
|
||||
t.ok(buffers.length >= MIN_PIECES, 'received in multiple parts')
|
||||
t.ok(buffers.length >= MIN_PIECES, 'received in multiple parts')
|
||||
t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
|
||||
t.end()
|
||||
})
|
||||
|
||||
@@ -1,13 +1,33 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
var UAParser = require('ua-parser-js')
|
||||
var url = require('url')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
|
||||
var browserName = browser.name
|
||||
var browserVersion = browser.major
|
||||
// Response urls don't work on many browsers
|
||||
var skipResponseUrl = ((browserName === 'IE') ||
|
||||
(browserName === 'Edge') ||
|
||||
(browserName === 'Chrome' && browserVersion <= 36) ||
|
||||
(browserName === 'Firefox' && browserVersion <= 31) ||
|
||||
((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion <= 8) ||
|
||||
(browserName === 'Android Browser' && browserVersion <= 4))
|
||||
|
||||
var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt')
|
||||
|
||||
test('basic functionality', function (t) {
|
||||
http.get('/basic.txt', function (res) {
|
||||
if (!skipResponseUrl) {
|
||||
var testUrl = url.resolve(global.location.href, '/basic.txt')
|
||||
// Redirects aren't tested, but presumably only browser bugs
|
||||
// would cause this to fail only after redirects.
|
||||
t.equals(res.url, testUrl, 'response url correct')
|
||||
}
|
||||
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
test('timeout', function (t) {
|
||||
var req = http.get({
|
||||
path: '/browserify.png?copies=5',
|
||||
requestTimeout: 10 // ms
|
||||
}, function (res) {
|
||||
res.on('data', function (data) {
|
||||
})
|
||||
res.on('end', function () {
|
||||
t.fail('request completed (should have timed out)')
|
||||
})
|
||||
})
|
||||
req.on('requestTimeout', function () {
|
||||
t.pass('got timeout')
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
// TODO: reenable this if there's a way to make it simultaneously
|
||||
// fast and reliable
|
||||
test.skip('no timeout after success', function (t) {
|
||||
var req = http.get({
|
||||
path: '/basic.txt',
|
||||
requestTimeout: 50000 // ms
|
||||
}, function (res) {
|
||||
res.on('data', function (data) {
|
||||
})
|
||||
res.on('end', function () {
|
||||
t.pass('success')
|
||||
global.setTimeout(function () {
|
||||
t.end()
|
||||
}, 50000)
|
||||
})
|
||||
})
|
||||
req.on('requestTimeout', function () {
|
||||
t.fail('unexpected timeout')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
var UAParser = require('ua-parser-js')
|
||||
var url = require('url')
|
||||
var work = require('webworkify')
|
||||
|
||||
var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png')
|
||||
|
||||
// Temporarily disabled due to https://github.com/browserify/webworkify/issues/41
|
||||
test.skip('binary download in WebWorker', function (t) {
|
||||
// We have to use a global url, since webworkify puts the worker in a Blob,
|
||||
// which doesn't have a proper location
|
||||
var testUrl = url.resolve(global.location.href, '/browserify.png')
|
||||
var worker = work(require('./lib/webworker-worker.js'))
|
||||
worker.addEventListener('message', function (ev) {
|
||||
var data = new Buffer(new Uint8Array(ev.data))
|
||||
t.ok(reference.equals(data), 'contents match')
|
||||
t.end()
|
||||
})
|
||||
worker.postMessage(testUrl)
|
||||
})
|
||||
@@ -1,16 +1,13 @@
|
||||
// These tests are teken from http-browserify to ensure compatibility with
|
||||
// These tests are taken from http-browserify to ensure compatibility with
|
||||
// that module
|
||||
var test = require('tape')
|
||||
var url = require('url')
|
||||
|
||||
global.window = {}
|
||||
window.location = {
|
||||
hostname: 'localhost',
|
||||
port: 8081,
|
||||
protocol: 'http:'
|
||||
}
|
||||
var location = 'http://localhost:8081/foo/123'
|
||||
|
||||
var noop = function() {}
|
||||
window.XMLHttpRequest = function() {
|
||||
global.location = url.parse(location)
|
||||
global.XMLHttpRequest = function() {
|
||||
this.open = noop
|
||||
this.send = noop
|
||||
this.withCredentials = false
|
||||
@@ -20,17 +17,32 @@ var moduleName = require.resolve('../../')
|
||||
delete require.cache[moduleName]
|
||||
var http = require('../../')
|
||||
|
||||
test('Test simple url string', function(t) {
|
||||
var url = { path: '/api/foo' }
|
||||
var request = http.get(url, noop)
|
||||
test('Make sure http object has correct properties', function (t) {
|
||||
t.ok(http.Agent, 'Agent defined')
|
||||
t.ok(http.ClientRequest, 'ClientRequest defined')
|
||||
t.ok(http.ClientRequest.prototype, 'ClientRequest.prototype defined')
|
||||
t.ok(http.IncomingMessage, 'IncomingMessage defined')
|
||||
t.ok(http.IncomingMessage.prototype, 'IncomingMessage.prototype defined')
|
||||
t.ok(http.METHODS, 'METHODS defined')
|
||||
t.ok(http.STATUS_CODES, 'STATUS_CODES defined')
|
||||
t.ok(http.get, 'get defined')
|
||||
t.ok(http.globalAgent, 'globalAgent defined')
|
||||
t.ok(http.request, 'request defined')
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.equal( request._url, 'http://localhost:8081/api/foo', 'Url should be correct')
|
||||
test('Test simple url string', function(t) {
|
||||
var testUrl = { path: '/api/foo' }
|
||||
var request = http.get(testUrl, noop)
|
||||
|
||||
var resolved = url.resolve(location, request._opts.url)
|
||||
t.equal(resolved, 'http://localhost:8081/api/foo', 'Url should be correct')
|
||||
t.end()
|
||||
|
||||
})
|
||||
|
||||
test('Test full url object', function(t) {
|
||||
var url = {
|
||||
var testUrl = {
|
||||
host: "localhost:8081",
|
||||
hostname: "localhost",
|
||||
href: "http://localhost:8081/api/foo?bar=baz",
|
||||
@@ -44,11 +56,11 @@ test('Test full url object', function(t) {
|
||||
slashes: true
|
||||
}
|
||||
|
||||
var request = http.get(url, noop)
|
||||
var request = http.get(testUrl, noop)
|
||||
|
||||
t.equal( request._url, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct')
|
||||
var resolved = url.resolve(location, request._opts.url)
|
||||
t.equal(resolved, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct')
|
||||
t.end()
|
||||
|
||||
})
|
||||
|
||||
test('Test alt protocol', function(t) {
|
||||
@@ -61,37 +73,75 @@ test('Test alt protocol', function(t) {
|
||||
|
||||
var request = http.get(params, noop)
|
||||
|
||||
t.equal( request._url, 'foo://localhost:3000/bar', 'Url should be correct')
|
||||
var resolved = url.resolve(location, request._opts.url)
|
||||
t.equal(resolved, 'foo://localhost:3000/bar', 'Url should be correct')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Test page with \'file:\' protocol', function (t) {
|
||||
var params = {
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path: '/bar'
|
||||
}
|
||||
|
||||
var fileLocation = 'file:///home/me/stuff/index.html'
|
||||
|
||||
var normalLocation = global.location
|
||||
global.location = url.parse(fileLocation) // Temporarily change the location
|
||||
var request = http.get(params, noop)
|
||||
global.location = normalLocation // Reset the location
|
||||
|
||||
var resolved = url.resolve(fileLocation, request._opts.url)
|
||||
t.equal(resolved, 'http://localhost:3000/bar', 'Url should be correct')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Test string as parameters', function(t) {
|
||||
var url = '/api/foo'
|
||||
var request = http.get(url, noop)
|
||||
var testUrl = '/api/foo'
|
||||
var request = http.get(testUrl, noop)
|
||||
|
||||
t.equal( request._url, 'http://localhost:8081/api/foo', 'Url should be correct')
|
||||
var resolved = url.resolve(location, request._opts.url)
|
||||
t.equal(resolved, 'http://localhost:8081/api/foo', 'Url should be correct')
|
||||
t.end()
|
||||
|
||||
})
|
||||
|
||||
test('Test withCredentials param', function(t) {
|
||||
var url = '/api/foo'
|
||||
|
||||
var request = http.get({ url: url, withCredentials: false }, noop)
|
||||
t.equal( request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
|
||||
t.equal(request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
|
||||
|
||||
var request = http.get({ url: url, withCredentials: true }, noop)
|
||||
t.equal( request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
|
||||
t.equal(request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
|
||||
|
||||
var request = http.get({ url: url }, noop)
|
||||
t.equal( request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
|
||||
t.equal(request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Test ipv6 address', function(t) {
|
||||
var testUrl = 'http://[::1]:80/foo'
|
||||
var request = http.get(testUrl, noop)
|
||||
|
||||
var resolved = url.resolve(location, request._opts.url)
|
||||
t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Test relative path in url', function(t) {
|
||||
var params = { path: './bar' }
|
||||
var request = http.get(params, noop)
|
||||
|
||||
var resolved = url.resolve(location, request._opts.url)
|
||||
t.equal(resolved, 'http://localhost:8081/foo/bar', 'Url should be correct')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Cleanup', function (t) {
|
||||
delete global.window
|
||||
delete global.location
|
||||
delete global.XMLHttpRequest
|
||||
delete require.cache[moduleName]
|
||||
t.end()
|
||||
})
|
||||
|
||||
+32
-3
@@ -1,3 +1,4 @@
|
||||
var cookieParser = require('cookie-parser')
|
||||
var basicAuth = require('basic-auth')
|
||||
var express = require('express')
|
||||
var fs = require('fs')
|
||||
@@ -31,13 +32,17 @@ app.get('/testHeaders', function (req, res) {
|
||||
})
|
||||
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.setHeader('Cache-Control', 'no-cache')
|
||||
|
||||
// Request headers are sent in the body as json
|
||||
var reqHeaders = {}
|
||||
Object.keys(req.headers).forEach(function (key) {
|
||||
key = key.toLowerCase()
|
||||
if (key.indexOf('test-') === 0)
|
||||
reqHeaders[key] = req.headers[key]
|
||||
if (key.indexOf('test-') === 0) {
|
||||
// different browsers format request headers with multiple values
|
||||
// slightly differently, so normalize
|
||||
reqHeaders[key] = req.headers[key].replace(', ', ',')
|
||||
}
|
||||
})
|
||||
|
||||
var body = JSON.stringify(reqHeaders)
|
||||
@@ -46,6 +51,12 @@ app.get('/testHeaders', function (req, res) {
|
||||
res.end()
|
||||
})
|
||||
|
||||
app.get('/cookie', cookieParser(), function (req, res) {
|
||||
res.setHeader('Content-Type', 'text/plain')
|
||||
res.write('hello=' + req.cookies.hello)
|
||||
res.end()
|
||||
})
|
||||
|
||||
app.get('/auth', function (req, res) {
|
||||
var user = basicAuth(req)
|
||||
|
||||
@@ -64,6 +75,24 @@ app.post('/echo', function (req, res) {
|
||||
req.pipe(res)
|
||||
})
|
||||
|
||||
app.use('/verifyEmpty', function (req, res) {
|
||||
var empty = true
|
||||
req.on('data', function (buf) {
|
||||
if (buf.length > 0) {
|
||||
empty = false
|
||||
}
|
||||
})
|
||||
req.on('end', function () {
|
||||
res.setHeader('Content-Type', 'text/plain')
|
||||
|
||||
if (empty) {
|
||||
res.end('empty')
|
||||
} else {
|
||||
res.end('not empty')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
var parsed = url.parse(req.url, true)
|
||||
|
||||
@@ -103,6 +132,6 @@ app.use(function (req, res, next) {
|
||||
|
||||
app.use(express.static(path.join(__dirname, 'static')))
|
||||
|
||||
var port = parseInt(process.env.ZUUL_PORT) || 8199
|
||||
var port = parseInt(process.env.AIRTAP_PORT) || 8199
|
||||
console.log('Test server listening on port', port)
|
||||
server.listen(port)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// TODO: send a PR to url to remove .trim() so this isn't necessary
|
||||
if (!String.prototype.trim) {
|
||||
(function() {
|
||||
// Make sure we trim BOM and NBSP
|
||||
Reference in New Issue
Block a user