Permalink
Browse files

add a test for request shaping

  • Loading branch information...
1 parent de9ca04 commit ba8e4dfa7183e56ee1f21f7970a396005e37036a @capflam capflam committed with vinoski May 27, 2011
Showing with 61 additions and 1 deletion.
  1. +8 −0 test/conf/stdconf.conf
  2. +1 −1 test/t2/Makefile
  3. +12 −0 test/t2/app_test.erl
  4. +40 −0 test/t2/shapertest.erl
View
@@ -166,3 +166,11 @@ use_fdsrv = false
appmods = posttest reentranttest
</server>
+<server localhost>
+ port = 8007
+ listen = 0.0.0.0
+ docroot = %YTOP%/www
+ auth_log = true
+ shaper = shapertest
+</server>
+
View
@@ -4,7 +4,7 @@ include ../support/include.mk
#
all: conf setup app_test.beam streamtest.beam jsontest.beam posttest.beam \
- reentranttest.beam phptest.beam rewritetest.beam
+ reentranttest.beam phptest.beam rewritetest.beam shapertest.beam
@echo "all ok"
View
@@ -26,6 +26,7 @@ start() ->
reentrant_test(),
php_handler_test(),
arg_rewrite_test(),
+ shaper_test(),
ibrowse:stop().
@@ -654,6 +655,17 @@ arg_rewrite_test_response() ->
ok.
+
+shaper_test() ->
+ io:format("shaper_test\n", []),
+ Uri = "http://localhost:8007/",
+ ?line {ok, "200", _, _} = ibrowse:send_req(Uri, [], get),
+ ?line {ok, "200", _, _} = ibrowse:send_req(Uri, [], get),
+ ?line {ok, "200", _, _} = ibrowse:send_req(Uri, [], get),
+ ?line {ok, "503", _, _} = ibrowse:send_req(Uri, [], get),
+ ok.
+
+
%% used for appmod tests
%%
out(_A) ->
View
@@ -0,0 +1,40 @@
+-module(shapertest).
+-behaviour(yaws_shaper).
+
+-export([
+ check/1,
+ update/3
+ ]).
+
+-include("../../include/yaws.hrl").
+-include("../../include/yaws_api.hrl").
+
+-define(SHAPER_DB, shaper_db).
+
+check(Ip) ->
+ try_create(),
+ case ets:lookup(?SHAPER_DB, Ip) of
+ [{Ip, NbHit}] when NbHit > 2 ->
+ {deny, 503, "Request quota exceeded"};
+ _ ->
+ allow
+ end.
+
+update(Ip, Hits, _Bytes) ->
+ try_create(),
+ case ets:member(?SHAPER_DB, Ip) of
+ true -> ets:update_counter(?SHAPER_DB, Ip, Hits);
+ false -> ets:insert(?SHAPER_DB, {Ip, Hits})
+ end.
+
+
+
+
+try_create() ->
+ case ets:info(?SHAPER_DB) of
+ undefined ->
+ ets:new(?SHAPER_DB, [set, public, named_table,
+ {heir, whereis(yaws_server), []}]);
+ _ ->
+ ok
+ end.

0 comments on commit ba8e4df

Please sign in to comment.