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

Custom headers #182

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 18 additions & 1 deletion source/image-handler/image-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class ImageRequest {
this.bucket = this.parseImageBucket(event, this.requestType);
this.key = this.parseImageKey(event, this.requestType);
this.edits = this.parseImageEdits(event, this.requestType);
this.originalImage = await this.getOriginalImage(this.bucket, this.key)
this.originalImage = await this.getOriginalImage(this.bucket, this.key);
this.headers = this.parseImageHeaders(event, this.requestType);
return Promise.resolve(this);
} catch (err) {
return Promise.reject(err);
Expand Down Expand Up @@ -124,6 +125,22 @@ class ImageRequest {
}
}

/**
* Parses the headers to be sent with the response
* @param {String} event - Lambda request body.
* @param {String} requestType - Image handler request type.
*/
parseImageHeaders(event, requestType) {
if (requestType === "Default") {
const decoded = this.decodeRequest(event);
if (decoded.headers !== undefined) {
return decoded.headers;
}
}

return [];
}

/**
* Parses the name of the appropriate Amazon S3 key corresponding to the
* original image.
Expand Down
14 changes: 12 additions & 2 deletions source/image-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ exports.handler = async (event) => {
const request = await imageRequest.setup(event);
console.log(request);
const processedRequest = await imageHandler.process(request);
const headers = getResponseHeaders();

if (request.headers) {
// Apply the custom headers overwriting any that may need overwriting
for (let i = 0; i < request.headers.length; i++) {
headers[request.headers[i].key] = request.headers[i].value;
}
}

const response = {
"statusCode": 200,
"headers" : getResponseHeaders(),
"headers" : headers,
"body": processedRequest,
"isBase64Encoded": true
}
Expand Down Expand Up @@ -53,7 +62,8 @@ const getResponseHeaders = (isErr) => {
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Credentials": true,
"Content-Type": "image"
}
};

if (corsEnabled) {
headers["Access-Control-Allow-Origin"] = process.env.CORS_ORIGIN;
}
Expand Down
29 changes: 28 additions & 1 deletion source/image-handler/test/test-image-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('setup()', function() {
bucket: 'validBucket',
key: 'validKey',
edits: { grayscale: true },
headers: [],
originalImage: Buffer.from('SampleImageContent\n')
}
// Assert
Expand Down Expand Up @@ -78,6 +79,7 @@ describe('setup()', function() {
bucket: 'allowedBucket001',
key: 'test-image-001.jpg',
edits: { grayscale: true },
headers: [],
originalImage: Buffer.from('SampleImageContent\n')
}
// Assert
Expand Down Expand Up @@ -116,6 +118,7 @@ describe('setup()', function() {
grayscale: true,
rotate: 90
},
headers: [],
originalImage: Buffer.from('SampleImageContent\n')
}
// Assert
Expand Down Expand Up @@ -644,4 +647,28 @@ describe('getAllowedSourceBuckets()', function() {
});
});
});
})
})

// ----------------------------------------------------------------------------
// parseImageHeaders()
// ----------------------------------------------------------------------------
describe('parseImageHeaders()', function() {
describe('001/defaultCustomHeaders', function () {
it(`Should pass if the proper result is returned for a sample base64-
encoded image request`, function () {
// Arrange
const event = {
path: '/eyJoZWFkZXJzIjpbeyJrZXkiOiAiQ2FjaGUtQ29udHJvbCIsICJ2YWx1ZSI6ICJtYXgtYWdlPTMxNTM2MDAwLHB1YmxpYyJ9XX0='
}
// Act
const imageRequest = new ImageRequest();
const result = imageRequest.parseImageHeaders(event, 'Default');
// Assert
const expectedResult = [{
key: "Cache-Control",
value: 'max-age=31536000,public'
}]
assert.deepEqual(result, expectedResult);
});
});
});