Fix issues with the UrlResolver and add more tests.

This commit is contained in:
André Cruz
2013-05-01 14:43:55 +01:00
parent 4919b9cb48
commit 6b7df8fd50
3 changed files with 238 additions and 29 deletions

View File

@@ -7,6 +7,7 @@ var mout = require('mout');
var Resolver = require('../Resolver');
var extract = require('../../util/extract');
var createError = require('../../util/createError');
var osJunk = require('../../util/osJunk');
var UrlResolver = function (source, options) {
var pos;
@@ -32,19 +33,39 @@ util.inherits(UrlResolver, Resolver);
// -----------------
UrlResolver.prototype._hasNew = function (pkgMeta) {
var oldHeaders = pkgMeta._cacheHeaders || {};
var oldCacheHeaders = pkgMeta._cacheHeaders || {},
reqHeaders = {};
// TODO: switch from HEAD request to normal + abort
// If the previous cache headers contain an ETag,
// send the "If-None-Match" header with it
if (oldCacheHeaders.ETag) {
reqHeaders['If-None-Match'] = oldCacheHeaders.ETag;
}
// Make a HEAD request to the source
// Make an HEAD request to the source
return Q.nfcall(request.head, this._source, {
proxy: this._config.proxy,
timeout: 5000
})
// Compare new headers with the old ones
.then(function (response) {
var headers = this._collectCacheHeaders(response);
return mout.object.equals(oldHeaders, headers);
.spread(function (response) {
var cacheHeaders;
// If the server responded with 303 then the resource
// still has the same ETag
if (response.statusCode === 304) {
return false;
}
// If status code is not in the 2xx range,
// then just resolve to true
if (response.statusCode < 200 || response.statusCode >= 300) {
return true;
}
// Fallback to comparing cache headers
cacheHeaders = this._collectCacheHeaders(response);
return !mout.object.equals(oldCacheHeaders, cacheHeaders);
}.bind(this), function () {
// Assume new contents if the request failed
return true;
@@ -58,8 +79,8 @@ UrlResolver.prototype._resolve = function () {
}
return this._download()
.then(this._parseHeaders.bind(this))
.then(this._extract.bind(this))
.spread(this._parseHeaders.bind(this))
.spread(this._extract.bind(this))
.then(this._rename.bind(this));
};
@@ -71,7 +92,8 @@ UrlResolver.prototype._download = function () {
req,
res,
writer,
finish;
finish,
that = this;
finish = function (err) {
// Ensure that all listeners are removed
@@ -82,7 +104,7 @@ UrlResolver.prototype._download = function () {
if (err) {
return deferred.reject(err);
} else {
this._response = res;
that._response = res;
return deferred.resolve([file, res]);
}
};
@@ -114,7 +136,7 @@ UrlResolver.prototype._parseHeaders = function (file, response) {
matches;
// Check if we got a Content-Disposition header
disposition = response.get('Content-Disposition');
disposition = response.headers['content-disposition'];
if (!disposition) {
return Q.resolve([file, response]);
}
@@ -135,7 +157,7 @@ UrlResolver.prototype._parseHeaders = function (file, response) {
};
UrlResolver.prototype._extract = function (file, response) {
var mimeType = response.getHeader('Content-Type');
var mimeType = response.headers['content-type'];
if (!extract.canExtract(mimeType || file)) {
return Q.resolve();
@@ -153,6 +175,10 @@ UrlResolver.prototype._rename = function () {
oldPath,
newPath;
// Remove any OS specific files from the files array
// before checking its length
files = files.filter(osJunk.isNotOsJunk);
if (files.length === 1) {
file = files[0];
this._singleFile = 'index' + path.extname(file);
@@ -181,7 +207,7 @@ UrlResolver.prototype._collectCacheHeaders = function (res) {
// Collect cache headers
this.constructor._cacheHeaders.forEach(function (name) {
var value = res.getHeader(name);
var value = res.headers[name.toLowerCase()];
if (value != null) {
headers[name] = value;