Compare commits
64 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
+1
-1
@@ -1,3 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.12"
|
||||
- "6.4"
|
||||
@@ -6,15 +6,17 @@ browsers:
|
||||
version: 34..latest
|
||||
- name: safari
|
||||
version: 5..latest
|
||||
- name: microsoftedge
|
||||
version: 13..latest
|
||||
- name: ie
|
||||
version: 9..latest
|
||||
- name: opera
|
||||
version: 11..latest
|
||||
- name: iphone
|
||||
version: 5.1..latest
|
||||
version: '8.1..latest'
|
||||
- name: android
|
||||
version: 4.0..latest
|
||||
version: '4.0..latest'
|
||||
server: ./test/server/index.js
|
||||
scripts:
|
||||
- "/ie8-polyfill.js"
|
||||
- "/test-polyfill.js"
|
||||
- "/test-polyfill.js"
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
[](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?
|
||||
|
||||
@@ -15,7 +15,7 @@ the request has completed whenever possible.
|
||||
|
||||
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
|
||||
@@ -38,54 +38,64 @@ All browsers with full ES5 support shouldn't require any polyfills.
|
||||
|
||||
## 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 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
|
||||
* '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.
|
||||
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
|
||||
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
|
||||
### 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` options in the `request` method is non-operational in Safari <= 5 and
|
||||
Opera <= 12.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -93,17 +103,32 @@ 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 [`.zuulrc` file](https://github.com/defunctzombie/zuul/wiki/zuulrc).
|
||||
|
||||
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
|
||||
`http://localhost:8080/__zuul`
|
||||
|
||||
## License
|
||||
|
||||
MIT. Copyright (C) John Hiesey and other contributors.
|
||||
|
||||
@@ -11,7 +11,12 @@ http.request = function (opts, cb) {
|
||||
else
|
||||
opts = extend(opts)
|
||||
|
||||
var protocol = opts.protocol || ''
|
||||
// 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:' : ''
|
||||
|
||||
var protocol = opts.protocol || defaultProtocol
|
||||
var host = opts.hostname || opts.host
|
||||
var port = opts.port
|
||||
var path = opts.path || '/'
|
||||
|
||||
+17
-7
@@ -1,4 +1,4 @@
|
||||
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableByteStream)
|
||||
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream)
|
||||
|
||||
exports.blobConstructor = false
|
||||
try {
|
||||
@@ -6,12 +6,22 @@ try {
|
||||
exports.blobConstructor = true
|
||||
} catch (e) {}
|
||||
|
||||
var xhr = new global.XMLHttpRequest()
|
||||
// If location.host is empty, e.g. if this page/worker was loaded
|
||||
// from a Blob, then use example.com to avoid an error
|
||||
xhr.open('GET', global.location.host ? '/' : 'https://example.com')
|
||||
// Service workers don't have XHR
|
||||
var xhr = null
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
function checkTypeSupport (type) {
|
||||
if (!xhr) return false
|
||||
try {
|
||||
xhr.responseType = type
|
||||
return xhr.responseType === type
|
||||
@@ -30,11 +40,11 @@ exports.arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer')
|
||||
exports.msstream = !exports.fetch && haveSlice && checkTypeSupport('ms-stream')
|
||||
exports.mozchunkedarraybuffer = !exports.fetch && haveArrayBuffer &&
|
||||
checkTypeSupport('moz-chunked-arraybuffer')
|
||||
exports.overrideMimeType = isFunction(xhr.overrideMimeType)
|
||||
exports.overrideMimeType = xhr ? isFunction(xhr.overrideMimeType) : false
|
||||
exports.vbArray = isFunction(global.VBArray)
|
||||
|
||||
function isFunction (value) {
|
||||
return typeof value === 'function'
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
xhr = null // Help gc
|
||||
|
||||
+25
-8
@@ -1,14 +1,14 @@
|
||||
// var Base64 = require('Base64')
|
||||
var capability = require('./capability')
|
||||
var inherits = require('inherits')
|
||||
var response = require('./response')
|
||||
var stream = require('stream')
|
||||
var stream = require('readable-stream')
|
||||
var toArrayBuffer = require('to-arraybuffer')
|
||||
|
||||
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'
|
||||
@@ -37,7 +37,13 @@ var ClientRequest = module.exports = function (opts) {
|
||||
})
|
||||
|
||||
var preferBinary
|
||||
if (opts.mode === 'prefer-streaming') {
|
||||
var useFetch = true
|
||||
if (opts.mode === 'disable-fetch' || 'timeout' in opts) {
|
||||
// If the use of XHR should be preferred and includes preserving the 'content-type' header.
|
||||
// Force XHR to be used since the Fetch API does not yet support timeouts.
|
||||
useFetch = false
|
||||
preferBinary = true
|
||||
} 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
|
||||
@@ -50,7 +56,7 @@ var ClientRequest = module.exports = function (opts) {
|
||||
} else {
|
||||
throw new Error('Invalid value for opts.mode')
|
||||
}
|
||||
self._mode = decideMode(preferBinary)
|
||||
self._mode = decideMode(preferBinary, useFetch)
|
||||
|
||||
self.on('finish', function () {
|
||||
self._onFinish()
|
||||
@@ -93,10 +99,10 @@ ClientRequest.prototype._onFinish = function () {
|
||||
|
||||
var headersObj = self._headers
|
||||
var body
|
||||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
|
||||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH' || opts.method === 'MERGE') {
|
||||
if (capability.blobConstructor) {
|
||||
body = new global.Blob(self._body.map(function (buffer) {
|
||||
return buffer.toArrayBuffer()
|
||||
return toArrayBuffer(buffer)
|
||||
}), {
|
||||
type: (headersObj['content-type'] || {}).value || ''
|
||||
})
|
||||
@@ -144,6 +150,13 @@ ClientRequest.prototype._onFinish = function () {
|
||||
if (self._mode === 'text' && 'overrideMimeType' in xhr)
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined')
|
||||
|
||||
if ('timeout' in opts) {
|
||||
xhr.timeout = opts.timeout
|
||||
xhr.ontimeout = function () {
|
||||
self.emit('timeout')
|
||||
}
|
||||
}
|
||||
|
||||
Object.keys(headersObj).forEach(function (name) {
|
||||
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
|
||||
})
|
||||
@@ -215,6 +228,10 @@ ClientRequest.prototype._connect = function () {
|
||||
return
|
||||
|
||||
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
|
||||
self._response.on('error', function(err) {
|
||||
self.emit('error', err)
|
||||
})
|
||||
|
||||
self.emit('response', self._response)
|
||||
}
|
||||
|
||||
|
||||
+20
-10
@@ -1,6 +1,6 @@
|
||||
var capability = require('./capability')
|
||||
var inherits = require('inherits')
|
||||
var stream = require('stream')
|
||||
var stream = require('readable-stream')
|
||||
|
||||
var rStates = exports.readyStates = {
|
||||
UNSENT: 0,
|
||||
@@ -31,14 +31,15 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
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
|
||||
var reader = response.body.getReader()
|
||||
@@ -52,6 +53,8 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
}
|
||||
self.push(new Buffer(result.value))
|
||||
read()
|
||||
}).catch(function(err) {
|
||||
self.emit('error', err)
|
||||
})
|
||||
}
|
||||
read()
|
||||
@@ -60,6 +63,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
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/)
|
||||
@@ -67,10 +71,16 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
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])
|
||||
}
|
||||
})
|
||||
@@ -135,7 +145,7 @@ 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)))
|
||||
|
||||
+10
-4
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "2.0.3",
|
||||
"version": "2.6.0",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
@@ -8,13 +8,17 @@
|
||||
"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 --no-coverage -- test/browser/*.js",
|
||||
"test-browser-local": "zuul --local 8080 --no-coverage -- 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",
|
||||
@@ -23,8 +27,10 @@
|
||||
"http-browserify"
|
||||
],
|
||||
"dependencies": {
|
||||
"builtin-status-codes": "^1.0.0",
|
||||
"builtin-status-codes": "^3.0.0",
|
||||
"inherits": "^2.0.1",
|
||||
"readable-stream": "^2.1.0",
|
||||
"to-arraybuffer": "^1.0.0",
|
||||
"xtend": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -35,6 +41,6 @@
|
||||
"tape": "^4.0.0",
|
||||
"ua-parser-js": "^0.7.7",
|
||||
"webworkify": "^1.0.2",
|
||||
"zuul": "^3.1.0"
|
||||
"zuul": "^3.10.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 () {})
|
||||
})
|
||||
})
|
||||
@@ -9,7 +9,7 @@ module.exports = function (self) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
self.postMessage(Buffer.concat(buffers).toArrayBuffer())
|
||||
self.postMessage(Buffer.concat(buffers).buffer)
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
|
||||
@@ -1,13 +1,35 @@
|
||||
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 === 'Opera') ||
|
||||
(browserName === 'IE') ||
|
||||
(browserName === 'Edge') ||
|
||||
(browserName === 'Chrome' && browserVersion <= 36) ||
|
||||
(browserName === 'Firefox' && browserVersion <= 31) ||
|
||||
((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion <= 8) ||
|
||||
(browserName === 'WebKit') || // Old mobile safari
|
||||
(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,24 @@
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
var skipTimeout = ((browserName === 'Opera' && browserVersion <= 12) ||
|
||||
(browserName === 'Safari' && browserVersion <= 5))
|
||||
|
||||
|
||||
test('emits timeout events', function (t) {
|
||||
if (skipTimeout) {
|
||||
return t.skip('Browser does not support setting timeouts')
|
||||
}
|
||||
|
||||
var req = http.request({
|
||||
path: '/basic.txt',
|
||||
timeout: 1
|
||||
})
|
||||
|
||||
req.on('timeout', function () {
|
||||
t.end() // the test will timeout if this does not happen
|
||||
})
|
||||
|
||||
req.end()
|
||||
})
|
||||
@@ -64,6 +64,25 @@ test('Test alt protocol', function(t) {
|
||||
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 testUrl = '/api/foo'
|
||||
var request = http.get(testUrl, noop)
|
||||
|
||||
Reference in New Issue
Block a user