Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'check-content-type' of git://github.com/zever/retinajs …

…into zever-check-content-type
  • Loading branch information...
commit 40dc758c007543dbb0bb89c54bbba96178cec65b 2 parents 9eb0f97 + f2139b2
Casey O'Hara authored
View
17 src/retina.js
@@ -2,6 +2,9 @@
var root = (typeof exports == 'undefined' ? window : exports);
+ var default_options = {
+ check_mime_type: true
+ };
function RetinaImagePath(path) {
this.path = path;
@@ -12,6 +15,13 @@
RetinaImagePath.confirmed_paths = [];
+ // only for testing because this automatically starts when loaded.
+ // https://github.com/imulus/retinajs/pull/45#issuecomment-8037131
+ RetinaImagePath.set_options = function(options) {
+ if (options == null) options = {};
+ for (var prop in options) default_options[prop] = options[prop];
+ }
+
RetinaImagePath.prototype.is_external = function() {
return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) )
}
@@ -31,6 +41,13 @@
}
if (http.status >= 200 && http.status <= 399) {
+ if (default_options.check_mime_type) {
+ var type = http.getResponseHeader('Content-Type');
+ if (type == null || !type.match(/^image/i)) {
+ return callback(false);
+ }
+ }
+
RetinaImagePath.confirmed_paths.push(that.at_2x_path);
return callback(true);
} else {
View
8 test/fixtures/xml_http_request.js
@@ -1,10 +1,12 @@
function XMLHttpRequest() {
this.status = XMLHttpRequest.status;
+ this.contentType = XMLHttpRequest.contentType;
this.readyState = 4;
this.onreadystatechange = function() {}
}
XMLHttpRequest.status = 200;
+XMLHttpRequest.contentType = 'image/png';
XMLHttpRequest.prototype.open = function() {
return true;
@@ -13,6 +15,10 @@ XMLHttpRequest.prototype.open = function() {
XMLHttpRequest.prototype.send = function() {
this.onreadystatechange();
}
-
+
+XMLHttpRequest.prototype.getResponseHeader = function(contentType) {
+ return this.contentType;
+}
+
var root = (exports || window);
root.XMLHttpRequest = XMLHttpRequest;
View
25 test/retina_image_path.test.js
@@ -98,6 +98,7 @@ describe('RetinaImagePath', function() {
it('should callback with false when remote at2x image does not exist', function(done) {
XMLHttpRequest.status = 404; // simulate a failing request
+ XMLHttpRequest.contentType = 'image/png'; // simulate a proper content type
path = new RetinaImagePath("/images/some_image.png");
path.check_2x_variant(function(hasVariant) {
hasVariant.should.equal(false);
@@ -105,8 +106,31 @@ describe('RetinaImagePath', function() {
});
});
+ it('should callback with false when content-type is not an image type', function(done) {
+ XMLHttpRequest.status = 200; // simulate a an image request that comes back OK
+ XMLHttpRequest.contentType = 'text/html'; // but is actually an improperly coded 404 page
+ path = new RetinaImagePath("/images/some_image.png");
+ path.check_2x_variant(function(hasVariant) {
+ hasVariant.should.equal(false);
+ done();
+ });
+ });
+
+ it('should callback with true when content-type is wrong, but check_mime_type is false', function(done) {
+ XMLHttpRequest.status = 200; // simulate a proper request
+ XMLHttpRequest.contentType = 'text/html'; // but with an incorrect content type
+ global.RetinaImagePath.set_options({check_mime_type: false}); // but ignore it
+ path = new RetinaImagePath("/images/some_image.png");
+ path.check_2x_variant(function(hasVariant) {
+ hasVariant.should.equal(true);
+ global.RetinaImagePath.set_options({check_mime_type: true});
+ done();
+ });
+ });
+
it('should callback with true when remote at2x image exists', function(done) {
XMLHttpRequest.status = 200; // simulate a proper request
+ XMLHttpRequest.contentType = 'image/png'; // simulate a proper content type
path = new RetinaImagePath("/images/some_image.png");
path.check_2x_variant(function(hasVariant) {
hasVariant.should.equal(true);
@@ -116,6 +140,7 @@ describe('RetinaImagePath', function() {
it('should add path to cache when at2x image exists', function(done) {
XMLHttpRequest.status = 200; // simulate a proper request
+ XMLHttpRequest.contentType = 'image/png'; // simulate a proper content type
path = new RetinaImagePath("/images/some_image.png");
path.check_2x_variant(function(hasVariant) {
RetinaImagePath.confirmed_paths.should.include(path.at_2x_path);
Please sign in to comment.
Something went wrong with that request. Please try again.