tokumei v0.8.3 Tokumei.Helpers View Source
Selection of utilities.
Link to this section Summary
Link to this section Functions
Redirect a request
Examples
# redirects back to referrer if provided
iex> Raxx.request(:GET, "/")
...> |> Raxx.set_header("referer", "/foo")
...> |> Helpers.back("/bar")
...> |> Map.get(:headers)
[{"location", "/foo"}]
# redirects to default path if none provided
iex> Raxx.request(:GET, "/")
...> |> Helpers.back("/bar")
...> |> Map.get(:headers)
[{"location", "/bar"}]
# redirect is see other by default
iex> Raxx.request(:GET, "/")
...> |> Helpers.back("/bar")
...> |> Map.get(:status)
303
Create a response directing the client to a new location
Examples
# Returns status 303 when only path given
iex> Helpers.redirect("/foo").status
303
# Location is set as redirected path
iex> Helpers.redirect("/foo").headers
[{"location", "/foo"}]
# Status can be passed as the second argument
iex> Helpers.redirect("/foo", 301).status
301
# Redirection can include a query
iex> Helpers.redirect({"/", %{key: "value"}}).headers
[{"location", "/?key=value"}]
# Redirection will set body including escaped url
iex> Helpers.redirect({"/", %{a: "b", c: "d"}}).body
...> |> String.contains?("/?a=b&c=d")
true
# Body can be overwritten by passing string as second argument
iex> Helpers.redirect("/", "Not here!").body
"Not here!"
# Body can be overwritten by passing io_list as second argument
iex> Helpers.redirect("/", ["Not here", "!"]).body
["Not here", "!"]
Notes
- 303 Instructs a client to make a
GET
request to the new location - 303 status was added to HTTP/1.1, if a client makes a HTTP/1.0 request then a 303 status should be rewritten to 302
- Could have
redirect/2,3
always take a request to generate absolute URI - And have separate
redirect_external/1,2
- Middleware could write relative redirects if required.
- Optionally extend routers to make a redirect helper that takes a resource/locale
MyApp.redirect(request, {:user, 24}, 301)
- Link to sinatra test for proxy logic https://github.com/sinatra/sinatra/blob/9bd0d40229f76ff60d81c01ad2f4b1a8e6f31e05/test/helpers_test.rb#L183