Skip to content

Commit

Permalink
Merge pull request #6 from hindsholm/minor-tweaks
Browse files Browse the repository at this point in the history
Support for redirection
  • Loading branch information
Tim Berglund committed Jan 8, 2013
2 parents 367ee07 + e84512a commit 605d027
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Request {

@Lazy byte[] input = servletRequest.inputStream.bytes
@Lazy Map<String, ?> queryParams = new ParamParser().parse(servletRequest.queryString, servletRequest.getCharacterEncoding())
@Lazy String text = new String(input, servletRequest.getCharacterEncoding())
@Lazy String text = servletRequest.characterEncoding ? new String(input, servletRequest.characterEncoding) : new String(input, 'ISO-8859-1')
@Lazy Object json = new JsonSlurper().parseText(getText())
@Lazy Map<String, ?> params = new ParamParser().parse(getText(), servletRequest.getCharacterEncoding())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.bleedingwolf.ratpack.request

import javax.servlet.http.HttpServletResponse
import org.mortbay.jetty.HttpHeaders

class ResponderDsl {

final Request request
Expand All @@ -37,5 +40,14 @@ class ResponderDsl {
String renderString(String string) {
response.renderString(string)
}

/**
* Sends a temporary redirect response to the client using the specified redirect location URL.
* @param location the redirect location URL
*/
void sendRedirect(String location) {
response.status = HttpServletResponse.SC_MOVED_TEMPORARILY
response.headers[HttpHeaders.LOCATION] = new URL(new URL(request.servletRequest.requestURL.toString()), location).toString()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ class BasicRatpackSpec extends RatpackSpec {
urlText() == "bar"
}

def "can redirect"() {
given:
ratpackFile << """
get("/") {
sendRedirect "/foo"
}
get("/foo") {
renderString "foo"
}
"""

when:
app.start()

then:
urlText('') == "foo"
}

}

0 comments on commit 605d027

Please sign in to comment.