From e3a84ea8e6b0556c1c558ef94b4aca63288b27ce Mon Sep 17 00:00:00 2001 From: KUOKA Yusuke Date: Wed, 30 Dec 2009 10:12:06 -0800 Subject: [PATCH] content-type is customizable per route --- src/main/scala/Step.scala | 5 +++-- src/test/scala/StepTest.scala | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/scala/Step.scala b/src/main/scala/Step.scala index fe3255284..3f3e67777 100644 --- a/src/main/scala/Step.scala +++ b/src/main/scala/Step.scala @@ -59,12 +59,13 @@ abstract class Step extends HttpServlet def isMatchingRoute(route: Route) = { def exec(args: Params) = { before _ - response setContentType contentType _request.withValue(request) { _response.withValue(response) { _session.withValue(request) { paramsMap.withValue(args ++ realParams withDefaultValue(null)) { - response.getWriter print route.action() + val res = route.action() + response setContentType contentType + response.getWriter print (res) } } } diff --git a/src/test/scala/StepTest.scala b/src/test/scala/StepTest.scala index 535b78cba..67d7f8221 100644 --- a/src/test/scala/StepTest.scala +++ b/src/test/scala/StepTest.scala @@ -69,6 +69,16 @@ class TestServlet extends Step { get("/print_host") { "host:" + request.host + ",port:" + request.port } + + get("/content_type/json") { + contentType = "application/json; charset=utf-8" + """{msg: "test"}""" + } + + get("/content_type/html") { + contentType = "text/html; charset=utf-8" + "test" + } } class StepSuite extends FunSuite with ShouldMatchers { @@ -225,4 +235,24 @@ class StepSuite extends FunSuite with ShouldMatchers { response.parse(tester.getResponses(request.generate())) response.getContent should equal ("posted_value is null") } + + test("content-type test") { + request.setMethod("GET") + request.setContent("") + + // default is "text/html" + request.setURI("/") + response.parse(tester.getResponses(request.generate)) + response.getHeader("Content-Type") should equal ("text/html; charset=utf-8") + + // set content-type to "application/json" + request.setURI("/content_type/json") + response.parse(tester.getResponses(request.generate)) + response.getHeader("Content-Type") should equal ("application/json; charset=utf-8") + + // set content-type to "text/html" again + request.setURI("/content_type/html") + response.parse(tester.getResponses(request.generate)) + response.getHeader("Content-Type") should equal ("text/html; charset=utf-8") + } }