diff --git a/README.md b/README.md index 6320766..4ba0bc0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/httparrot.ex b/lib/httparrot.ex index 9db1ace..0921f41 100644 --- a/lib/httparrot.ex +++ b/lib/httparrot.ex @@ -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) diff --git a/lib/httparrot/response_headers_handler.ex b/lib/httparrot/response_headers_handler.ex new file mode 100644 index 0000000..b638927 --- /dev/null +++ b/lib/httparrot/response_headers_handler.ex @@ -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 diff --git a/priv/index.html b/priv/index.html index 42f9218..a67d007 100644 --- a/priv/index.html +++ b/priv/index.html @@ -65,6 +65,7 @@
  • /ip Returns Origin IP.
  • /user-agent Returns user-agent.
  • /headers Returns header dict.
  • +
  • /response-headers Returns given response headers.
  • /get Returns GET data.
  • /post Returns POST data.
  • /put Returns PUT data.
  • diff --git a/test/response_headers_handler_test.exs b/test/response_headers_handler_test.exs new file mode 100644 index 0000000..4d1a527 --- /dev/null +++ b/test/response_headers_handler_test.exs @@ -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