You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2021/03/09 04:06:08 UTC

[GitHub] [couchdb] eiri opened a new pull request #3409: Use stored peer when available in json_req_obj

eiri opened a new pull request #3409:
URL: https://github.com/apache/couchdb/pull/3409


   ## Overview
   
   When function `chttpd_external:json_req_obj/2` acquiring `peer` it's always trying to read it from a socket, which doesn't work on a foreign node, because socket is unavailable there. We, however, read `peer` at request time in `chttpd:handle_request_int/1` and store it in `#httpd` record.
   
   This change makes advantage of that and changes `chttpd_external:json_req_obj/2` to read `peer` from the stored record first and fail back to the socket reading only when it's not available there.
   
   ## Testing recommendations
   
   There was no tests for `chttpd_external` module, so I've added a new test suite `chttpd_external_test` that covers  the changes. To run an isolated run execute: `make eunit apps=chttpd suites=chttpd_external_test`
   
   ## Checklist
   
   - [x] Code is written and works correctly
   - [x] Changes are covered by tests
   - [ ] Any new configurable parameters are documented in `rel/overlay/etc/default.ini`
   - [ ] A PR for documentation changes has been made in https://github.com/apache/couchdb-documentation
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] eiri commented on a change in pull request #3409: Use stored peer when available in json_req_obj

Posted by GitBox <gi...@apache.org>.
eiri commented on a change in pull request #3409:
URL: https://github.com/apache/couchdb/pull/3409#discussion_r590350288



##########
File path: src/chttpd/test/eunit/chttpd_external_test.erl
##########
@@ -0,0 +1,119 @@
+% 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(chttpd_external_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+
+setup_mock() ->
+    ok = meck:new([config, couch], [passthrough]),
+    ok = meck:expect(couch_db, is_clustered, 1, false),
+    ok = meck:expect(couch_db, get_db_info, 1, {ok, [{name, <<"fake">>}]}),
+    ok = meck:expect(couch_db, name, 1, <<"fake">>),
+    ok = meck:expect(couch_db, get_user_ctx, 1, #user_ctx{}),
+    ok = meck:expect(couch_db, get_security, 1, []),
+    ok = meck:expect(couch_uuids, new, 0, <<"4">>),
+    ok = meck:expect(config, get_integer, fun(_, _, N) -> N end).
+
+teardown_mock(_) ->
+    meck:unload().
+
+setup_local_httpd_req() ->
+    ok = meck:new(mochiweb, [passthrough]),
+    ok = meck:expect(mochiweb_socket, peername, fun(_) ->
+        {ok, {{127, 0, 0, 1}, 5984}}
+    end),
+    ok = meck:expect(mochiweb_request, recv_body, 2, {[{<<"a">>, 42}]}),
+    Headers = mochiweb_headers:make([{"host", "example.com"}]),
+    MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers),
+    #httpd{
+        mochi_req = MochiReq,
+        method = 'GET',
+        path_parts = [<<"/">>],
+        requested_path_parts = [<<"/">>],
+        user_ctx = #user_ctx{}
+    }.
+
+setup_remote_httpd_req() ->
+    Headers = mochiweb_headers:make([{"host", "example.com"}]),
+    MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers),
+    #httpd{
+        mochi_req = MochiReq,
+        method = 'GET',
+        path_parts = [<<"/">>],
+        requested_path_parts = [<<"/">>],
+        peer = "127.0.0.1",
+        req_body = {[{<<"a">>, 42}]},
+        user_ctx = #user_ctx{}
+    }.
+
+json_req_obj_local_httpd_req_test_() ->
+    {
+        "chttpd external local httpd_req tests",
+        {
+            setup,
+            fun setup_mock/0,
+            fun teardown_mock/1,
+            {
+                setup,
+                fun setup_local_httpd_req/0,
+                fun should_convert_req_to_json_obj/1

Review comment:
       This is not even a trick, this is official supported form for [eunit fixtures](http://erlang.org/doc/apps/eunit/chapter.html#fixtures) 😄
   
   Basically
   ```
   {setup, Setup, Tests | Instantiator}
   {setup, Setup, Cleanup, Tests | Instantiator}
   {setup, Where, Setup, Tests | Instantiator}
   {setup, Where, Setup, Cleanup, Tests | Instantiator}
   ```
   
   All of those are supported terms for the tests representations and eunit runner figures out what's what by a number of the elements in a representation tuple.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] jaydoane commented on a change in pull request #3409: Use stored peer when available in json_req_obj

Posted by GitBox <gi...@apache.org>.
jaydoane commented on a change in pull request #3409:
URL: https://github.com/apache/couchdb/pull/3409#discussion_r589959983



##########
File path: src/chttpd/test/eunit/chttpd_external_test.erl
##########
@@ -0,0 +1,119 @@
+% 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(chttpd_external_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+
+setup_mock() ->
+    ok = meck:new([config, couch], [passthrough]),
+    ok = meck:expect(couch_db, is_clustered, 1, false),
+    ok = meck:expect(couch_db, get_db_info, 1, {ok, [{name, <<"fake">>}]}),
+    ok = meck:expect(couch_db, name, 1, <<"fake">>),
+    ok = meck:expect(couch_db, get_user_ctx, 1, #user_ctx{}),
+    ok = meck:expect(couch_db, get_security, 1, []),
+    ok = meck:expect(couch_uuids, new, 0, <<"4">>),
+    ok = meck:expect(config, get_integer, fun(_, _, N) -> N end).
+
+teardown_mock(_) ->
+    meck:unload().
+
+setup_local_httpd_req() ->
+    ok = meck:new(mochiweb, [passthrough]),
+    ok = meck:expect(mochiweb_socket, peername, fun(_) ->
+        {ok, {{127, 0, 0, 1}, 5984}}
+    end),
+    ok = meck:expect(mochiweb_request, recv_body, 2, {[{<<"a">>, 42}]}),
+    Headers = mochiweb_headers:make([{"host", "example.com"}]),
+    MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers),
+    #httpd{
+        mochi_req = MochiReq,
+        method = 'GET',
+        path_parts = [<<"/">>],
+        requested_path_parts = [<<"/">>],
+        user_ctx = #user_ctx{}
+    }.
+
+setup_remote_httpd_req() ->
+    Headers = mochiweb_headers:make([{"host", "example.com"}]),
+    MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers),
+    #httpd{
+        mochi_req = MochiReq,
+        method = 'GET',
+        path_parts = [<<"/">>],
+        requested_path_parts = [<<"/">>],
+        peer = "127.0.0.1",
+        req_body = {[{<<"a">>, 42}]},
+        user_ctx = #user_ctx{}
+    }.
+
+json_req_obj_local_httpd_req_test_() ->
+    {
+        "chttpd external local httpd_req tests",
+        {
+            setup,
+            fun setup_mock/0,
+            fun teardown_mock/1,
+            {
+                setup,
+                fun setup_local_httpd_req/0,
+                fun should_convert_req_to_json_obj/1

Review comment:
       I don't think I've seen this pattern before, and it took me a couple moments to figure out that the tests are effectively in place of a normal teardown function? It's a neat trick!




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] eiri merged pull request #3409: Use stored peer when available in json_req_obj

Posted by GitBox <gi...@apache.org>.
eiri merged pull request #3409:
URL: https://github.com/apache/couchdb/pull/3409


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] jaydoane commented on a change in pull request #3409: Use stored peer when available in json_req_obj

Posted by GitBox <gi...@apache.org>.
jaydoane commented on a change in pull request #3409:
URL: https://github.com/apache/couchdb/pull/3409#discussion_r590623653



##########
File path: src/chttpd/test/eunit/chttpd_external_test.erl
##########
@@ -0,0 +1,119 @@
+% 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(chttpd_external_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+
+setup_mock() ->
+    ok = meck:new([config, couch], [passthrough]),
+    ok = meck:expect(couch_db, is_clustered, 1, false),
+    ok = meck:expect(couch_db, get_db_info, 1, {ok, [{name, <<"fake">>}]}),
+    ok = meck:expect(couch_db, name, 1, <<"fake">>),
+    ok = meck:expect(couch_db, get_user_ctx, 1, #user_ctx{}),
+    ok = meck:expect(couch_db, get_security, 1, []),
+    ok = meck:expect(couch_uuids, new, 0, <<"4">>),
+    ok = meck:expect(config, get_integer, fun(_, _, N) -> N end).
+
+teardown_mock(_) ->
+    meck:unload().
+
+setup_local_httpd_req() ->
+    ok = meck:new(mochiweb, [passthrough]),
+    ok = meck:expect(mochiweb_socket, peername, fun(_) ->
+        {ok, {{127, 0, 0, 1}, 5984}}
+    end),
+    ok = meck:expect(mochiweb_request, recv_body, 2, {[{<<"a">>, 42}]}),
+    Headers = mochiweb_headers:make([{"host", "example.com"}]),
+    MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers),
+    #httpd{
+        mochi_req = MochiReq,
+        method = 'GET',
+        path_parts = [<<"/">>],
+        requested_path_parts = [<<"/">>],
+        user_ctx = #user_ctx{}
+    }.
+
+setup_remote_httpd_req() ->
+    Headers = mochiweb_headers:make([{"host", "example.com"}]),
+    MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers),
+    #httpd{
+        mochi_req = MochiReq,
+        method = 'GET',
+        path_parts = [<<"/">>],
+        requested_path_parts = [<<"/">>],
+        peer = "127.0.0.1",
+        req_body = {[{<<"a">>, 42}]},
+        user_ctx = #user_ctx{}
+    }.
+
+json_req_obj_local_httpd_req_test_() ->
+    {
+        "chttpd external local httpd_req tests",
+        {
+            setup,
+            fun setup_mock/0,
+            fun teardown_mock/1,
+            {
+                setup,
+                fun setup_local_httpd_req/0,
+                fun should_convert_req_to_json_obj/1

Review comment:
       Thanks, it's slick and expressive!




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org