Skip to content

Commit

Permalink
content-type is customizable per route
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Dec 30, 2009
1 parent 6ae29b0 commit e3a84ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/scala/Step.scala
Expand Up @@ -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)
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/scala/StepTest.scala
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}

0 comments on commit e3a84ea

Please sign in to comment.