1
0
Fork 0
mirror of https://github.com/edgurgel/httparrot synced 2025-04-05 08:12:31 -04:00

Merge pull request #13 from flupke/response-headers

Add /response-headers endpoint
This commit is contained in:
Eduardo Gurgel 2015-04-29 08:25:45 +12:00
commit 207f8f7364
5 changed files with 72 additions and 2 deletions

View file

@ -39,7 +39,6 @@ HTTP server built on top of Cowboy using (mostly) `cowboy_rest` handlers to serv
## TODO
* [ ] /deflate Returns deflate-encoded data.
* [ ] /response-headers?key=val Returns given response headers.
* [ ] /digest-auth/:qop/:user/:passwd Challenges HTTP Digest Auth.
## License

View file

@ -31,7 +31,8 @@ defmodule HTTParrot do
{'/robots.txt', HTTParrot.RobotsHandler, []},
{'/base64/:value', HTTParrot.Base64Handler, []},
{'/image', HTTParrot.ImageHandler, []},
{'/websocket', HTTParrot.WebsocketHandler, []} ] }
{'/websocket', HTTParrot.WebsocketHandler, []},
{'/response-headers', HTTParrot.ResponseHeadersHandler, []} ] }
])
{:ok, http_port} = Application.fetch_env(:httparrot, :http_port)

View file

@ -0,0 +1,30 @@
defmodule HTTParrot.ResponseHeadersHandler do
@moduledoc """
Returns given response headers.
"""
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
def malformed_request(req, state) do
{qs_vals, req} = :cowboy_req.qs_vals(req)
if not Enum.empty?(qs_vals) do
{false, req, qs_vals}
else
{true, req, state}
end
end
def content_types_provided(req, state) do
{[{{"application", "json", []}, :get_json}], req, state}
end
def get_json(req, qs_vals) do
req = Enum.reduce qs_vals, req, fn({key, value}, req) ->
:cowboy_req.set_resp_header(key, value, req)
end
{response(qs_vals), req, qs_vals}
end
defp response(qs_vals) do
qs_vals |> JSX.encode!
end
end

View file

@ -65,6 +65,7 @@
<li><a href="/ip" data-bare-link="true"><code>/ip</code></a> Returns Origin IP.</li>
<li><a href="/user-agent" data-bare-link="true"><code>/user-agent</code></a> Returns user-agent.</li>
<li><a href="/headers" data-bare-link="true"><code>/headers</code></a> Returns header dict.</li>
<li><a href="/response-headers?k1=v1&amp;k2=v2" data-bare-link="true"><code>/response-headers</code></a> Returns given response headers.</li>
<li><a href="/get" data-bare-link="true"><code>/get</code></a> Returns GET data.</li>
<li><code>/post</code> Returns POST data.</li>
<li><code>/put</code> Returns PUT data.</li>

View file

@ -0,0 +1,39 @@
defmodule HTTParrot.ResponseHeadersHandlerTest do
use ExUnit.Case
import :meck
import HTTParrot.ResponseHeadersHandler
setup do
new :cowboy_req
new JSX
on_exit fn -> unload end
:ok
end
test "malformed_request returns true if query string is empty" do
expect(:cowboy_req, :qs_vals, 1, {[], :req2})
assert malformed_request(:req1, :state) == {true, :req2, :state}
assert validate :cowboy_req
end
test "malformed_request returns false if query string is not empty" do
expect(:cowboy_req, :qs_vals, 1, {[{"foo", "bar"}], :req2})
assert malformed_request(:req1, :state) == {false, :req2, [{"foo", "bar"}]}
assert validate :cowboy_req
end
test "query string parameters are sent as headers" do
expect(:cowboy_req, :set_resp_header, [{[:k1, :v1, :req1], :req2},
{[:k2, :v2, :req2], :req3}])
expect(JSX, :encode!, [{[[k1: :v1, k2: :v2]], :json}])
assert get_json(:req1, [k1: :v1, k2: :v2]) == {:json, :req3, [k1: :v1, k2: :v2]}
assert validate :cowboy_req
assert validate JSX
end
end