Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow redirects #789

Merged
merged 8 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ function loadFromURL(options, cb) {
if (err) {
return cb(err);
}

if('headers' in response && 'location' in response.headers){
options.url = response.headers.location;
return loadFromURL(options, cb);
}

if (typeof data === 'object' && Buffer.isBuffer(data)) {
return cb(null, data);
Expand Down
Binary file added packages/core/test/images/pixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions packages/core/test/redirect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Jimp as jimp, getTestDir } from '@jimp/test-utils';

const fs = require('fs');
const http = require('http');

const imagesDir = getTestDir(__dirname) + '/images';

const httpHandler = (req, res) => {
switch (req.method) {
case 'GET':
switch (req.url) {
case '/redirect.png':
res.writeHead(301, {
Location: 'http://localhost:5136/corrected.png'
});
res.end();
break;
case '/corrected.png':
res.writeHead(200, { 'Content-Type': 'image/png' });
res.end(fs.readFileSync(imagesDir + '/pixel.png'), 'binary');
break;
default:
res.writeHead(404);
res.end('Not a valid test endpoint');
break;
}
break;
default:
res.writeHead(404);
res.end('Invalid request method');
break;
}
};

describe('redirect', function() {
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
xit('Not testing redirects in browser');
} else {
const httpServer = http.createServer(httpHandler);
before(function() {
httpServer.listen(5136);
});

it('follows 301 redirect', function(done) {
jimp
.read('http://localhost:5136/redirect.png')
.then(() => {
httpServer.close();
done();
})
.catch(error => {
httpServer.close();
done(error);
});
});
}
});