Skip to content

Commit

Permalink
Cast string bodies to buffers to ensure correct encoding used (#1303)
Browse files Browse the repository at this point in the history
Cast string bodies to buffers to ensure correct encoding used
  • Loading branch information
jeskew committed Jan 17, 2017
1 parent be97613 commit 1afffc7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-S3-3a275bd9.json
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "S3",
"description": "Convert string bodies to buffers to ensure correct encoding is used"
}
14 changes: 14 additions & 0 deletions features/s3/objects.feature
Expand Up @@ -27,6 +27,20 @@ Feature: Working with Objects in S3
Then I get the object "contentlength"
And the object "contentlength" should contain "foo"

@multi-byte
Scenario: Putting multi-byte metadata
When I put a buffer containing "foo" to the key "mbmd" with the metadata key "mdkey" and value "voilà ça marche"
Then the object "mbmd" should exist
Then I get the object "mbmd"
And the object "mbmd" should contain "foo"

@multi-byte
Scenario: Putting multi-byte metadata
When I put "foo" to the key "mbmd" with the metadata key "mdkey" and value "voilà ça marche"
Then the object "mbmd" should exist
Then I get the object "mbmd"
And the object "mbmd" should contain "foo"

@multi-byte
Scenario: Putting a multi-byte string to an object
When I put "åß∂ƒ©" to the key "multi"
Expand Down
16 changes: 16 additions & 0 deletions features/s3/step_definitions/objects.js
Expand Up @@ -29,6 +29,22 @@ module.exports = function () {
this.request('s3nochecksums', 'putObject', params, next);
});

this.When(/^I put "([^"]*)" to the key "([^"]*)" with the metadata key "([^"]*)" and value "([^"]*)"$/, function(contents, key, mdKey, mdValue, next) {
var metadata = {};
metadata[mdKey] = mdValue;
var params = {Bucket: this.sharedBucket, Key: key, Body: contents, Metadata: metadata };
this.s3nochecksums = new this.AWS.S3({computeChecksums: false});
this.request('s3nochecksums', 'putObject', params, next);
});

this.When(/^I put a buffer containing "([^"]*)" to the key "([^"]*)" with the metadata key "([^"]*)" and value "([^"]*)"$/, function(contents, key, mdKey, mdValue, next) {
var metadata = {};
metadata[mdKey] = mdValue;
var params = {Bucket: this.sharedBucket, Key: key, Body: new Buffer(contents), Metadata: metadata };
this.s3nochecksums = new this.AWS.S3({computeChecksums: false});
this.request('s3nochecksums', 'putObject', params, next);
});

this.Then(/^the object "([^"]*)" should contain "([^"]*)"$/, function(key, contents, next) {
this.assert.equal(this.data.Body.toString().replace('\n', ''), contents);
next();
Expand Down
4 changes: 4 additions & 0 deletions lib/http/node.js
Expand Up @@ -100,6 +100,10 @@ AWS.NodeHttpClient = AWS.util.inherit({
total: totalBytes
});
});
if (typeof body === 'string') {
body = typeof Buffer.from === 'function' ?
Buffer.from(body) : new Buffer(body);
}
stream.end(body);
} else {
// no request body
Expand Down

0 comments on commit 1afffc7

Please sign in to comment.