Permalink
Browse files

Refactored CWIGA to take advantage of the new handle_responses/2 method

  • Loading branch information...
1 parent c30144e commit ae90a965a02782662ecc81699f3f1688114b555e @baphled committed Mar 28, 2009
Showing with 22 additions and 19 deletions.
  1. +16 −14 lib/chatterl/src/cwiga.erl
  2. +6 −5 lib/chatterl/src/test_cwiga.erl
View
30 lib/chatterl/src/cwiga.erl
@@ -53,34 +53,36 @@ dispatch_requests(Req) ->
handle('POST',"/groups/send/" ++ Group,ContentType,Post) ->
[{"client",Sender},{"msg",Message}] = Post,
Response = chatterl_mid_man:group_send(ContentType,{Group,Sender,Message}),
- case check_json_response(Response) of
- {<<"failure">>,_} -> failure(Response,ContentType);
- {<<"success">>,_} -> success(Response,ContentType);
- {<<"error">>,_} -> error(Response,ContentType)
- end;
+ handle_response(Response,ContentType);
handle('GET',"/users/connect/" ++ Client,ContentType,_Post) ->
- success(chatterl_mid_man:connect(ContentType,Client),ContentType);
+ handle_response(chatterl_mid_man:connect(ContentType,Client),ContentType);
handle('GET',"/users/disconnect/" ++ Client,ContentType,_Post) ->
- success(chatterl_mid_man:disconnect(ContentType,Client),ContentType);
+ handle_response(chatterl_mid_man:disconnect(ContentType,Client),ContentType);
handle('GET',"/users/list/" ++ Group,ContentType,_Post) ->
- success(chatterl_mid_man:user_list(ContentType,Group),ContentType);
+ handle_response(chatterl_mid_man:user_list(ContentType,Group),ContentType);
handle('GET',"/users/list",ContentType,_Post) ->
- success(chatterl_mid_man:user_list(ContentType),ContentType);
+ handle_response(chatterl_mid_man:user_list(ContentType),ContentType);
handle('GET',"/users/poll/" ++ Client,ContentType,_Post) ->
- success(chatterl_mid_man:user_poll(ContentType,Client),ContentType);
+ handle_response(chatterl_mid_man:user_poll(ContentType,Client),ContentType);
handle('GET',"/users/groups/" ++ Client,ContentType,_Post) ->
- success(chatterl_mid_man:user_groups(ContentType,Client),ContentType);
+ handle_response(chatterl_mid_man:user_groups(ContentType,Client),ContentType);
handle('GET',"/groups/poll/" ++ Group,ContentType,_Post) ->
- success(chatterl_mid_man:group_poll(ContentType,Group),ContentType);
+ handle_response(chatterl_mid_man:group_poll(ContentType,Group),ContentType);
handle('GET',"/groups/list",ContentType,_Post) ->
- success(chatterl_mid_man:group_list(ContentType),ContentType);
+ handle_response(chatterl_mid_man:group_list(ContentType),ContentType);
handle('GET',"/groups/info/" ++ Group,ContentType,_Post) ->
- success(chatterl_mid_man:group_info(ContentType,Group),ContentType);
+ handle_response(chatterl_mid_man:group_info(ContentType,Group),ContentType);
handle(_,Path,ContentType,_) ->
Response = message_handler:get_response_body(ContentType,
message_handler:build_carrier("error", "Unknown command: " ++Path)),
{404, [{"Content-Type", ContentType}], list_to_binary(Response)}.
+handle_response(Response,ContentType) ->
+ case check_json_response(Response) of
+ {<<"failure">>,_} -> failure(Response,ContentType);
+ {<<"success">>,_} -> success(Response,ContentType);
+ {<<"error">>,_} -> error(Response,ContentType)
+ end.
check_json_response(Json) ->
{struct,[{<<"chatterl">>,{struct,[{<<"response">>,{struct,[Response]}}]}}]} = mochijson2:decode(Json),
View
11 lib/chatterl/src/test_cwiga.erl
@@ -62,7 +62,7 @@ handles_test_() ->
end},
{"CWIGA can retrieve responses in XML format",
fun() ->
- Response = http:request("http://127.0.0.1:8080/users/list.xml"),
+ Response = http:request("http://127.0.0.1:8080/list.xml"),
?assertEqual({"content-type","text/xml"},check_response(content_type,Response))
end},
{"CWIGA can connect clients to chatterl",
@@ -102,16 +102,16 @@ groups_handle_test_() ->
Response = http:request("http://127.0.0.1:8080/users/groups/" ++ Client),
Response2 = http:request("http://127.0.0.1:8080/users/groups/" ++ "blah"),
?assertEqual(200,check_response(code,Response)),
- ?assertEqual(200,check_response(code,Response2)),
+ ?assertEqual(501,check_response(code,Response2)),
?assertEqual({struct,[{<<"groups">>,[]}]},check_json(mochijson2:decode(check_response(body,Response)))),
?assertEqual(<<"Client: blah doesn't exist">>,check_json(mochijson2:decode(check_response(body,Response2))))
end},
{"CWIGA allows clients to poll chatterl for messages",
fun() ->
Response = http:request("http://127.0.0.1:8080/users/poll/" ++ "blah"),
Response2 = http:request("http://127.0.0.1:8080/users/poll/" ++ Client),
- ?assertEqual(200,check_response(code,Response)),
- ?assertEqual(200,check_response(code,Response)),
+ ?assertEqual(501,check_response(code,Response)),
+ ?assertEqual(200,check_response(code,Response2)),
?assertEqual(<<"Client: blah doesn't exist">>,check_json(mochijson2:decode(check_response(body,Response)))),
?assertEqual({struct,[{<<"messages">>,[]}]},check_json(mochijson2:decode(check_response(body,Response2))))
end},
@@ -147,7 +147,8 @@ groups_send_message_handle_test_() ->
Args = [{"msg","hey"},{"client","blah"}],
Body = set_params(Args),
Response = http:request(post, {?URL ++ "/groups/send/" ++ Group, [], "application/x-www-form-urlencoded", Body}, [], []),
- ?assertEqual(501,check_response(code,Response))
+ ?assertEqual(501,check_response(code,Response)),
+ ?assertEqual(<<"Unable to send msg!">>,check_json(mochijson2:decode(check_response(body,Response))))
end},
{"CWIGA allows clients to send messages to chatterl groups",
fun() ->

0 comments on commit ae90a96

Please sign in to comment.