summaryrefslogtreecommitdiff
path: root/src/chttpd/test/chttpd_handlers_tests.erl
blob: f3e8f5dcd7d1c520a56aa0d2fba0574012e2cdd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
            UrlBin = ?l2b(Url),
            Request = couch_util:json_encode({[
                {<<"source">>, <<UrlBin/binary, "/foo%2Fbar">>},
                {<<"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">>]),
            Target = json_value(JSON, [<<"target">>, <<"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.