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 09C7A175C4 for ; Tue, 2 Jun 2015 19:35:36 +0000 (UTC) Received: (qmail 36785 invoked by uid 500); 2 Jun 2015 19:35:33 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 36693 invoked by uid 500); 2 Jun 2015 19:35:33 -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 34958 invoked by uid 99); 2 Jun 2015 19:35:32 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Jun 2015 19:35:32 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BC8C4E0281; Tue, 2 Jun 2015 19:35:32 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: chewbranca@apache.org To: commits@couchdb.apache.org Date: Tue, 02 Jun 2015 19:36:04 -0000 Message-Id: In-Reply-To: <1b227edc773e4cbabd298c26188e5f92@git.apache.org> References: <1b227edc773e4cbabd298c26188e5f92@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [33/50] couch commit: updated refs/heads/2080-port-cors-to-chttpd to 529339b Add CORS test case for couch_mrview_show COUCHDB-2656 Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/6ddb2ab7 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/6ddb2ab7 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/6ddb2ab7 Branch: refs/heads/2080-port-cors-to-chttpd Commit: 6ddb2ab7a1af7a9def8b22dea013cc9f400ea084 Parents: b6d8665 Author: ILYA Khlopotov Authored: Thu Apr 16 07:49:30 2015 -0700 Committer: ILYA Khlopotov Committed: Thu Apr 16 08:01:33 2015 -0700 ---------------------------------------------------------------------- test/couchdb_mrview_cors_tests.erl | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/6ddb2ab7/test/couchdb_mrview_cors_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb_mrview_cors_tests.erl b/test/couchdb_mrview_cors_tests.erl new file mode 100644 index 0000000..76b7948 --- /dev/null +++ b/test/couchdb_mrview_cors_tests.erl @@ -0,0 +1,108 @@ +% 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. + +-module(couchdb_mrview_cors_tests). + +-include_lib("couch/include/couch_eunit.hrl"). +-include_lib("couch/include/couch_db.hrl"). + + + +-define(DDOC, {[ + {<<"_id">>, <<"_design/foo">>}, + {<<"shows">>, {[ + {<<"bar">>, <<"function(doc, req) {return '

wosh

';}">>} + ]}} +]}). + +start() -> + ok = test_util:start_couch([chttpd]), + ok = config:set("httpd", "enable_cors", "true", false), + ok = config:set("vhosts", "example.com", "/", false), + ok. + +setup(PortType) -> + DbName = ?tempdb(), + ok = create_db(PortType, DbName), + + config:set("cors", "credentials", "false", false), + config:set("cors", "origins", "http://example.com", false), + + Addr = config:get("httpd", "bind_address", "127.0.0.1"), + Host = "http://" ++ Addr ++ ":" ++ port(PortType), + upload_ddoc(Host, ?b2l(DbName)), + {Host, ?b2l(DbName)}. + +teardown(PortType, {_Host, DbName}) -> + delete_db(PortType, ?l2b(DbName)), + ok. + +cors_test_() -> + { + "CORS for mrview", + { + setup, + fun start/0, fun test_util:stop_couch/1, + [show_tests()] + } + }. + +show_tests() -> + { + "Check CORS for show", + [ + make_test_case(clustered, [fun should_make_shows_request/2]), + make_test_case(backdoor, [fun should_make_shows_request/2]) + ] + }. + +make_test_case(Mod, Funs) -> + { + lists:flatten(io_lib:format("~s", [Mod])), + {foreachx, fun setup/1, fun teardown/2, [{Mod, Fun} || Fun <- Funs]} + }. + +should_make_shows_request(_, {Host, DbName}) -> + ?_test(begin + ReqUrl = Host ++ "/" ++ DbName ++ "/_design/foo/_show/bar", + Headers = [{"Origin", "http://example.com"}, + {"Access-Control-Request-Method", "GET"}], + {ok, _, Resp, Body} = test_request:get(ReqUrl, Headers), + Origin = proplists:get_value("Access-Control-Allow-Origin", Resp), + ?assertEqual("http://example.com", Origin), + ?assertEqual(<<"

wosh

">>, Body) + end). + +create_db(backdoor, DbName) -> + {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]), + couch_db:close(Db); +create_db(clustered, DbName) -> + ok = fabric:create_db(DbName, [?ADMIN_CTX]). + +delete_db(backdoor, DbName) -> + couch_server:delete(DbName, [?ADMIN_CTX]); +delete_db(clustered, DbName) -> + ok = fabric:delete_db(DbName, [?ADMIN_CTX]). + + + +port(clustered) -> + integer_to_list(mochiweb_socket_server:get(chttpd, port)); +port(backdoor) -> + integer_to_list(mochiweb_socket_server:get(couch_httpd, port)). + + +upload_ddoc(Host, DbName) -> + Url = Host ++ "/" ++ DbName ++ "/_design/foo", + Body = couch_util:json_encode(?DDOC), + {ok, 201, _Resp, _Body} = test_request:put(Url, Body), + ok.