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

Add '/cookies'

This commit is contained in:
Eduardo Gurgel 2014-01-02 00:06:33 -03:00
parent f4c2ee7466
commit 5b4a5e52f9
3 changed files with 51 additions and 0 deletions

View file

@ -10,6 +10,7 @@ defmodule HTTParrot do
{'/post', HTTParrot.PostHandler, []},
{'/status/:code', HTTParrot.StatusCodeHandler, []},
{'/redirect-to', HTTParrot.RedirectToHandler, []},
{'/cookies', HTTParrot.CookiesHandler, []},
{'/basic-auth/:user/:passwd', HTTParrot.BasicAuthHandler, []},
{'/html', :cowboy_static, {:priv_file, :httparrot, "html.html"}} ] }
])

View file

@ -0,0 +1,25 @@
defmodule HTTParrot.CookiesHandler do
def init(_transport, _req, _opts) do
{:upgrade, :protocol, :cowboy_rest}
end
def allowed_methods(req, state) do
{["GET"], req, state}
end
def content_types_provided(req, state) do
{[{{"application", "json", []}, :get_json}], req, state}
end
def get_json(req, state) do
{cookies, req} = :cowboy_req.cookies(req)
if cookies == [], do: cookies = [{}]
{response(cookies), req, state}
end
defp response(cookies) do
[cookies: cookies] |> JSEX.encode!
end
def terminate(_, _, _), do: :ok
end

View file

@ -0,0 +1,25 @@
defmodule HTTParrot.CookiesHandlerTest do
use ExUnit.Case
import :meck
import HTTParrot.CookiesHandler
setup do
new :cowboy_req
new JSEX
end
teardown do
unload :cowboy_req
unload JSEX
end
test "returns a JSON with all the cookies o/" do
expect(:cowboy_req, :cookies, 1, {[k1: :v1], :req2})
expect(JSEX, :encode!, [{[[cookies: [k1: :v1]]], :json}])
assert get_json(:req1, :state) == {:json, :req2, :state}
assert validate :cowboy_req
assert validate JSEX
end
end