[Pkg-javascript-commits] [node-stream-http] 132/208: Expose response url
Bastien Roucariès
rouca at moszumanska.debian.org
Sun Aug 13 13:39:36 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-stream-http.
commit a0c6d49ba4e0123af8b2158aa5ed66a74d5ec861
Author: John Hiesey <john at hiesey.com>
Date: Wed Apr 20 01:33:14 2016 -0700
Expose response url
Fixes #39
---
README.md | 6 ++++++
lib/response.js | 2 ++
test/browser/text.js | 22 ++++++++++++++++++++++
3 files changed, 30 insertions(+)
diff --git a/README.md b/README.md
index c29b8c9..abe03ad 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,10 @@ node docs for how these work.
### 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.
@@ -86,6 +90,8 @@ certain headers.
* `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.
## Example
diff --git a/lib/response.js b/lib/response.js
index af2e4ca..89aeffe 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -31,6 +31,7 @@ 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>):
@@ -60,6 +61,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/)
diff --git a/test/browser/text.js b/test/browser/text.js
index 5076f84..6cc1c81 100644
--- a/test/browser/text.js
+++ b/test/browser/text.js
@@ -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 () {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-http.git
More information about the Pkg-javascript-commits
mailing list