Permalink
Browse files

! testkit: in RouteTests always convert URIs into absolute ones, fixes

…#464

This is to mirror the behavior of spray-can / spray-servlet which make
request URIs always absolute (using the supplied `Host` header).
  • Loading branch information...
jrudolph committed Aug 27, 2013
1 parent cf08747 commit 72c939702b8f287eb07a47c32533f9603e8fed0e
@@ -58,7 +58,7 @@ The basic structure of a test built with *spray-testkit* is this (expression pla
In this template *REQUEST* is an expression evaluating to an ``HttpRequest`` instance. Since both *RouteTest* traits
extend the *spray-httpx* :ref:`RequestBuilding` trait you have access to its mini-DSL for convenient and concise request
-construction.
+construction. [1]_
*ROUTE* is an expression evaluating to a *spray-routing* ``Route``. You can specify one inline or simply refer to the
route structure defined in your service.
@@ -110,6 +110,10 @@ Inspector Description
``ChunkedMessageEnd`` response part.
================================================ =======================================================================
+.. [1] If the request URI is relative it will be made absolute using an implicitly available instance of
+ ``DefaultHostInfo`` whose value is "http://example.com" by default. This mirrors the behavior of *spray-can*
+ which always produces absolute URIs for incoming request based on the request URI and the ``Host``-header of
+ the request. You can customize this behavior by bringing an instance of ``DefaultHostInfo`` into scope.
Sealing Routes
--------------
@@ -44,7 +44,7 @@ class CachingDirectivesSpec extends RoutingSpec with CachingDirectives {
}
}
def prime(route: Route) = {
- route(RequestContext(HttpRequest(), system.deadLetters, Uri.Path.Empty).withDefaultSender(system.deadLetters))
+ route(RequestContext(HttpRequest(uri = Uri("http://example.com/")), system.deadLetters, Uri.Path.Empty).withDefaultSender(system.deadLetters))
route
}
@@ -43,7 +43,7 @@ class DebuggingDirectivesSpec extends RoutingSpec {
"produce a proper log message for incoming requests" in {
Get("/hello") ~> logRequest("1") { completeOk } ~> check {
response === Ok
- debugMsg === "1: HttpRequest(GET,/hello,List(),EmptyEntity,HTTP/1.1)\n"
+ debugMsg === "1: HttpRequest(GET,http://example.com/hello,List(),EmptyEntity,HTTP/1.1)\n"
}
}
}
@@ -64,7 +64,7 @@ class DebuggingDirectivesSpec extends RoutingSpec {
Get("/hello") ~> logRequestResponse("3") { completeOk } ~> check {
response === Ok
debugMsg === """|3: Response for
- | Request : HttpRequest(GET,/hello,List(),EmptyEntity,HTTP/1.1)
+ | Request : HttpRequest(GET,http://example.com/hello,List(),EmptyEntity,HTTP/1.1)
| Response: HttpResponse(200 OK,EmptyEntity,List(),HTTP/1.1)
|""".stripMargin.replace(EOL, "\n")
}
@@ -86,20 +86,26 @@ trait RouteTest extends RequestBuilding with RouteResultComponent {
def apply(request: HttpRequest, f: A B): Out
}
+ case class DefaultHostInfo(host: HttpHeaders.Host, securedConnection: Boolean)
+ object DefaultHostInfo {
+ implicit def defaultHost: DefaultHostInfo =
+ DefaultHostInfo(HttpHeaders.Host("example.com"), securedConnection = false)
+ }
object TildeArrow {
implicit object InjectIntoRequestTransformer extends TildeArrow[HttpRequest, HttpRequest] {
type Out = HttpRequest
def apply(request: HttpRequest, f: HttpRequest HttpRequest) = f(request)
}
implicit def injectIntoRoute(implicit timeout: RouteTestTimeout, settings: RoutingSettings,
- log: LoggingContext, eh: ExceptionHandler) =
+ log: LoggingContext, eh: ExceptionHandler, defaultHostInfo: DefaultHostInfo) =
new TildeArrow[RequestContext, Unit] {
type Out = RouteResult
def apply(request: HttpRequest, route: Route) = {
val routeResult = new RouteResult(timeout.duration)
val effectiveRequest =
- try request.withEffectiveUri(securedConnection = false)
- catch { case NonFatal(_) request }
+ request.withEffectiveUri(
+ securedConnection = defaultHostInfo.securedConnection,
+ defaultHostHeader = defaultHostInfo.host)
ExecutionDirectives.handleExceptions(eh orElse ExceptionHandler.default)(route) {
RequestContext(
request = effectiveRequest,

0 comments on commit 72c9397

Please sign in to comment.