Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3589CD450 for ; Mon, 5 Nov 2012 05:02:46 +0000 (UTC) Received: (qmail 54916 invoked by uid 500); 5 Nov 2012 05:02:45 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 54728 invoked by uid 500); 5 Nov 2012 05:02:45 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 54554 invoked by uid 99); 5 Nov 2012 05:02:39 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Nov 2012 05:02:39 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 0C07D52D9A; Mon, 5 Nov 2012 05:02:38 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: benoitc@apache.org To: commits@couchdb.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [2/3] git commit: Make name of test file consistent with every other test Message-Id: <20121105050239.0C07D52D9A@tyr.zones.apache.org> Date: Mon, 5 Nov 2012 05:02:38 +0000 (UTC) Make name of test file consistent with every other test Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/84b2d155 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/84b2d155 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/84b2d155 Branch: refs/heads/431-feature_cors Commit: 84b2d155847505b661e3085b4aaac27c0894bcad Parents: cae8e16 Author: Dale Harvey Authored: Sun Nov 4 18:35:39 2012 -0800 Committer: Dale Harvey Committed: Sun Nov 4 18:35:39 2012 -0800 ---------------------------------------------------------------------- test/etap/231-cors.t | 247 +++++++++++++++++++++++++++++++++++++++++++++ test/etap/231_cors.t | 247 --------------------------------------------- 2 files changed, 247 insertions(+), 247 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/84b2d155/test/etap/231-cors.t ---------------------------------------------------------------------- diff --git a/test/etap/231-cors.t b/test/etap/231-cors.t new file mode 100644 index 0000000..85867be --- /dev/null +++ b/test/etap/231-cors.t @@ -0,0 +1,247 @@ +#!/usr/bin/env escript +%% -*- erlang -*- + +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-record(user_ctx, { + name = null, + roles = [], + handler +}). + + +-define(SUPPORTED_METHODS, "GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, COPY, OPTIONS"). +server() -> + lists:concat([ + "http://127.0.0.1:", + mochiweb_socket_server:get(couch_httpd, port), + "/" + ]). + + +main(_) -> + test_util:init_code_path(), + + etap:plan(12), + case (catch test()) of + ok -> + etap:end_tests(); + Other -> + etap:diag(io_lib:format("Test died abnormally: ~p", [Other])), + etap:bail(Other) + end, + ok. + +dbname() -> "etap-test-db". +dbname1() -> "etap-test-db1". +dbname2() -> "etap-test-db2". + +admin_user_ctx() -> {user_ctx, #user_ctx{roles=[<<"_admin">>]}}. + +set_admin_password(UserName, Password) -> + Salt = binary_to_list(couch_uuids:random()), + Hashed = couch_util:to_hex(crypto:sha(Password ++ Salt)), + couch_config:set("admins", UserName, + "-hashed-" ++ Hashed ++ "," ++ Salt, false). + +test() -> + + ibrowse:start(), + crypto:start(), + + %% launch couchdb + couch_server_sup:start_link(test_util:config_files()), + + %% initialize db + timer:sleep(1000), + couch_server:delete(list_to_binary(dbname()), [admin_user_ctx()]), + couch_server:delete(list_to_binary(dbname1()), [admin_user_ctx()]), + couch_server:delete(list_to_binary(dbname2()), [admin_user_ctx()]), + {ok, Db} = couch_db:create(list_to_binary(dbname()), [admin_user_ctx()]), + {ok, Db1} = couch_db:create(list_to_binary(dbname1()), [admin_user_ctx()]), + {ok, Db2} = couch_db:create(list_to_binary(dbname2()), [admin_user_ctx()]), + + % CORS is disabled by default + test_no_headers_server(), + test_no_headers_db(), + + % Now enable CORS + ok = couch_config:set("httpd", "enable_cors", "true", false), + ok = couch_config:set("cors", "origins", "http://example.com", false), + + %% do tests + test_incorrect_origin_simple_request(), + test_incorrect_origin_preflight_request(), + + test_preflight_request(), + test_db_request(), + test_db_preflight_request(), + test_db_origin_request(), + test_db1_origin_request(), + + ok = couch_config:set("cors", "origins", "*", false), + test_preflight_with_wildcard(), + ok = couch_config:set("cors", "origins", "http://example.com", false), + + %% do tests with auth + ok = set_admin_password("test", "test"), + + test_db_preflight_auth_request(), + test_db_origin_auth_request(), + + %% restart boilerplate + catch couch_db:close(Db), + catch couch_db:close(Db1), + catch couch_db:close(Db2), + + couch_server:delete(list_to_binary(dbname()), [admin_user_ctx()]), + couch_server:delete(list_to_binary(dbname1()), [admin_user_ctx()]), + couch_server:delete(list_to_binary(dbname2()), [admin_user_ctx()]), + + timer:sleep(3000), + couch_server_sup:stop(), + ok. + +%% Cors is disabled, should not return Access-Control-Allow-Origin +test_no_headers_server() -> + Headers = [{"Origin", "http://127.0.0.1"}], + {ok, _, Resp, _} = ibrowse:send_req(server(), Headers, get, []), + etap:is(proplists:get_value("Access-Control-Allow-Origin", Resp), + undefined, "No CORS Headers when disabled"). + +%% Cors is disabled, should not return Access-Control-Allow-Origin +test_no_headers_db() -> + Headers = [{"Origin", "http://127.0.0.1"}], + Url = server() ++ "etap-test-db", + {ok, _, Resp, _} = ibrowse:send_req(Url, Headers, get, []), + etap:is(proplists:get_value("Access-Control-Allow-Origin", Resp), + undefined, "No CORS Headers when disabled"). + +test_incorrect_origin_simple_request() -> + Headers = [{"Origin", "http://127.0.0.1"}], + {ok, _, RespHeaders, _} = ibrowse:send_req(server(), Headers, get, []), + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + undefined, + "Specified invalid origin, no Access"). + +test_incorrect_origin_preflight_request() -> + Headers = [{"Origin", "http://127.0.0.1"}, + {"Access-Control-Request-Method", "GET"}], + {ok, _, RespHeaders, _} = ibrowse:send_req(server(), Headers, options, []), + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + undefined, + "invalid origin"). + +test_preflight_request() -> + Headers = [{"Origin", "http://example.com"}, + {"Access-Control-Request-Method", "GET"}], + case ibrowse:send_req(server(), Headers, options, []) of + {ok, _, RespHeaders, _} -> + etap:is(proplists:get_value("Access-Control-Allow-Methods", RespHeaders), + ?SUPPORTED_METHODS, + "test_preflight_request Access-Control-Allow-Methods ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. + +test_db_request() -> + Headers = [{"Origin", "http://example.com"}], + Url = server() ++ "etap-test-db", + case ibrowse:send_req(Url, Headers, get, []) of + {ok, _, RespHeaders, _Body} -> + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + "http://example.com", + "db Access-Control-Allow-Origin ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. + +test_db_preflight_request() -> + Url = server() ++ "etap-test-db", + Headers = [{"Origin", "http://example.com"}, + {"Access-Control-Request-Method", "GET"}], + case ibrowse:send_req(Url, Headers, options, []) of + {ok, _, RespHeaders, _} -> + etap:is(proplists:get_value("Access-Control-Allow-Methods", RespHeaders), + ?SUPPORTED_METHODS, + "db Access-Control-Allow-Methods ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. + + +test_db_origin_request() -> + Headers = [{"Origin", "http://example.com"}], + Url = server() ++ "etap-test-db", + case ibrowse:send_req(Url, Headers, get, []) of + {ok, _, RespHeaders, _Body} -> + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + "http://example.com", + "db origin ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. + +test_db1_origin_request() -> + Headers = [{"Origin", "http://example.com"}], + Url = server() ++ "etap-test-db1", + case ibrowse:send_req(Url, Headers, get, [], [{host_header, "example.com"}]) of + {ok, _, RespHeaders, _Body} -> + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + "http://example.com", + "db origin ok"); + _Else -> + io:format("else ~p~n", [_Else]), + etap:is(false, true, "ibrowse failed") + end. + +test_db_preflight_auth_request() -> + Url = server() ++ "etap-test-db2", + Headers = [{"Origin", "http://example.com"}, + {"Access-Control-Request-Method", "GET"}], + case ibrowse:send_req(Url, Headers, options, []) of + {ok, _Status, RespHeaders, _} -> + etap:is(proplists:get_value("Access-Control-Allow-Methods", RespHeaders), + ?SUPPORTED_METHODS, + "db Access-Control-Allow-Methods ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. + + +test_db_origin_auth_request() -> + Headers = [{"Origin", "http://example.com"}], + Url = server() ++ "etap-test-db2", + + case ibrowse:send_req(Url, Headers, get, [], + [{basic_auth, {"test", "test"}}]) of + {ok, _, RespHeaders, _Body} -> + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + "http://example.com", + "db origin ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. + +test_preflight_with_wildcard() -> + Headers = [{"Origin", "http://example.com"}, + {"Access-Control-Request-Method", "GET"}], + case ibrowse:send_req(server(), Headers, options, []) of + {ok, _, RespHeaders, _} -> + % I would either expect the current origin or a wildcard to be returned + etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), + "http://example.com", + "db origin ok"); + _ -> + etap:is(false, true, "ibrowse failed") + end. http://git-wip-us.apache.org/repos/asf/couchdb/blob/84b2d155/test/etap/231_cors.t ---------------------------------------------------------------------- diff --git a/test/etap/231_cors.t b/test/etap/231_cors.t deleted file mode 100644 index 85867be..0000000 --- a/test/etap/231_cors.t +++ /dev/null @@ -1,247 +0,0 @@ -#!/usr/bin/env escript -%% -*- erlang -*- - -% Licensed under the Apache License, Version 2.0 (the "License"); you may not -% use this file except in compliance with the License. You may obtain a copy of -% the License at -% -% http://www.apache.org/licenses/LICENSE-2.0 -% -% Unless required by applicable law or agreed to in writing, software -% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -% License for the specific language governing permissions and limitations under -% the License. - --record(user_ctx, { - name = null, - roles = [], - handler -}). - - --define(SUPPORTED_METHODS, "GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, COPY, OPTIONS"). -server() -> - lists:concat([ - "http://127.0.0.1:", - mochiweb_socket_server:get(couch_httpd, port), - "/" - ]). - - -main(_) -> - test_util:init_code_path(), - - etap:plan(12), - case (catch test()) of - ok -> - etap:end_tests(); - Other -> - etap:diag(io_lib:format("Test died abnormally: ~p", [Other])), - etap:bail(Other) - end, - ok. - -dbname() -> "etap-test-db". -dbname1() -> "etap-test-db1". -dbname2() -> "etap-test-db2". - -admin_user_ctx() -> {user_ctx, #user_ctx{roles=[<<"_admin">>]}}. - -set_admin_password(UserName, Password) -> - Salt = binary_to_list(couch_uuids:random()), - Hashed = couch_util:to_hex(crypto:sha(Password ++ Salt)), - couch_config:set("admins", UserName, - "-hashed-" ++ Hashed ++ "," ++ Salt, false). - -test() -> - - ibrowse:start(), - crypto:start(), - - %% launch couchdb - couch_server_sup:start_link(test_util:config_files()), - - %% initialize db - timer:sleep(1000), - couch_server:delete(list_to_binary(dbname()), [admin_user_ctx()]), - couch_server:delete(list_to_binary(dbname1()), [admin_user_ctx()]), - couch_server:delete(list_to_binary(dbname2()), [admin_user_ctx()]), - {ok, Db} = couch_db:create(list_to_binary(dbname()), [admin_user_ctx()]), - {ok, Db1} = couch_db:create(list_to_binary(dbname1()), [admin_user_ctx()]), - {ok, Db2} = couch_db:create(list_to_binary(dbname2()), [admin_user_ctx()]), - - % CORS is disabled by default - test_no_headers_server(), - test_no_headers_db(), - - % Now enable CORS - ok = couch_config:set("httpd", "enable_cors", "true", false), - ok = couch_config:set("cors", "origins", "http://example.com", false), - - %% do tests - test_incorrect_origin_simple_request(), - test_incorrect_origin_preflight_request(), - - test_preflight_request(), - test_db_request(), - test_db_preflight_request(), - test_db_origin_request(), - test_db1_origin_request(), - - ok = couch_config:set("cors", "origins", "*", false), - test_preflight_with_wildcard(), - ok = couch_config:set("cors", "origins", "http://example.com", false), - - %% do tests with auth - ok = set_admin_password("test", "test"), - - test_db_preflight_auth_request(), - test_db_origin_auth_request(), - - %% restart boilerplate - catch couch_db:close(Db), - catch couch_db:close(Db1), - catch couch_db:close(Db2), - - couch_server:delete(list_to_binary(dbname()), [admin_user_ctx()]), - couch_server:delete(list_to_binary(dbname1()), [admin_user_ctx()]), - couch_server:delete(list_to_binary(dbname2()), [admin_user_ctx()]), - - timer:sleep(3000), - couch_server_sup:stop(), - ok. - -%% Cors is disabled, should not return Access-Control-Allow-Origin -test_no_headers_server() -> - Headers = [{"Origin", "http://127.0.0.1"}], - {ok, _, Resp, _} = ibrowse:send_req(server(), Headers, get, []), - etap:is(proplists:get_value("Access-Control-Allow-Origin", Resp), - undefined, "No CORS Headers when disabled"). - -%% Cors is disabled, should not return Access-Control-Allow-Origin -test_no_headers_db() -> - Headers = [{"Origin", "http://127.0.0.1"}], - Url = server() ++ "etap-test-db", - {ok, _, Resp, _} = ibrowse:send_req(Url, Headers, get, []), - etap:is(proplists:get_value("Access-Control-Allow-Origin", Resp), - undefined, "No CORS Headers when disabled"). - -test_incorrect_origin_simple_request() -> - Headers = [{"Origin", "http://127.0.0.1"}], - {ok, _, RespHeaders, _} = ibrowse:send_req(server(), Headers, get, []), - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - undefined, - "Specified invalid origin, no Access"). - -test_incorrect_origin_preflight_request() -> - Headers = [{"Origin", "http://127.0.0.1"}, - {"Access-Control-Request-Method", "GET"}], - {ok, _, RespHeaders, _} = ibrowse:send_req(server(), Headers, options, []), - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - undefined, - "invalid origin"). - -test_preflight_request() -> - Headers = [{"Origin", "http://example.com"}, - {"Access-Control-Request-Method", "GET"}], - case ibrowse:send_req(server(), Headers, options, []) of - {ok, _, RespHeaders, _} -> - etap:is(proplists:get_value("Access-Control-Allow-Methods", RespHeaders), - ?SUPPORTED_METHODS, - "test_preflight_request Access-Control-Allow-Methods ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end. - -test_db_request() -> - Headers = [{"Origin", "http://example.com"}], - Url = server() ++ "etap-test-db", - case ibrowse:send_req(Url, Headers, get, []) of - {ok, _, RespHeaders, _Body} -> - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - "http://example.com", - "db Access-Control-Allow-Origin ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end. - -test_db_preflight_request() -> - Url = server() ++ "etap-test-db", - Headers = [{"Origin", "http://example.com"}, - {"Access-Control-Request-Method", "GET"}], - case ibrowse:send_req(Url, Headers, options, []) of - {ok, _, RespHeaders, _} -> - etap:is(proplists:get_value("Access-Control-Allow-Methods", RespHeaders), - ?SUPPORTED_METHODS, - "db Access-Control-Allow-Methods ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end. - - -test_db_origin_request() -> - Headers = [{"Origin", "http://example.com"}], - Url = server() ++ "etap-test-db", - case ibrowse:send_req(Url, Headers, get, []) of - {ok, _, RespHeaders, _Body} -> - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - "http://example.com", - "db origin ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end. - -test_db1_origin_request() -> - Headers = [{"Origin", "http://example.com"}], - Url = server() ++ "etap-test-db1", - case ibrowse:send_req(Url, Headers, get, [], [{host_header, "example.com"}]) of - {ok, _, RespHeaders, _Body} -> - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - "http://example.com", - "db origin ok"); - _Else -> - io:format("else ~p~n", [_Else]), - etap:is(false, true, "ibrowse failed") - end. - -test_db_preflight_auth_request() -> - Url = server() ++ "etap-test-db2", - Headers = [{"Origin", "http://example.com"}, - {"Access-Control-Request-Method", "GET"}], - case ibrowse:send_req(Url, Headers, options, []) of - {ok, _Status, RespHeaders, _} -> - etap:is(proplists:get_value("Access-Control-Allow-Methods", RespHeaders), - ?SUPPORTED_METHODS, - "db Access-Control-Allow-Methods ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end. - - -test_db_origin_auth_request() -> - Headers = [{"Origin", "http://example.com"}], - Url = server() ++ "etap-test-db2", - - case ibrowse:send_req(Url, Headers, get, [], - [{basic_auth, {"test", "test"}}]) of - {ok, _, RespHeaders, _Body} -> - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - "http://example.com", - "db origin ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end. - -test_preflight_with_wildcard() -> - Headers = [{"Origin", "http://example.com"}, - {"Access-Control-Request-Method", "GET"}], - case ibrowse:send_req(server(), Headers, options, []) of - {ok, _, RespHeaders, _} -> - % I would either expect the current origin or a wildcard to be returned - etap:is(proplists:get_value("Access-Control-Allow-Origin", RespHeaders), - "http://example.com", - "db origin ok"); - _ -> - etap:is(false, true, "ibrowse failed") - end.