2015-10-11 05:41:23 -04:00
require " rails_helper "
2015-02-05 00:08:52 -05:00
require_dependency " middleware/request_tracker "
describe Middleware :: RequestTracker do
def env ( opts = { } )
{
" HTTP_HOST " = > " http://test.com " ,
" REQUEST_URI " = > " /path?bla=1 " ,
" REQUEST_METHOD " = > " GET " ,
" rack.input " = > " "
} . merge ( opts )
end
context " log_request " do
2015-02-25 19:40:57 -05:00
before do
2015-02-05 00:08:52 -05:00
freeze_time Time . now
ApplicationRequest . clear_cache!
2015-02-25 19:40:57 -05:00
end
def log_tracked_view ( val )
data = Middleware :: RequestTracker . get_data ( env (
" HTTP_DISCOURSE_TRACK_VIEW " = > val
) , [ " 200 " , { " Content-Type " = > 'text/html' } ] )
Middleware :: RequestTracker . log_request ( data )
end
it " can exclude/include based on custom header " do
log_tracked_view ( " true " )
log_tracked_view ( " 1 " )
log_tracked_view ( " false " )
log_tracked_view ( " 0 " )
ApplicationRequest . write_cache!
2015-04-25 11:18:35 -04:00
expect ( ApplicationRequest . page_view_anon . first . count ) . to eq ( 2 )
2015-02-25 19:40:57 -05:00
end
it " can log requests correctly " do
2015-02-05 00:08:52 -05:00
2015-02-10 01:03:33 -05:00
data = Middleware :: RequestTracker . get_data ( env (
2015-02-05 22:39:04 -05:00
" HTTP_USER_AGENT " = > " AdsBot-Google (+http://www.google.com/adsbot.html) "
2015-02-10 01:03:33 -05:00
) , [ " 200 " , { " Content-Type " = > 'text/html' } ] )
Middleware :: RequestTracker . log_request ( data )
data = Middleware :: RequestTracker . get_data ( env (
2015-02-05 22:39:04 -05:00
" HTTP_DISCOURSE_TRACK_VIEW " = > " 1 "
2015-02-10 01:03:33 -05:00
) , [ " 200 " , { } ] )
Middleware :: RequestTracker . log_request ( data )
2015-02-05 00:08:52 -05:00
2015-07-03 17:02:57 -04:00
data = Middleware :: RequestTracker . get_data ( env (
" HTTP_USER_AGENT " = > " Mozilla/5.0 (iPhone; CPU iPhone OS 8_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B410 Safari/600.1.4 "
) , [ " 200 " , { " Content-Type " = > 'text/html' } ] )
Middleware :: RequestTracker . log_request ( data )
2015-02-05 00:08:52 -05:00
ApplicationRequest . write_cache!
2015-07-03 17:02:57 -04:00
expect ( ApplicationRequest . http_total . first . count ) . to eq ( 3 )
expect ( ApplicationRequest . http_2xx . first . count ) . to eq ( 3 )
2015-02-05 00:08:52 -05:00
2015-07-03 17:02:57 -04:00
expect ( ApplicationRequest . page_view_anon . first . count ) . to eq ( 2 )
2015-04-25 11:18:35 -04:00
expect ( ApplicationRequest . page_view_crawler . first . count ) . to eq ( 1 )
2015-07-03 17:02:57 -04:00
expect ( ApplicationRequest . page_view_anon_mobile . first . count ) . to eq ( 1 )
2015-02-05 00:08:52 -05:00
end
end
end