mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 08:12:31 -04:00
Add /response-headers endpoint
This commit is contained in:
parent
77759d9127
commit
5bf6df34e4
5 changed files with 65 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
26
lib/httparrot/response_headers_handler.ex
Normal file
26
lib/httparrot/response_headers_handler.ex
Normal file
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
{"", req, qs_vals}
|
||||
end
|
||||
end
|
|
@ -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&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>
|
||||
|
|
36
test/response_headers_handler_test.exs
Normal file
36
test/response_headers_handler_test.exs
Normal file
|
@ -0,0 +1,36 @@
|
|||
defmodule HTTParrot.ResponseHeadersHandlerTest do
|
||||
use ExUnit.Case
|
||||
import :meck
|
||||
import HTTParrot.ResponseHeadersHandler
|
||||
|
||||
setup do
|
||||
new :cowboy_req
|
||||
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}])
|
||||
|
||||
assert get_json(:req1, [k1: :v1, k2: :v2]) == {"", :req3, [k1: :v1, k2: :v2]}
|
||||
|
||||
assert validate :cowboy_req
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue