Return-Path: X-Original-To: apmail-couchdb-dev-archive@www.apache.org Delivered-To: apmail-couchdb-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 43C7210A38 for ; Mon, 1 Dec 2014 12:41:16 +0000 (UTC) Received: (qmail 91243 invoked by uid 500); 1 Dec 2014 12:41:16 -0000 Delivered-To: apmail-couchdb-dev-archive@couchdb.apache.org Received: (qmail 91178 invoked by uid 500); 1 Dec 2014 12:41:16 -0000 Mailing-List: contact dev-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 dev@couchdb.apache.org Received: (qmail 91167 invoked by uid 99); 1 Dec 2014 12:41:15 -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, 01 Dec 2014 12:41:15 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 5AC6B988872; Mon, 1 Dec 2014 12:41:15 +0000 (UTC) From: hdiedrich To: dev@couchdb.apache.org Reply-To: dev@couchdb.apache.org References: In-Reply-To: Subject: [GitHub] couchdb-chttpd pull request: Dynamic endpoints handlers Content-Type: text/plain Message-Id: <20141201124115.5AC6B988872@tyr.zones.apache.org> Date: Mon, 1 Dec 2014 12:41:15 +0000 (UTC) Github user hdiedrich commented on a diff in the pull request: https://github.com/apache/couchdb-chttpd/pull/10#discussion_r21086105 --- Diff: test/chttpd_handler_callback_test.erl --- @@ -0,0 +1,266 @@ +%% 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. +%% +%% @doc +%% These whitebox tests call the endpoints that are implemented by dynamic funs, +%% which replace the previously hardwired handlers, with a dynamically created +%% module. This test mocks everything except the proper relationship between +%% handler function clause and returned fun. The handler functions are called +%% directly. +%% +%% This can look like mocks testing themselves. In this case, in other words: +%% think of the former url_handler function in chttpd.erl: The relationship +%% between a function clause and the specific returned fun() is exactly what is +%% established by the dynamically created module. And it's precisely what this +%% test verifies, for every endpoint. +%% +%% This test was part of chttpd-cloudant dbcore-2.7.7, verifying the original +%% hardcoded endpoints. It has since been reworked but remains functionally +%% mostly identical; the config source and processing has been replaced. + +-module(chttpd_handler_callback_test). + +-include_lib("eunit/include/eunit.hrl"). + +all_test_() -> + io:format(user, "~nEndpoint handler callbacks:~n", []), + {setup, + fun() -> chttpd_handler:build(test_cfg()) end, + [ + + % url handlers + fun() -> + assertReturns("", chttpd_misc, handle_welcome_req) + end, + fun() -> + assertReturns("favicon.ico", chttpd_misc, handle_favicon_req) + end, + fun() -> + assertReturns("_utils", chttpd_misc, handle_utils_dir_req) + end, + fun() -> + assertReturns("_all_dbs", chttpd_misc, handle_all_dbs_req) + end, + fun() -> + assertReturns("_active_tasks", chttpd_misc, handle_task_status_req) + end, + fun() -> + assertReturns("_config", chttpd_misc, handle_config_req) + end, + fun() -> + assertReturns("_reload_query_servers", chttpd_misc, + handle_reload_query_servers_req) + end, + fun() -> + assertReturns("_replicate", chttpd_misc, handle_replicate_req) + end, + fun() -> + assertReturns("_uuids", chttpd_misc, handle_uuids_req) + end, + fun() -> + assertReturns("_sleep", chttpd_misc, handle_sleep_req) + end, + fun() -> + assertReturns("_session", chttpd_auth, handle_session_req) + end, + fun() -> + assertReturns("_oauth", couch_httpd_oauth, handle_oauth_req) + end, + fun() -> + assertReturns("_up", chttpd_misc, handle_up_req) + end, + fun() -> + assertReturns("_membership", mem3_httpd, handle_membership_req) + end, + fun() -> + assertReturns("_db_updates", global_changes_httpd, + handle_global_changes_req) end, + fun() -> + assertReturns("anything", chttpd_db, handle_request) + end, + + % Test the tests: if the final target function is missing, the call must + % fail, with any parameter. All parameters are valid. + fun() -> + assertUnmockedFails("", chttpd_misc) + end, + fun() -> + assertUnmockedFails("favicon.ico", chttpd_misc) + end, + fun() -> + assertUnmockedFails(anything, chttpd_db) + end, + + + % db url handler tests + + fun() -> + assertReturns(db_url_handlers, <<"_view_cleanup">>, chttpd_db, + handle_view_cleanup_req, 2) end, + fun() -> + assertReturns(db_url_handlers, <<"_compact">>, chttpd_db, + handle_compact_req, 2) + end, + fun() -> + assertReturns(db_url_handlers, <<"_design">>, chttpd_db, + handle_design_req, 2) + end, + fun() -> + assertReturns(db_url_handlers, <<"_temp_view">>, chttpd_view, + handle_temp_view_req, 2) end, + fun() -> + assertReturns(db_url_handlers, <<"_changes">>, chttpd_db, + handle_changes_req, 2) end, + fun() -> + assertReturns(db_url_handlers, <<"_shards">>, mem3_httpd, + handle_shards_req, 2) end, + + % Test the test: when the final target function is missing, the Fun call + % must fail, with any argument, including valid ones. + fun() -> + assertUnmockedFails(db_url_handlers, <<"_view_cleanup">>, chttpd_db, 2) + end, + + % Test the test: when the argument is unknown, the Fun call must fail. + fun() -> + assertUnknownFails(db_url_handlers, <<"_something">>) + end, + + + % design url handler tests + + fun() -> + assertReturns(design_url_handlers, <<"_view">>, chttpd_view, + handle_view_req, 3) end, + fun() -> + assertReturns(design_url_handlers, <<"_show">>, chttpd_show, + handle_doc_show_req, 3) end, + fun() -> + assertReturns(design_url_handlers, <<"_list">>, chttpd_show, + handle_view_list_req, 3) end, + fun() -> + assertReturns(design_url_handlers, <<"_update">>, chttpd_show, + handle_doc_update_req, 3) end, + fun() -> + assertReturns(design_url_handlers, <<"_info">>, chttpd_db, + handle_design_info_req, 3) end, + fun() -> + assertReturns(design_url_handlers, <<"_rewrite">>, chttpd_rewrite, + handle_rewrite_req, 3) end, + + % Test the test: when the final target function is missing, the Fun call + % must fail with any argument, including valid ones. + fun() -> + assertUnmockedFails(design_url_handlers, <<"_view">>, chttpd_view, 3) + end, + % Test the test: when the argument is unknown, the Fun call must fail. + fun() -> + assertUnknownFails(design_url_handlers, <<"_something">>) + end]}. + + +%% Call the dynamic function with a parameter known to trigger a specific +%% clause. Then call the returned fun and get confirmation that this called +%% the right implicit fun, which is mocked, and returns the expected tuple. +assertReturns(Endpoint, M, F) -> + meck:new(M, [passthrough, non_strict]), + try + io:format(user, "~-47...s ", [Endpoint]), + meck:expect(M, F, fun(X) -> {return, Endpoint, X} end), + Fun = chttpd_handler:url_handler(Endpoint), + ?assertEqual({return, Endpoint, x}, Fun(x)) --- End diff -- This is now changed, thanks. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---