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

Changed exception when sending msg to queue without proper url #493

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.message.BasicNameValuePair
import org.elasticmq._
import org.elasticmq.rest.sqs.model.RedrivePolicy
import org.scalatest._
import org.scalatest.matchers.should.Matchers

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -1948,6 +1947,13 @@ class AmazonJavaSdkTestSuite extends SqsClientServerCommunication with Matchers
}
}

test("should throws proper exception when referencing queue by name") {
val _ = client.createQueue(new CreateQueueRequest("testQueue1")).getQueueUrl

val ex = the[AmazonSQSException] thrownBy client.sendMessage(new SendMessageRequest("testQueue12", "Message 1"))
ex.getMessage should include("WrongURLFormatRejection(Provided only queueName instead of the full URL)")
}

def queueDelay(queueUrl: String): Long = getQueueLongAttribute(queueUrl, delaySecondsAttribute)

def getQueueLongAttribute(queueUrl: String, attributeName: String): Long = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.elasticmq.rest.sqs.directives

import akka.actor.ActorRef
import akka.http.scaladsl.server.{Directive1, Directives, MissingFormFieldRejection, Route}
import akka.http.scaladsl.server.{Directive1, Directives, MissingQueryParamRejection, Rejection, Route}
import org.elasticmq.QueueData
import org.elasticmq.actor.reply._
import org.elasticmq.msg.{GetQueueData, LookupQueue}
Expand Down Expand Up @@ -45,13 +45,14 @@ trait QueueDirectives {

private def queueNameFromRequest(p: AnyParams)(body: String => Route): Route = {
val queueNameDirective =
checkOnlyQueueNameInUri() |
pathPrefix(accountId.r / Segment).tmap(_._2) |
pathPrefix(QueueUrlContext / Segment) |
queueNameFromParams(p) |
queueUrlFromParams(p).flatMap { queueUrl =>
lastPathSegment.findFirstMatchIn(queueUrl).map(_.group(2)) match {
case Some(queueName) => provide(queueName)
case None => reject(MissingFormFieldRejection(queueUrlParameter)): Directive1[String]
case None => reject(MissingQueryParamRejection(queueUrlParameter)): Directive1[String]
}
}

Expand All @@ -76,4 +77,18 @@ trait QueueDirectives {
body(queueData)
}
}

private def checkOnlyQueueNameInUri(): Directive1[String] = {
extractUri.flatMap {
uri => {
val onlyQueueNameInUriPath = Option(uri.path.toString().tail)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this always return Some(the uri without the first character)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for advice, now I'm checking if there is only one segment in uri.

onlyQueueNameInUriPath match {
case Some(_) => reject(WrongURLFormatRejection("Provided only queueName instead of the full URL"))
case None => reject(WrongURLFormatRejection("Empty URL"))
}
}
}
}
}

final case class WrongURLFormatRejection(fieldName: String) extends Rejection
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.elasticmq.rest

import akka.http.scaladsl.server.{MissingQueryParamRejection, Directive1, Directive0}
import akka.http.scaladsl.server.{Directive0, Directive1, MissingQueryParamRejection}
import akka.http.scaladsl.server.Directives._

package object sqs {
Expand All @@ -24,3 +24,4 @@ package object sqs {
}
}
}