Skip to content

Commit

Permalink
Add test on gzipping
Browse files Browse the repository at this point in the history
  • Loading branch information
laurilehmijoki committed Aug 2, 2016
1 parent 54f8670 commit a5c890d
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/test/scala/s3/website/S3WebsiteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,30 @@ class S3WebsiteSpec extends Specification {
sentPutObjectRequest.getKey must equalTo("styles.css")
}

"not gzip the file if it's already gzipped" in new BasicSetup {
def using[T <: Closeable, R](cl: T)(f: (T) => R): R = try f(cl) finally cl.close()
"gzips a file" in new BasicSetup {
val htmlString = "<h1>hi again</h1>"
val gzippedBytes = gzip(htmlString.getBytes(StandardCharsets.UTF_8))
config = "gzip: true"
setLocalFileWithContent("index.html", gzippedBytes)
setS3File("styles.css", "1c5117e5839ad8fc00ce3c41296255a1" /* md5 of the gzip of the file contents */)
val putObjectRequestCaptor = ArgumentCaptor.forClass(classOf[PutObjectRequest])
push()
sentPutObjectRequest.getKey must equalTo("index.html")
verify(amazonS3Client).putObject(putObjectRequestCaptor.capture())

val gzippedCss: ByteArrayOutputStream = new ByteArrayOutputStream
val cssString = "body { color: black }"
using(new GZIPOutputStream(gzippedCss)) { stream =>
IOUtils.copy(new ByteArrayInputStream(cssString.getBytes(StandardCharsets.UTF_8)), stream)
}
val bytesToS3: InputStream = putObjectRequestCaptor.getValue.getInputStream
val unzippedBytesToS3 = new GZIPInputStream(bytesToS3)
val unzippedString = IOUtils.toString(unzippedBytesToS3, StandardCharsets.UTF_8)

unzippedString must equalTo(htmlString)
}

"not gzip the file if it's already gzipped" in new BasicSetup {
config = "gzip: true"

setLocalFileWithContent("styles.css", gzippedCss.toByteArray)
val cssString = "body { color: red }"
val gzippedCss = gzip(cssString.getBytes(StandardCharsets.UTF_8))
setLocalFileWithContent("styles.css", gzippedCss)
val putObjectRequestCaptor = ArgumentCaptor.forClass(classOf[PutObjectRequest])
push()
sentPutObjectRequest.getKey must equalTo("styles.css")
Expand All @@ -68,6 +80,16 @@ class S3WebsiteSpec extends Specification {
unzippedString must equalTo(cssString)
}

def gzip(data: Array[Byte]): Array[Byte] = {
def using[T <: Closeable, R](cl: T)(f: (T) => R): R = try f(cl) finally cl.close()

val gzippedOutputStream: ByteArrayOutputStream = new ByteArrayOutputStream
using(new GZIPOutputStream(gzippedOutputStream)) { stream =>
IOUtils.copy(new ByteArrayInputStream(data), stream)
}
gzippedOutputStream.toByteArray
}

"not update a gzipped S3 object if the contents has not changed" in new BasicSetup {
config = "gzip: true"
setLocalFileWithContent(("styles.css", "<h1>hi</h1>"))
Expand Down

0 comments on commit a5c890d

Please sign in to comment.