From 5bf6df34e46881ea003008bbfa6896598cfb7590 Mon Sep 17 00:00:00 2001 From: Luper Rouch Date: Mon, 27 Apr 2015 20:08:39 +0200 Subject: [PATCH] Add /response-headers endpoint --- README.md | 1 - lib/httparrot.ex | 3 +- lib/httparrot/response_headers_handler.ex | 26 ++++++++++++++++ priv/index.html | 1 + test/response_headers_handler_test.exs | 36 +++++++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 lib/httparrot/response_headers_handler.ex create mode 100644 test/response_headers_handler_test.exs 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..2752ae8 --- /dev/null +++ b/lib/httparrot/response_headers_handler.ex @@ -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 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..14c8a96 --- /dev/null +++ b/test/response_headers_handler_test.exs @@ -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