Skip to content

Commit

Permalink
Merge pull request #461 from kppullin/issue-371-try-2
Browse files Browse the repository at this point in the history
Strip the UTF8 BOM from a UTF encoded response
  • Loading branch information
mikeal committed Mar 5, 2013
2 parents cd7d571 + fa1ef30 commit 97754ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -764,6 +764,11 @@ Request.prototype.start = function () {
response.body = body.toString(self.encoding)
}
} else if (buffer.length) {
// The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation.
// Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().
if (self.encoding === 'utf8' && buffer[0].length > 0 && buffer[0][0] === "\uFEFF") {
buffer[0] = buffer[0].substring(1)
}
response.body = buffer.join('')
}

Expand Down
5 changes: 5 additions & 0 deletions tests/test-body.js
Expand Up @@ -35,6 +35,11 @@ var tests =
, encoding: 'hex'
, expectBody: "efa3bfcea9e29883"
}
, testGetUTF8:
{ resp: server.createGetResponse(new Buffer([0xEF, 0xBB, 0xBF, 226, 152, 131]))
, encoding: "utf8"
, expectBody: "☃"
}
, testGetJSON :
{ resp : server.createGetResponse('{"test":true}', 'application/json')
, json : true
Expand Down

0 comments on commit 97754ad

Please sign in to comment.