Skip to content
This repository
Browse code

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

  • Loading branch information...
commit ae90a965a02782662ecc81699f3f1688114b555e 1 parent c30144e
Yomi Colledge authored

Showing 2 changed files with 22 additions and 19 deletions. Show diff stats Hide diff stats

  1. +16 14 lib/chatterl/src/cwiga.erl
  2. +6 5 lib/chatterl/src/test_cwiga.erl
30 lib/chatterl/src/cwiga.erl
@@ -53,34 +53,36 @@ dispatch_requests(Req) ->
53 53 handle('POST',"/groups/send/" ++ Group,ContentType,Post) ->
54 54 [{"client",Sender},{"msg",Message}] = Post,
55 55 Response = chatterl_mid_man:group_send(ContentType,{Group,Sender,Message}),
56   - case check_json_response(Response) of
57   - {<<"failure">>,_} -> failure(Response,ContentType);
58   - {<<"success">>,_} -> success(Response,ContentType);
59   - {<<"error">>,_} -> error(Response,ContentType)
60   - end;
  56 + handle_response(Response,ContentType);
61 57 handle('GET',"/users/connect/" ++ Client,ContentType,_Post) ->
62   - success(chatterl_mid_man:connect(ContentType,Client),ContentType);
  58 + handle_response(chatterl_mid_man:connect(ContentType,Client),ContentType);
63 59 handle('GET',"/users/disconnect/" ++ Client,ContentType,_Post) ->
64   - success(chatterl_mid_man:disconnect(ContentType,Client),ContentType);
  60 + handle_response(chatterl_mid_man:disconnect(ContentType,Client),ContentType);
65 61 handle('GET',"/users/list/" ++ Group,ContentType,_Post) ->
66   - success(chatterl_mid_man:user_list(ContentType,Group),ContentType);
  62 + handle_response(chatterl_mid_man:user_list(ContentType,Group),ContentType);
67 63 handle('GET',"/users/list",ContentType,_Post) ->
68   - success(chatterl_mid_man:user_list(ContentType),ContentType);
  64 + handle_response(chatterl_mid_man:user_list(ContentType),ContentType);
69 65 handle('GET',"/users/poll/" ++ Client,ContentType,_Post) ->
70   - success(chatterl_mid_man:user_poll(ContentType,Client),ContentType);
  66 + handle_response(chatterl_mid_man:user_poll(ContentType,Client),ContentType);
71 67 handle('GET',"/users/groups/" ++ Client,ContentType,_Post) ->
72   - success(chatterl_mid_man:user_groups(ContentType,Client),ContentType);
  68 + handle_response(chatterl_mid_man:user_groups(ContentType,Client),ContentType);
73 69 handle('GET',"/groups/poll/" ++ Group,ContentType,_Post) ->
74   - success(chatterl_mid_man:group_poll(ContentType,Group),ContentType);
  70 + handle_response(chatterl_mid_man:group_poll(ContentType,Group),ContentType);
75 71 handle('GET',"/groups/list",ContentType,_Post) ->
76   - success(chatterl_mid_man:group_list(ContentType),ContentType);
  72 + handle_response(chatterl_mid_man:group_list(ContentType),ContentType);
77 73 handle('GET',"/groups/info/" ++ Group,ContentType,_Post) ->
78   - success(chatterl_mid_man:group_info(ContentType,Group),ContentType);
  74 + handle_response(chatterl_mid_man:group_info(ContentType,Group),ContentType);
79 75 handle(_,Path,ContentType,_) ->
80 76 Response = message_handler:get_response_body(ContentType,
81 77 message_handler:build_carrier("error", "Unknown command: " ++Path)),
82 78 {404, [{"Content-Type", ContentType}], list_to_binary(Response)}.
83 79
  80 +handle_response(Response,ContentType) ->
  81 + case check_json_response(Response) of
  82 + {<<"failure">>,_} -> failure(Response,ContentType);
  83 + {<<"success">>,_} -> success(Response,ContentType);
  84 + {<<"error">>,_} -> error(Response,ContentType)
  85 + end.
84 86
85 87 check_json_response(Json) ->
86 88 {struct,[{<<"chatterl">>,{struct,[{<<"response">>,{struct,[Response]}}]}}]} = mochijson2:decode(Json),
11 lib/chatterl/src/test_cwiga.erl
@@ -62,7 +62,7 @@ handles_test_() ->
62 62 end},
63 63 {"CWIGA can retrieve responses in XML format",
64 64 fun() ->
65   - Response = http:request("http://127.0.0.1:8080/users/list.xml"),
  65 + Response = http:request("http://127.0.0.1:8080/list.xml"),
66 66 ?assertEqual({"content-type","text/xml"},check_response(content_type,Response))
67 67 end},
68 68 {"CWIGA can connect clients to chatterl",
@@ -102,7 +102,7 @@ groups_handle_test_() ->
102 102 Response = http:request("http://127.0.0.1:8080/users/groups/" ++ Client),
103 103 Response2 = http:request("http://127.0.0.1:8080/users/groups/" ++ "blah"),
104 104 ?assertEqual(200,check_response(code,Response)),
105   - ?assertEqual(200,check_response(code,Response2)),
  105 + ?assertEqual(501,check_response(code,Response2)),
106 106 ?assertEqual({struct,[{<<"groups">>,[]}]},check_json(mochijson2:decode(check_response(body,Response)))),
107 107 ?assertEqual(<<"Client: blah doesn't exist">>,check_json(mochijson2:decode(check_response(body,Response2))))
108 108 end},
@@ -110,8 +110,8 @@ groups_handle_test_() ->
110 110 fun() ->
111 111 Response = http:request("http://127.0.0.1:8080/users/poll/" ++ "blah"),
112 112 Response2 = http:request("http://127.0.0.1:8080/users/poll/" ++ Client),
113   - ?assertEqual(200,check_response(code,Response)),
114   - ?assertEqual(200,check_response(code,Response)),
  113 + ?assertEqual(501,check_response(code,Response)),
  114 + ?assertEqual(200,check_response(code,Response2)),
115 115 ?assertEqual(<<"Client: blah doesn't exist">>,check_json(mochijson2:decode(check_response(body,Response)))),
116 116 ?assertEqual({struct,[{<<"messages">>,[]}]},check_json(mochijson2:decode(check_response(body,Response2))))
117 117 end},
@@ -147,7 +147,8 @@ groups_send_message_handle_test_() ->
147 147 Args = [{"msg","hey"},{"client","blah"}],
148 148 Body = set_params(Args),
149 149 Response = http:request(post, {?URL ++ "/groups/send/" ++ Group, [], "application/x-www-form-urlencoded", Body}, [], []),
150   - ?assertEqual(501,check_response(code,Response))
  150 + ?assertEqual(501,check_response(code,Response)),
  151 + ?assertEqual(<<"Unable to send msg!">>,check_json(mochijson2:decode(check_response(body,Response))))
151 152 end},
152 153 {"CWIGA allows clients to send messages to chatterl groups",
153 154 fun() ->

0 comments on commit ae90a96

Please sign in to comment.
Something went wrong with that request. Please try again.