-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Content-Type support
- Loading branch information
Filippo De Luca
committed
Oct 7, 2016
1 parent
afd047b
commit 256dadb
Showing
6 changed files
with
206 additions
and
24 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
akka-http-aws/src/main/scala/com/bluelabs/akkaaws/AwsHeaders.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,58 @@ | ||
package com.bluelabs.akkaaws | ||
|
||
import akka.http.scaladsl.model.headers.{ModeledCustomHeaderCompanion, ModeledCustomHeader} | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
|
||
object AwsHeaders { | ||
|
||
sealed abstract class ServerSideEncryptionAlgorithm(val name: String) | ||
object ServerSideEncryptionAlgorithm { | ||
case object AES256 extends ServerSideEncryptionAlgorithm("AES256") | ||
case object KMS extends ServerSideEncryptionAlgorithm("aws:kms") | ||
|
||
def fromString(raw: String): Try[ServerSideEncryptionAlgorithm] = raw match { | ||
case "AES256" => Success(AES256) | ||
case "aws:kms" => Success(KMS) | ||
case invalid => Failure(new IllegalArgumentException(s"$invalid is not a valid server side encryption algorithm.")) | ||
} | ||
} | ||
|
||
object `X-Amz-Server-Side-Encryption` extends ModeledCustomHeaderCompanion[`X-Amz-Server-Side-Encryption`] { | ||
override def name: String = "X-Amz-Server-Side-Encryption" | ||
override def parse(value: String): Try[`X-Amz-Server-Side-Encryption`] = | ||
ServerSideEncryptionAlgorithm.fromString(value).map(new `X-Amz-Server-Side-Encryption`(_)) | ||
} | ||
|
||
final case class `X-Amz-Server-Side-Encryption`(algorithm: ServerSideEncryptionAlgorithm) extends ModeledCustomHeader[`X-Amz-Server-Side-Encryption`] { | ||
|
||
override def companion: ModeledCustomHeaderCompanion[`X-Amz-Server-Side-Encryption`] = `X-Amz-Server-Side-Encryption` | ||
|
||
override def value(): String = algorithm.name | ||
|
||
override def renderInResponses(): Boolean = true | ||
|
||
override def renderInRequests(): Boolean = true | ||
} | ||
|
||
object `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id` extends ModeledCustomHeaderCompanion[`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`] { | ||
override def name: String = "X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id" | ||
override def parse(value: String): Try[`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`] = | ||
Success(new `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`(value)) | ||
} | ||
|
||
final case class `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`(id: String) extends ModeledCustomHeader[`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`] { | ||
|
||
override def companion: ModeledCustomHeaderCompanion[`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`] = `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id` | ||
|
||
override def value(): String = id | ||
|
||
override def renderInResponses(): Boolean = true | ||
|
||
override def renderInRequests(): Boolean = true | ||
} | ||
|
||
// TODO add `x-amz-server-side-encryption-context` header. | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
akka-http-aws/src/test/scala/com/bluelabs/akkaaws/AwsHeadersSpec.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,47 @@ | ||
package com.bluelabs.akkaaws | ||
|
||
import akka.http.scaladsl.model.headers.RawHeader | ||
import com.bluelabs.akkaaws.AwsHeaders.ServerSideEncryptionAlgorithm.AES256 | ||
import com.bluelabs.akkaaws.AwsHeaders.{`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`, `X-Amz-Server-Side-Encryption`, ServerSideEncryptionAlgorithm} | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
import scala.util.{Failure, Success} | ||
|
||
|
||
class AwsHeadersSpec extends FlatSpec with Matchers { | ||
|
||
"ServerSideEncryptionAlgorithm" should "parse AES256" in { | ||
ServerSideEncryptionAlgorithm.fromString("AES256") shouldBe Success(ServerSideEncryptionAlgorithm.AES256) | ||
} | ||
|
||
it should "parse KMS" in { | ||
ServerSideEncryptionAlgorithm.fromString("aws:kms") shouldBe Success(ServerSideEncryptionAlgorithm.KMS) | ||
} | ||
|
||
it should "not parse an unsupported algorithm" in { | ||
ServerSideEncryptionAlgorithm.fromString("Zip War AirGanon") shouldBe a[Failure[_]] | ||
} | ||
|
||
"`X-Amz-Server-Side-Encryption`" should "parse AES256 algorithm" in { | ||
val `X-Amz-Server-Side-Encryption`(algorithm) = `X-Amz-Server-Side-Encryption`("AES256") | ||
algorithm shouldBe AES256 | ||
} | ||
|
||
it should "set the X-Amz-Server-Side-Encryption header" in { | ||
val RawHeader(key, value) = `X-Amz-Server-Side-Encryption`("AES256") | ||
key shouldBe "X-Amz-Server-Side-Encryption" | ||
value shouldBe "AES256" | ||
} | ||
|
||
"`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`" should "parse kms key id" in { | ||
val `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`(id) = `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`("myId") | ||
id shouldBe "myId" | ||
} | ||
|
||
it should "set the X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id header" in { | ||
val RawHeader(key, value) = `X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`("myId") | ||
key shouldBe "X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id" | ||
value shouldBe "myId" | ||
} | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
s3-stream/src/main/scala/com/bluelabs/s3stream/ServerSideEncryption.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,14 @@ | ||
package com.bluelabs.s3stream | ||
|
||
|
||
sealed trait ServerSideEncryption | ||
|
||
object ServerSideEncryption { | ||
|
||
case object None extends ServerSideEncryption | ||
|
||
case object Aes256 extends ServerSideEncryption | ||
|
||
// TODO add context | ||
case class Kms(keyId: String) extends ServerSideEncryption | ||
} |
38 changes: 38 additions & 0 deletions
38
s3-stream/src/test/scala/com/bluelabs/s3stream/HttpRequestsSpec.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,38 @@ | ||
package com.bluelabs.s3stream | ||
|
||
import akka.http.scaladsl.model.ContentTypes | ||
import akka.http.scaladsl.model.headers.`Content-Type` | ||
import com.bluelabs.akkaaws.AwsHeaders.ServerSideEncryptionAlgorithm.{KMS, AES256} | ||
|
||
import org.scalatest.{Matchers, FlatSpec} | ||
|
||
class HttpRequestsSpec extends FlatSpec with Matchers { | ||
import HttpRequests._ | ||
import com.bluelabs.akkaaws.AwsHeaders._ | ||
|
||
"metadataHeaders" should "add the contentType header when contentType is default" in { | ||
metadataHeaders(Metadata()) should contain (`Content-Type`(ContentTypes.`application/octet-stream`)) | ||
} | ||
|
||
it should "add the contentType header contentType is custom" in { | ||
val customContentType = ContentTypes.`application/json` | ||
metadataHeaders(Metadata(customContentType)) should contain (`Content-Type`(customContentType)) | ||
} | ||
|
||
it should "not add the x-amz-server-side-encryption header when the server side encryption is None" in { | ||
metadataHeaders(Metadata(serverSideEncryption = ServerSideEncryption.None)) should not contain a[`X-Amz-Server-Side-Encryption`] | ||
} | ||
|
||
it should "add the x-amz-server-side-encryption with AES256 header when the server side encryption is AES256" in { | ||
metadataHeaders(Metadata(serverSideEncryption = ServerSideEncryption.Aes256)) should contain (`X-Amz-Server-Side-Encryption`(AES256)) | ||
} | ||
|
||
it should "add the x-amz-server-side-encryption with KMZ header when the server side encryption is KMS" in { | ||
metadataHeaders(Metadata(serverSideEncryption = ServerSideEncryption.Kms("my-id"))) should contain (`X-Amz-Server-Side-Encryption`(KMS)) | ||
} | ||
|
||
it should "add the x-amz-server-side-encryption-kms-id with KMZ header when the server side encryption is KMS" in { | ||
metadataHeaders(Metadata(serverSideEncryption = ServerSideEncryption.Kms("my-id"))) should contain (`X-Amz-Server-Side-Encryption-Aws-Kms-Key-Id`("my-id")) | ||
} | ||
|
||
} |