summaryrefslogtreecommitdiff
path: root/src/couch/test/couchdb_cookie_domain_tests.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/couch/test/couchdb_cookie_domain_tests.erl')
-rwxr-xr-xsrc/couch/test/couchdb_cookie_domain_tests.erl78
1 files changed, 35 insertions, 43 deletions
diff --git a/src/couch/test/couchdb_cookie_domain_tests.erl b/src/couch/test/couchdb_cookie_domain_tests.erl
index 1a9aedb93..e66ab31e6 100755
--- a/src/couch/test/couchdb_cookie_domain_tests.erl
+++ b/src/couch/test/couchdb_cookie_domain_tests.erl
@@ -18,60 +18,52 @@
-define(USER, "cookie_domain_test_admin").
-define(PASS, "pass").
-setup(PortType) ->
+setup() ->
+ Ctx = test_util:start_couch([chttpd]),
Hashed = couch_passwords:hash_admin_password(?PASS),
ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist=false),
Addr = config:get("httpd", "bind_address", "127.0.0.1"),
- lists:concat(["http://", Addr, ":", port(PortType), "/_session"]).
+ Port = mochiweb_socket_server:get(chttpd, port),
+ Url = ?l2b(io_lib:format("http://~s:~b/_session", [Addr, Port])),
+ ContentType = [{"Content-Type", "application/json"}],
+ Payload = jiffy:encode({[{name, ?l2b(?USER)}, {password, ?l2b(?PASS)}]}),
+ {ok, ?b2l(Url), ContentType, ?b2l(Payload), Ctx}.
-teardown(_,_) ->
- ok = config:delete("admins", ?USER, _Persist=false).
+teardown({ok, _, _, _, Ctx}) ->
+ ok = config:delete("admins", ?USER, _Persist=false),
+ test_util:stop_couch(Ctx).
cookie_test_() ->
- Tests = [
- fun should_set_cookie_domain/2,
- fun should_not_set_cookie_domain/2
- ],
{
"Cookie domain tests",
{
setup,
- fun() -> test_util:start_couch([chttpd]) end, fun test_util:stop_couch/1,
- [
- make_test_case(clustered, Tests)
- ]
+ fun setup/0,
+ fun teardown/1,
+ fun({ok, Url, ContentType, Payload, _}) ->
+ [
+ should_set_cookie_domain(Url, ContentType, Payload),
+ should_not_set_cookie_domain(Url, ContentType, Payload)
+ ]
+ end
}
}.
-make_test_case(Mod, Funs) ->
-{
- lists:flatten(io_lib:format("~s", [Mod])),
- {foreachx, fun setup/1, fun teardown/2, [{Mod, Fun} || Fun <- Funs]}
-}.
+should_set_cookie_domain(Url, ContentType, Payload) ->
+ ?_test(begin
+ ok = config:set("couch_httpd_auth", "cookie_domain",
+ "example.com", false),
+ {ok, Code, Headers, _} = test_request:post(Url, ContentType, Payload),
+ ?assertEqual(200, Code),
+ Cookie = proplists:get_value("Set-Cookie", Headers),
+ ?assert(string:str(Cookie, "; Domain=example.com") > 0)
+ end).
-should_set_cookie_domain(_PortType, Url) ->
- ?_assertEqual(true,
- begin
- ok = config:set("couch_httpd_auth", "cookie_domain", "example.com", false),
- {ok, Code, Headers, _} = test_request:post(Url, [{"Content-Type", "application/json"}],
- "{\"name\":\"" ++ ?USER ++ "\", \"password\": \"" ++ ?PASS ++ "\"}"),
- ?_assert(Code =:= 200),
- Cookie = proplists:get_value("Set-Cookie", Headers),
- string:str(Cookie, "; Domain=example.com") > 0
- end).
-
-should_not_set_cookie_domain(_PortType, Url) ->
- ?_assertEqual(0,
- begin
- ok = config:set("couch_httpd_auth", "cookie_domain", "", false),
- {ok, Code, Headers, _} = test_request:post(Url, [{"Content-Type", "application/json"}],
- "{\"name\":\"" ++ ?USER ++ "\", \"password\": \"" ++ ?PASS ++ "\"}"),
- ?_assert(Code =:= 200),
- Cookie = proplists:get_value("Set-Cookie", Headers),
- string:str(Cookie, "; Domain=")
- end).
-
-port(clustered) ->
- integer_to_list(mochiweb_socket_server:get(chttpd, port));
-port(backdoor) ->
- integer_to_list(mochiweb_socket_server:get(couch_httpd, port)).
+should_not_set_cookie_domain(Url, ContentType, Payload) ->
+ ?_test(begin
+ ok = config:set("couch_httpd_auth", "cookie_domain", "", false),
+ {ok, Code, Headers, _} = test_request:post(Url, ContentType, Payload),
+ ?assertEqual(200, Code),
+ Cookie = proplists:get_value("Set-Cookie", Headers),
+ ?assertEqual(0, string:str(Cookie, "; Domain="))
+ end).