Skip to content

Commit

Permalink
Accept Content-Encoding values case-insensitive (#354)
Browse files Browse the repository at this point in the history
According to the spec the value of the content-encoding header field
is case insensitive, therefor changed `readResponse()` to utilize
`equalsIgnoreCase()` when checking the content encoding.

See https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5
  • Loading branch information
grob authored and botic committed Oct 3, 2016
1 parent 3d377a3 commit 0ffad94
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/ringo/httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ var readResponse = function(connection) {
}
var encoding = connection.getContentEncoding();
if (encoding != null) {
if (encoding === "gzip") {
if (encoding.equalsIgnoreCase("gzip")) {
inStream = new GZIPInputStream(inStream);
} else if (encoding === "deflate") {
} else if (encoding.equalsIgnoreCase("deflate")) {
inStream = new InflaterInputStream(inStream);
}
}
Expand Down
54 changes: 54 additions & 0 deletions test/ringo/httpclient_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var {MemoryStream, TextStream} = require("io");
var fs = require("fs");
var base64 = require("ringo/base64");
var {ByteArray, ByteString} = require("binary");
var {ByteArrayOutputStream} = java.io;
var {GZIPOutputStream, DeflaterOutputStream} = java.util.zip;

var server;
var host = "127.0.0.1";
Expand Down Expand Up @@ -332,6 +334,58 @@ exports.testStreamRequest = function() {
assert.strictEqual(exchange.contentType, "image/png");
};

exports.testContentDecoding = function() {
var unzipped = "abcdefghijklmnop";

var compress = function(CompressorOutputStream) {
var bos = new ByteArrayOutputStream();
var cos = new CompressorOutputStream(bos, true);
cos.write(unzipped.toByteArray());
cos.finish();
var bytes = ByteArray.wrap(bos.toByteArray());
cos.close();
return bytes;
};

var compressions = [
{
encodings: ['deflate', 'dEfLaTe'],
CompressorOutputStream: DeflaterOutputStream
},
{
encodings: ['gzip', 'gZiP'],
CompressorOutputStream: GZIPOutputStream
}
];

for each (let {encodings, CompressorOutputStream} in compressions) {
let compressed = compress(CompressorOutputStream);
for each (let encoding in encodings) {
getResponse = function(req) {
return {
status: 200,
headers: {
'Content-Type': 'image/png',
'Content-Encoding': encoding
},
body: {
forEach: function(fn) {
return fn(compressed);
}
}
};
};

let exchange = request({
url: baseUri,
method: 'GET'
});
assert.isNotNull(exchange, 'Content-Encoding: ' + encoding);
assert.strictEqual(exchange.content, unzipped, 'Content-Encoding: ' + encoding);
}
}
};

exports.testPost = function() {

getResponse = function(req) {
Expand Down

0 comments on commit 0ffad94

Please sign in to comment.