1818 <<" OPTIONS" >>,
1919 <<" PATCH" >>]).
2020-define (DEFAULT_PREFIX , [? HANDLER ]).
21- -define (CLASSES , [<<" 1XX " >>, << " 2XX" >>, <<" 3XX" >>, <<" 4XX" >>, <<" 5XX" >>]).
21+ -define (CLASSES , [<<" 2XX" >>, <<" 3XX" >>, <<" 4XX" >>, <<" 5XX" >>]).
2222
2323
2424
@@ -39,12 +39,10 @@ all() ->
3939% %-------------------------------------------------------------------
4040
4141init_per_suite (Config ) ->
42- application :ensure_all_started (gun ),
4342 ejabberd_helper :start_ejabberd_with_config (Config , " ejabberd.cfg" ),
4443 Config .
4544
4645end_per_suite (_Config ) ->
47- application :stop (gun ),
4846 ejabberd_helper :stop_ejabberd (),
4947 ok .
5048
@@ -71,8 +69,8 @@ metrics_are_not_recorded_when_record_metrics_is_false(_Config) ->
7169 destroy_metrics ().
7270
7371metrics_are_not_recorded () ->
74- run_requests ([get , head , post , put , delete , options , patch ],
75- [<<" 100 " >>, << " 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>]),
72+ run_requests ([get , head , post , put , delete , options , patch ],
73+ [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>]),
7674 [ensure_metric_value (M , 0 )
7775 || M <- count_metrics (? DEFAULT_PREFIX ) ++ latency_metrics (? DEFAULT_PREFIX )].
7876
@@ -81,10 +79,11 @@ metrics_are_recorded(_Config) ->
8179 start_listener ([{'_' , ? HANDLER , []}], [{record_metrics , true }]),
8280
8381 timer :sleep (1000 ),
82+ Statuses = [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>],
8483 run_requests ([get , head , post , put , delete , options , patch ],
85- [<< " 100 " >>, << " 200 " >>, << " 300 " >>, << " 400 " >>, << " 500 " >>] ),
84+ Statuses ),
8685
87- [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), 5 ) || M <- ? METHODS ],
86+ [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), length ( Statuses ) ) || M <- ? METHODS ],
8887 [ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , C ), 1 ) || M <- ? METHODS , C <- ? CLASSES ],
8988 [ensure_metric_bumped (response_latency_metric (? DEFAULT_PREFIX , M , C )) || M <- ? METHODS , C <- ? CLASSES ],
9089
@@ -98,10 +97,11 @@ metrics_are_recorded_with_configured_prefix(_Config) ->
9897 start_listener ([{'_' , ? HANDLER , []}], [{record_metrics , true },
9998 {handler_to_metric_prefix , #{? HANDLER => Prefix }}]),
10099
100+ Statuses = [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>],
101101 run_requests ([get , head , post , put , delete , options , patch ],
102- [<< " 100 " >>, << " 200 " >>, << " 300 " >>, << " 400 " >>, << " 500 " >>] ),
102+ Statuses ),
103103
104- [ensure_metric_value (request_count_metric (Prefix , M ), 5 ) || M <- ? METHODS ],
104+ [ensure_metric_value (request_count_metric (Prefix , M ), length ( Statuses ) ) || M <- ? METHODS ],
105105 [ensure_metric_value (response_count_metric (Prefix , M , C ), 1 ) || M <- ? METHODS , C <- ? CLASSES ],
106106 [ensure_metric_bumped (response_latency_metric (Prefix , M , C )) || M <- ? METHODS , C <- ? CLASSES ],
107107
@@ -112,12 +112,13 @@ unsent_responses_generate_metrics(_Config) ->
112112 create_metrics (),
113113 start_listener ([{'_' , ? HANDLER , [{action , none }]}], [{record_metrics , true }]),
114114
115+ Statuses = [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>],
115116 run_requests ([get , head , post , put , delete , options , patch ],
116- [<< " 100 " >>, << " 200 " >>, << " 300 " >>, << " 400 " >>, << " 500 " >>] ),
117+ Statuses ),
117118
118119 % % when response is not sent Cowboy returns 204
119- [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), 5 ) || M <- ? METHODS ],
120- [ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , <<" 2XX" >>), 5 ) || M <- ? METHODS ],
120+ [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), length ( Statuses ) ) || M <- ? METHODS ],
121+ [ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , <<" 2XX" >>), length ( Statuses ) ) || M <- ? METHODS ],
121122 [ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , C ), 0 ) || M <- ? METHODS , C <- ? CLASSES -- [<<" 2XX" >>]],
122123 [ensure_metric_bumped (response_latency_metric (? DEFAULT_PREFIX , M , <<" 2XX" >>)) || M <- ? METHODS ],
123124 [ensure_metric_value (response_latency_metric (? DEFAULT_PREFIX , M , C ), 0 ) || M <- ? METHODS , C <- ? CLASSES -- [<<" 2XX" >>]],
@@ -135,12 +136,12 @@ init(_Type, Req, Opts) ->
135136handle (Req , echo_status = State ) ->
136137 {BinStatus , Req1 } = cowboy_req :qs_val (<<" status" >>, Req ),
137138 Status = binary_to_integer (BinStatus ),
138- {ok , RespReq } = cowboy_req :reply (Status , [], <<>>, Req1 ),
139+ {ok , RespReq } = cowboy_req :reply (Status , Req1 ),
139140 {ok , RespReq , State };
140141handle (Req , none = State ) ->
141142 {ok , Req , State }.
142143
143- terminate (_Reason , _Req , _State ) ->
144+ terminate (Reason , _Req , _State ) ->
144145 ok .
145146
146147% %-------------------------------------------------------------------
@@ -192,10 +193,19 @@ ensure_metric_bumped(Metric) ->
192193 ? assertNotEqual (0 , proplists :get_value (value , DataPoints )).
193194
194195run_requests (Methods , Statuses ) ->
195- {ok , Conn } = gun :open (" localhost" , ranch :get_port (? LISTENER )),
196- {ok , _ } = gun :await_up (Conn ),
197- [begin
198- StreamRef = gun :M (Conn , <<" /?status=" , S /binary >>, []),
199- {response , fin , _ , _ } = gun :await (Conn , StreamRef )
200- end || M <- Methods , S <- Statuses ].
201-
196+ Port = ranch :get_port (? LISTENER ),
197+ {ok , Conn } = fusco :start_link ({" localhost" , Port , false }, []),
198+ Res = [{ok , _Result } = fusco :request (Conn , <<" /?status=" , S /binary >>, method_to_upper_bin (M ), [], [], 5000 )
199+ || M <- Methods , S <- Statuses ],
200+ fusco :disconnect (Conn ),
201+ Res .
202+
203+ method_to_upper_bin (Method ) when is_binary (Method ) ->
204+ Method ;
205+ method_to_upper_bin (Method ) when is_atom (Method ) ->
206+ MethodBin = atom_to_binary (Method , utf8 ),
207+ list_to_binary (
208+ string :to_upper (
209+ binary_to_list (MethodBin )
210+ )
211+ ).
0 commit comments