Repository: couchdb-couch Updated Branches: refs/heads/master fc1899bed -> 44fe5b6b2
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/master Commit: 6ddb2ab7a1af7a9def8b22dea013cc9f400ea084 Parents: b6d8665 Author: ILYA Khlopotov <[email protected]> Authored: Thu Apr 16 07:49:30 2015 -0700 Committer: ILYA Khlopotov <[email protected]> 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 '<h1>wosh</h1>';}">>} + ]}} +]}). + +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(<<"<h1>wosh</h1>">>, 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.
