couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [2/3] chttpd commit: updated refs/heads/master to 7a2bd8f
Date Thu, 22 Oct 2015 18:52:31 GMT
Escape database name on POST /_replicate

COUCHDB-2666


Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/895715a6
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/895715a6
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/895715a6

Branch: refs/heads/master
Commit: 895715a6adf317a7b2f25dc54b0ce7eb1191c8ff
Parents: bf604a5
Author: Alexander Shorin <kxepal@apache.org>
Authored: Tue Oct 20 21:13:39 2015 +0300
Committer: Alexander Shorin <kxepal@apache.org>
Committed: Wed Oct 21 12:56:26 2015 +0300

----------------------------------------------------------------------
 src/chttpd.erl                 |  4 +-
 test/chttpd_handlers_tests.erl | 87 +++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/895715a6/src/chttpd.erl
----------------------------------------------------------------------
diff --git a/src/chttpd.erl b/src/chttpd.erl
index b861ea9..4f2183e 100644
--- a/src/chttpd.erl
+++ b/src/chttpd.erl
@@ -381,7 +381,7 @@ fix_uri(Req, Props, Type) ->
         true ->
             Props;
         false ->
-            Uri = make_uri(Req,replication_uri(Type, Props)),
+            Uri = make_uri(Req, Uri0),
             [{Type,Uri}|proplists:delete(Type,Props)]
         end
     end.
@@ -391,7 +391,7 @@ replication_uri(Type, PostProps) ->
     {Props} ->
         couch_util:get_value(<<"url">>, Props);
     Else ->
-        Else
+        quote(Else)
     end.
 
 is_http(<<"http://", _/binary>>) ->

http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/895715a6/test/chttpd_handlers_tests.erl
----------------------------------------------------------------------
diff --git a/test/chttpd_handlers_tests.erl b/test/chttpd_handlers_tests.erl
new file mode 100644
index 0000000..05a21b7
--- /dev/null
+++ b/test/chttpd_handlers_tests.erl
@@ -0,0 +1,87 @@
+% 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_handlers_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+
+setup() ->
+    Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
+    Port = mochiweb_socket_server:get(chttpd, port),
+    BaseUrl = lists:concat(["http://", Addr, ":", Port]),
+    BaseUrl.
+
+teardown(_Url) ->
+    ok.
+
+
+replicate_test_() ->
+    {
+        "_replicate",
+        {
+            setup,
+            fun chttpd_test_util:start_couch/0,
+            fun chttpd_test_util:stop_couch/1,
+            {
+                foreach,
+                fun setup/0, fun teardown/1,
+                [
+                    fun should_escape_dbname_on_replicate/1
+                ]
+            }
+        }
+    }.
+
+
+should_escape_dbname_on_replicate(Url) ->
+    ?_test(
+        begin
+            Request = couch_util:json_encode({[
+                {<<"source">>, <<"foo/bar">>},
+                {<<"target">>, <<"bar/baz">>},
+                {<<"create_target">>, true}
+            ]}),
+            {ok, 200, _, Body} = request_replicate(Url ++ "/_replicate", Request),
+            JSON = couch_util:json_decode(Body),
+
+            Source = json_value(JSON, [<<"source">>, <<"url">>]),
+            Target = json_value(JSON, [<<"target">>, <<"url">>]),
+            UrlBin = ?l2b(Url),
+            ?assertEqual(<<UrlBin/binary, "/foo%2Fbar">>, Source),
+            ?assertEqual(<<UrlBin/binary, "/bar%2Fbaz">>, Target)
+        end).
+
+
+json_value(JSON, Keys) ->
+    couch_util:get_nested_json_value(JSON, Keys).
+
+request_replicate(Url, Body) ->
+    Headers = [{"Content-Type", "application/json"}],
+    Handler = {chttpd_misc, handle_replicate_req},
+    request(post, Url, Headers, Body, Handler, fun(Req) ->
+        chttpd:send_json(Req, 200, get(post_body))
+    end).
+
+request(Method, Url, Headers, Body, {M, F}, MockFun) ->
+    meck:new(M, [passthrough, non_strict]),
+    try
+        meck:expect(M, F, MockFun),
+        Result = test_request:Method(Url, Headers, Body),
+        ?assert(meck:validate(M)),
+        Result
+    catch Kind:Reason ->
+        {Kind, Reason}
+    after
+        meck:unload(M)
+    end.


Mime
View raw message