This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
! httpx: decode should remove Content-Encoding header from message
This fixes #316 by changing Decoder.decode to remove the Content-Encoding header. I added extra round-trip unit tests to the DeflateSpec and GzipSpec but I also added some basic unit tests for the Encoder and Decoder abstract classes themselves.
- Loading branch information
Showing
6 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
spray-httpx/src/test/scala/spray/httpx/encoding/DecoderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2011-2013 spray.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package spray.httpx.encoding | ||
|
||
import spray.http._ | ||
import HttpHeaders._ | ||
import org.specs2.mutable.Specification | ||
|
||
class DecoderSpec extends Specification with CodecSpecSupport { | ||
|
||
"A Decoder" should { | ||
"not transform the message if it doesn't contain a Content-Encoding header" in { | ||
val request = HttpRequest(entity = HttpEntity(smallText)) | ||
DummyDecoder.decode(request) === request | ||
} | ||
"correctly transform the message if it contains a Content-Encoding header" in { | ||
val request = HttpRequest(entity = HttpEntity(smallText), headers = List(`Content-Encoding`(DummyDecoder.encoding))) | ||
val decoded = DummyDecoder.decode(request) | ||
decoded.headers === Nil | ||
decoded.entity === HttpEntity(dummyDecompress(smallText)) | ||
} | ||
} | ||
|
||
def dummyDecompress(s: String): String = new String(dummyDecompress(s.getBytes("UTF8")), "UTF8") | ||
def dummyDecompress(bytes: Array[Byte]): Array[Byte] = DummyDecompressor.decompress(bytes) | ||
|
||
case object DummyDecoder extends Decoder { | ||
val encoding = HttpEncodings.compress | ||
def newDecompressor = DummyDecompressor | ||
} | ||
|
||
case object DummyDecompressor extends Decompressor { | ||
protected def decompress(buffer: Array[Byte], offset: Int) = { | ||
output.write(buffer, 0, buffer.length) | ||
output.write("compressed".getBytes("UTF8")) | ||
-1 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
spray-httpx/src/test/scala/spray/httpx/encoding/EncoderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2011-2013 spray.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package spray.httpx.encoding | ||
|
||
import spray.http._ | ||
import HttpHeaders._ | ||
import org.specs2.mutable.Specification | ||
|
||
class EncoderSpec extends Specification with CodecSpecSupport { | ||
|
||
"An Encoder" should { | ||
"not transform the message if messageFilter returns false" in { | ||
val request = HttpRequest(entity = HttpEntity(smallText.getBytes("UTF8"))) | ||
DummyEncoder.encode(request) === request | ||
} | ||
"correctly transform the HttpMessage if messageFilter returns true" in { | ||
val request = HttpRequest(entity = HttpEntity(smallText)) | ||
val encoded = DummyEncoder.encode(request) | ||
encoded.headers === List(`Content-Encoding`(DummyEncoder.encoding)) | ||
encoded.entity === HttpEntity(dummyCompress(smallText)) | ||
} | ||
} | ||
|
||
def dummyCompress(s: String): String = new String(dummyCompress(s.getBytes("UTF8")), "UTF8") | ||
def dummyCompress(bytes: Array[Byte]): Array[Byte] = DummyCompressor.compress(bytes).finish() | ||
|
||
case object DummyEncoder extends Encoder { | ||
val messageFilter = Encoder.DefaultFilter | ||
val encoding = HttpEncodings.compress | ||
def newCompressor = DummyCompressor | ||
} | ||
|
||
case object DummyCompressor extends Compressor { | ||
def compress(buffer: Array[Byte]) = { | ||
if (buffer.length > 0) output.write(buffer, 0, buffer.length) | ||
output.write("compressed".getBytes("UTF8")) | ||
this | ||
} | ||
def flush() = getBytes | ||
def finish() = getBytes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters