Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TargetArn as valid destination for publish #47

Merged
merged 2 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.1.0
sbt.version=1.1.1
11 changes: 9 additions & 2 deletions src/main/scala/me/snov/sns/api/PublishApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import spray.json._

import scala.concurrent.{ExecutionContext, Future}

case class InvalidTopicArnException(msg: String) extends Exception(msg)

object PublishApi {
private val arnPattern = """([\w+_:-]{1,512})""".r
Expand All @@ -25,9 +26,9 @@ object PublishApi {
formField('Action ! "Publish") {
formFieldSeq { fields =>
val messageAttributes: Map[String, MessageAttribute] = MessageAttribute.parse(fields)
formFields('TopicArn, 'MessageStructure.?, 'Message) { (topicArn, messageStructure, message) =>
formFields('TopicArn.?, 'TargetArn.?, 'MessageStructure.?, 'Message) { (topicArnMaybe, targetArnMaybe, messageStructure, message) =>
try {
topicArn match {
topicArn(topicArnMaybe, targetArnMaybe) match {
case arnPattern(topic) => complete {
val bodies = messageStructure match {
case Some("json") => message.parseJson.asJsObject.convertTo[Map[String, String]]
Expand All @@ -44,6 +45,7 @@ object PublishApi {
case _ => complete(HttpResponse(400, entity = "Invalid topic ARN"))
}
} catch {
case e: InvalidTopicArnException => complete(HttpResponse(400, entity = e.getMessage))
case e: RuntimeException => complete(HttpResponse(400, entity = e.getMessage))
}
}
Expand All @@ -52,4 +54,9 @@ object PublishApi {
}
}
}

private def topicArn(topicArnMaybe: Option[String], targetArnMaybe: Option[String]): String = {
topicArnMaybe.getOrElse(targetArnMaybe.getOrElse(throw InvalidTopicArnException("Neither TopicArn nor TargetArn provided")))
}
}

18 changes: 18 additions & 0 deletions src/test/scala/me/snov/sns/api/PublishSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ class PublishSpec extends WordSpec with Matchers with ScalatestRouteTest {
}
}

"Sends publish command to TargetArn" in {
val params = Map(
"Action" -> "Publish",
"TargetArn" -> "foo",
"Message" -> "bar"
)

probe.setAutoPilot(new TestActor.AutoPilot {
def run(sender: ActorRef, msg: Any) = {
sender ! Message(Map("default" -> "foo"))
this
}
})
Post("/", FormData(params)) ~> route ~> check {
probe.expectMsg(CmdPublish("foo", Map("default" -> "bar"), Map.empty))
}
}

"Sends publish command with attributes" in {
val params = Map(
"Action" -> "Publish",
Expand Down