From ce07ae75b70e14b0571ceabae63675d3edc6d570 Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Thu, 8 Oct 2015 16:26:29 +0200 Subject: [PATCH] +doc,htp #18496 overrideMethodWithParameter --- .../MethodDirectivesExamplesSpec.scala | 26 +++++++++++++++++- .../RespondWithDirectivesExamplesSpec.scala | 19 +------------ .../routing-dsl/directives/alphabetically.rst | 1 - .../overrideMethodWithParameter.rst | 15 ++++++++--- .../respond-with-directives/index.rst | 1 - .../overrideStatusCode.rst | 27 ------------------- .../RespondWithDirectivesSpec.scala | 13 --------- .../directives/RespondWithDirectives.scala | 6 ----- 8 files changed, 38 insertions(+), 70 deletions(-) delete mode 100644 akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/overrideStatusCode.rst diff --git a/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala b/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala index ce5cb64db8..23149a0103 100644 --- a/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala +++ b/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala @@ -99,4 +99,28 @@ class MethodDirectivesExamplesSpec extends RoutingSpec { responseAs[String] shouldEqual "This HEAD request, clearly is not a GET!" } } -} + + "overrideMethodWithParameter-0" in { + val route = + overrideMethodWithParameter("method") { + get { + complete("This looks like a GET request.") + } ~ + post { + complete("This looks like a POST request.") + } + } + + Get("/?method=POST") ~> route ~> check { + responseAs[String] shouldEqual "This looks like a POST request." + } + Post("/?method=get") ~> route ~> check { + responseAs[String] shouldEqual "This looks like a GET request." + } + + Get("/?method=hallo") ~> route ~> check { + status shouldEqual StatusCodes.NotImplemented + } + } + +} \ No newline at end of file diff --git a/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RespondWithDirectivesExamplesSpec.scala b/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RespondWithDirectivesExamplesSpec.scala index 56c1dba636..01601acf72 100644 --- a/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RespondWithDirectivesExamplesSpec.scala +++ b/akka-docs-dev/rst/scala/code/docs/http/scaladsl/server/directives/RespondWithDirectivesExamplesSpec.scala @@ -5,10 +5,7 @@ package docs.http.scaladsl.server package directives -import akka.http.scaladsl.server.UnacceptedResponseContentTypeRejection -import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers._ -import headers._ class RespondWithDirectivesExamplesSpec extends RoutingSpec { @@ -130,18 +127,4 @@ class RespondWithDirectivesExamplesSpec extends RoutingSpec { // } // } - // FIXME https://github.com/akka/akka/issues/18626 - // "respondWithStatus-examples" in { - // val route = - // path("foo") { - // respondWithStatus(201) { - // complete("beep") - // } - // } - // - // Get("/foo") ~> route ~> check { - // status shouldEqual StatusCodes.Created - // responseAs[String] shouldEqual "beep" - // } - // } -} +} \ No newline at end of file diff --git a/akka-docs-dev/rst/scala/http/routing-dsl/directives/alphabetically.rst b/akka-docs-dev/rst/scala/http/routing-dsl/directives/alphabetically.rst index 4cc016064d..f042a8b389 100644 --- a/akka-docs-dev/rst/scala/http/routing-dsl/directives/alphabetically.rst +++ b/akka-docs-dev/rst/scala/http/routing-dsl/directives/alphabetically.rst @@ -139,7 +139,6 @@ Directive Description ``PartialFunction[HttpHeader, T]`` :ref:`-options-` Rejects all non-OPTIONS requests :ref:`-overrideMethodWithParameter-` Changes the request method to the value of the specified query parameter -:ref:`-overrideStatusCode-` Overrides the response status code with the given one :ref:`-parameter-` Extracts a query parameter value from the request :ref:`-parameterMap-` Extracts the request's query parameters as a ``Map[String, String]`` :ref:`-parameterMultiMap-` Extracts the request's query parameters as a ``Map[String, List[String]]`` diff --git a/akka-docs-dev/rst/scala/http/routing-dsl/directives/method-directives/overrideMethodWithParameter.rst b/akka-docs-dev/rst/scala/http/routing-dsl/directives/method-directives/overrideMethodWithParameter.rst index 16777f4c4b..19f01ea5c5 100644 --- a/akka-docs-dev/rst/scala/http/routing-dsl/directives/method-directives/overrideMethodWithParameter.rst +++ b/akka-docs-dev/rst/scala/http/routing-dsl/directives/method-directives/overrideMethodWithParameter.rst @@ -3,7 +3,7 @@ overrideMethodWithParameter =========================== -... +Changes the request method to the value of the specified query parameter. Signature --------- @@ -14,10 +14,19 @@ Signature Description ----------- -... +Changes the HTTP method of the request to the value of the specified query string parameter. +If the query string parameter is not specified this directive has no effect. + +If the query string is specified as something that is not a HTTP method, +then this directive completes the request with a ``501 Not Implemented`` response. + +This directive is useful for: + +- Use in combination with JSONP (JSONP only supports GET) +- Supporting older browsers that lack support for certain HTTP methods. E.g. IE8 does not support PATCH Example ------- .. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala - :snippet: 0overrideMethodWithParameter + :snippet: overrideMethodWithParameter-0 diff --git a/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/index.rst b/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/index.rst index 694532960c..7232fefa57 100644 --- a/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/index.rst +++ b/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/index.rst @@ -6,7 +6,6 @@ RespondWithDirectives .. toctree:: :maxdepth: 1 - overrideStatusCode respondWithDefaultHeader respondWithDefaultHeaders respondWithHeader diff --git a/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/overrideStatusCode.rst b/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/overrideStatusCode.rst deleted file mode 100644 index 7e245d3eb8..0000000000 --- a/akka-docs-dev/rst/scala/http/routing-dsl/directives/respond-with-directives/overrideStatusCode.rst +++ /dev/null @@ -1,27 +0,0 @@ -.. _-overrideStatusCode-: - -overrideStatusCode -================== - -Overrides the status code of all responses coming back from its inner route with the given one. - - -Signature ---------- - -.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/RespondWithDirectives.scala - :snippet: overrideStatusCode - - -Description ------------ - -This directive transforms ``HttpResponse`` and ``ChunkedResponseStart`` messages coming back from its inner route by -unconditionally overriding the status code with the given one. - - -Example -------- - -.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/RespondWithDirectivesExamplesSpec.scala - :snippet: overrideStatusCode-examples \ No newline at end of file diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/RespondWithDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/RespondWithDirectivesSpec.scala index 0287aa2f60..abdd3dcd56 100644 --- a/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/RespondWithDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/RespondWithDirectivesSpec.scala @@ -13,19 +13,6 @@ import akka.http.scaladsl.server._ class RespondWithDirectivesSpec extends RoutingSpec { - "overrideStatusCode" should { - "set the given status on successful responses" in { - Get() ~> { - overrideStatusCode(Created) { completeOk } - } ~> check { response shouldEqual HttpResponse(Created) } - } - "leave rejections unaffected" in { - Get() ~> { - overrideStatusCode(Created) { reject } - } ~> check { rejections shouldEqual Nil } - } - } - val customHeader = RawHeader("custom", "custom") val customHeader2 = RawHeader("custom2", "custom2") val existingHeader = RawHeader("custom", "existing") diff --git a/akka-http/src/main/scala/akka/http/scaladsl/server/directives/RespondWithDirectives.scala b/akka-http/src/main/scala/akka/http/scaladsl/server/directives/RespondWithDirectives.scala index b9cf0050ab..72df156061 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/server/directives/RespondWithDirectives.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/server/directives/RespondWithDirectives.scala @@ -7,12 +7,6 @@ import scala.collection.immutable trait RespondWithDirectives { import BasicDirectives._ - /** - * Overrides the response status code with the given one. - */ - def overrideStatusCode(responseStatus: StatusCode): Directive0 = - mapResponse(_.copy(status = responseStatus)) - /** * Unconditionally adds the given response header to all HTTP responses of its inner Route. */