Skip to content

Instantly share code, notes, and snippets.

@MoreOutput
Last active August 29, 2015 14:19
Show Gist options
  • Save MoreOutput/8079d42c54b9ad2fc04b to your computer and use it in GitHub Desktop.
Save MoreOutput/8079d42c54b9ad2fc04b to your computer and use it in GitHub Desktop.
Cowboy Module Testing
-module(page_handler).
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/3]).
-record(state, {
}).
init(_, Req, _Opts) ->
{ok, Req, #state{}}.
handle(Req, State=#state{}) ->
{Method, _} = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req),
{ok, Req2} = respond(Method, HasBody, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.
respond(<<"GET">>, false, Req) ->
{ok, Body} = index_dtl:render([
{name, "Michael Blooth"}
]),
cowboy_req:reply(200,
[{<<"content-type">>, <<"text/html">>}],
Body,
Req);
respond(<<"POST">>, true, Req) ->
cowboy_req:reply(200,
[{<<"content-type">>, <<"text/html">>}],
<<"POSTING">>,
Req);
respond(<<"POST">>, false, Req) ->
cowboy_req:reply(400,
[{<<"content-type">>, <<"text/html">>}],
<<"TEST">>,
Req).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment