Skip to content

Commit

Permalink
Added test suite, misc cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngerakines committed Jun 13, 2009
1 parent 27e8121 commit 4b00a11
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 7 deletions.
14 changes: 7 additions & 7 deletions c/mocheventcnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ void request_handler(struct evhttp_request *req, void *arg) {
u_char *data = EVBUFFER_DATA(req->input_buffer);
size_t len = EVBUFFER_LENGTH(req->input_buffer);
char *body;
if ((body = malloc(len + 1)) == NULL) {
if ((body = malloc(len)) == NULL) {
evbuffer_drain(req->input_buffer, len);
}

memcpy(body, data, len);
body[len] = '\0';
evbuffer_drain(req->input_buffer, len + 1);
// body[len] = '\0';
evbuffer_drain(req->input_buffer, len);

ETERM *harray[50]; // Wasted space if there are less than 50 headers
int hcount = 0;
struct evkeyval *header; // XXX Not being released.
struct evkeyval *header;
TAILQ_FOREACH(header, req->input_headers, next) {
if (hcount == 50) {
if (hcount != 50) {
ETERM *harr[2];
harr[0] = erl_mk_string((const char *) header->key);
harr[1] = erl_mk_string((const char *) header->value);
Expand All @@ -122,7 +122,7 @@ void request_handler(struct evhttp_request *req, void *arg) {
arr[2] = erl_mk_int(req->type);
arr[3] = erl_mk_binary(req->uri, sizeof(req->uri));
arr[4] = erl_mk_list(harray, hcount);
arr[5] = erl_mk_binary(body, len + 1);
arr[5] = erl_mk_binary(body, len);
emsg2 = erl_mk_tuple(arr, 6);

pthread_mutex_lock(&clients_mutex);
Expand Down Expand Up @@ -336,7 +336,7 @@ int main(int argc, char **argv) {
}
}
if (ipaddress == NULL) {
ipaddress = "127.0.0.1";
ipaddress = "0.0.0.0";
}
if (remotenode == NULL) {
remotenode = "httpdmaster@localhost";
Expand Down
1 change: 1 addition & 0 deletions foo.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It's a secret to everybody.
30 changes: 30 additions & 0 deletions t/001-load.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -name httpdmaster -pa ./ebin -boot start_sasl -setcookie supersecret

main(_) ->
etap:plan(unknown),
error_logger:tty(false),
inets:start(),
case (catch start()) of
{'EXIT', Err} ->
io:format("Err ~p~n", [Err]),
etap:bail();
_ ->
etap:end_tests()
end,
ok.

start() ->
mochevent:start({mochevent, default}),
os:cmd("for i in `cat /tmp/mochevent.lock`; do kill $i; done"),
os:cmd("./bin/mocheventcnode --daemon --ip 0.0.0.0 --port 5001 --master httpdmaster@`hostname`"),

(fun() ->
Request = etap_web:build_request(get, "http://127.0.0.1:5001/nick", [], []),
Request:status_is(200, "status code ok"),
Request:body_is("The rain in Spain falls gently on the plain.", "body ok"),
ok
end)(),

ok.
68 changes: 68 additions & 0 deletions t/002-methods.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -name httpdmaster -pa ./ebin -boot start_sasl -setcookie supersecret

main(_) ->
etap:plan(unknown),
error_logger:tty(false),
inets:start(),
case (catch start()) of
{'EXIT', Err} ->
io:format("Err ~p~n", [Err]),
etap:bail();
_ ->
etap:end_tests()
end,
ok.

start() ->
Pid = spawn_link(fun() -> server() end),
register(mochevent_handler, Pid),

os:cmd("for i in `cat /tmp/mochevent.lock`; do kill $i; done"),
os:cmd("./bin/mocheventcnode --daemon --ip 0.0.0.0 --port 5001 --master httpdmaster@`hostname`"),

(fun() ->
Request = etap_web:build_request(get, "http://127.0.0.1:5001/nick", [], []),
Request:status_is(200, "status code ok"),
Request:body_is("Received request method was GET", "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(post, "http://127.0.0.1:5001/nick", [], "POST body"),
Request:status_is(200, "status code ok"),
Request:body_is("Received request method was POST", "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(put, "http://127.0.0.1:5001/nick", [], "PUT body"),
Request:status_is(200, "status code ok"),
Request:body_is("Received request method was PUT", "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(delete, "http://127.0.0.1:5001/nick", [], []),
Request:status_is(200, "status code ok"),
Request:body_is("Received request method was DELETE", "body ok"),
ok
end)(),

ok.

server() ->
receive
{Pid, Id, Method, Uri, Headers, Body} ->
Req = mochevent_request:new(Pid, Id, Method, Uri, {1, 0}, mochiweb_headers:make(Headers), Body),
OutBody = erlang:iolist_to_binary([<<"Received request method was ">>, clean_method(Method)]),
Req:respond({200, [{<<"content-type">>, <<"text/plain">>}], OutBody})
end,
server().

clean_method(0) -> "GET";
clean_method(1) -> "POST";
clean_method(3) -> "PUT";
clean_method(4) -> "DELETE";
clean_method(_) -> "OTHER".
68 changes: 68 additions & 0 deletions t/003-request_body.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -name httpdmaster -pa ./ebin -boot start_sasl -setcookie supersecret

main(_) ->
etap:plan(unknown),
error_logger:tty(false),
inets:start(),
case (catch start()) of
{'EXIT', Err} ->
io:format("Err ~p~n", [Err]),
etap:bail();
_ ->
etap:end_tests()
end,
ok.

start() ->
Pid = spawn_link(fun() -> server() end),
register(mochevent_handler, Pid),

os:cmd("for i in `cat /tmp/mochevent.lock`; do kill $i; done"),
os:cmd("./bin/mocheventcnode --daemon --ip 0.0.0.0 --port 5001 --master httpdmaster@`hostname`"),

(fun() ->
Request = etap_web:build_request(get, "http://127.0.0.1:5001/nick", [], "GET body"),
Request:status_is(200, "status code ok"),
Request:body_is("", "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(post, "http://127.0.0.1:5001/nick", [], "POST body"),
Request:status_is(200, "status code ok"),
Request:body_is("POST body", "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(put, "http://127.0.0.1:5001/nick", [], "PUT body"),
Request:status_is(200, "status code ok"),
Request:body_is("PUT body", "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(delete, "http://127.0.0.1:5001/nick", [], "DELETE body"),
Request:status_is(200, "status code ok"),
Request:body_is("", "body ok"),
ok
end)(),

ok.

server() ->
receive
{Pid, Id, Method, Uri, Headers, Body} ->
Req = mochevent_request:new(Pid, Id, Method, Uri, {1, 0}, mochiweb_headers:make(Headers), Body),
OutBody = erlang:iolist_to_binary([<<"Received request method was ">>, clean_method(Method)]),
Req:respond({200, [{<<"content-type">>, <<"text/plain">>}], Body})
end,
server().

clean_method(0) -> "GET";
clean_method(1) -> "POST";
clean_method(3) -> "PUT";
clean_method(4) -> "DELETE";
clean_method(_) -> "OTHER".
81 changes: 81 additions & 0 deletions t/004-headers.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -name httpdmaster -pa ./ebin -boot start_sasl -setcookie supersecret

main(_) ->
etap:plan(unknown),
error_logger:tty(false),
inets:start(),
case (catch start()) of
{'EXIT', Err} ->
io:format("Err ~p~n", [Err]),
etap:bail();
_ ->
etap:end_tests()
end,
ok.

gen_headers(N) ->
[{"header" ++ integer_to_list(X), "value" ++ integer_to_list(X)} || X <- lists:seq(1, N)].

start() ->
Pid = spawn_link(fun() -> server() end),
register(mochevent_handler, Pid),

os:cmd("for i in `cat /tmp/mochevent.lock`; do kill $i; done"),
os:cmd("./bin/mocheventcnode --daemon --ip 0.0.0.0 --port 5001 --master httpdmaster@`hostname`"),

(fun() ->
Request = etap_web:build_request(get, "http://127.0.0.1:5001/nick", gen_headers(25), "GET body"),
Request:status_is(200, "status code ok"),
Request:body_is(header_body(gen_headers(25)), "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(get, "http://127.0.0.1:5001/nick", gen_headers(51), "GET body"),
Request:status_is(200, "status code ok"),
Request:body_is(header_body(gen_headers(47)), "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(post, "http://127.0.0.1:5001/nick", gen_headers(25), "POST body"),
Request:status_is(200, "status code ok"),
Request:body_is(header_body(gen_headers(25)), "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(put, "http://127.0.0.1:5001/nick", gen_headers(25), "PUT body"),
Request:status_is(200, "status code ok"),
Request:body_is(header_body(gen_headers(25)), "body ok"),
ok
end)(),

(fun() ->
Request = etap_web:build_request(delete, "http://127.0.0.1:5001/nick", gen_headers(25), "DELETE body"),
Request:status_is(200, "status code ok"),
Request:body_is(header_body(gen_headers(25)), "body ok"),
ok
end)(),

ok.

server() ->
receive
{Pid, Id, Method, Uri, Headers, Body} ->
Req = mochevent_request:new(Pid, Id, Method, Uri, {1, 0}, mochiweb_headers:make(Headers), Body),
OutBody = header_body(Headers),
Req:respond({200, [{<<"content-type">>, <<"text/plain">>}], list_to_binary(OutBody)})
end,
server().

header_body(Headers) ->
lists:foldl(
fun ({"header" ++ _ = K, V}, Str) -> lists:append([Str, K ++ ":" ++ V ++ "\n"]);
(_, Str) -> Str
end,
"",
Headers
).
41 changes: 41 additions & 0 deletions t/010-special.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -name httpdmaster -pa ./ebin -boot start_sasl -setcookie supersecret

main(_) ->
etap:plan(unknown),
error_logger:tty(false),
inets:start(),
case (catch start()) of
{'EXIT', Err} ->
io:format("Err ~p~n", [Err]),
etap:bail();
_ ->
etap:end_tests()
end,
ok.

start() ->
Pid = spawn_link(fun() -> server() end),
register(mochevent_handler, Pid),

os:cmd("for i in `cat /tmp/mochevent.lock`; do kill $i; done"),
os:cmd("./bin/mocheventcnode --daemon --ip 0.0.0.0 --port 5001 --master httpdmaster@`hostname`"),

(fun() ->
Request = etap_web:build_request(get, "http://127.0.0.1:5001/foo.bin", [], []),
Request:status_is(200, "status code ok"),
Request:body_is("It's a secret to everybody.\n", "body ok"),
ok
end)(),

ok.

server() ->
receive
{Pid, Id, Method, <<"/foo.bin">>, Headers, Body} ->
Req = mochevent_request:new(Pid, Id, Method, <<"/foo.bin">>, {1, 0}, mochiweb_headers:make(Headers), Body),
{ok, Binary} = file:read_file("foo.bin"),
Req:respond({200, [{<<"content-type">>, <<"application/octet-stream">>}], Binary})
end,
server().

0 comments on commit 4b00a11

Please sign in to comment.