summaryrefslogtreecommitdiff
path: root/src/couch_replicator/test/eunit/couch_replicator_proxy_tests.erl
blob: ca1816b33f9e175507a46edc1f81801734b32f19 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
% 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(couch_replicator_proxy_tests).

-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch_replicator/src/couch_replicator.hrl").
-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").

setup() ->
    ok.

teardown(_) ->
    ok.

replicator_proxy_test_() ->
    {
        "replicator proxy tests",
        {
            setup,
            fun() -> test_util:start_couch([couch_replicator]) end,
            fun test_util:stop_couch/1,
            {
                foreach,
                fun setup/0,
                fun teardown/1,
                [
                    fun parse_rep_doc_without_proxy/1,
                    fun parse_rep_doc_with_proxy/1,
                    fun parse_rep_source_target_proxy/1,
                    fun mutually_exclusive_proxy_and_source_proxy/1,
                    fun mutually_exclusive_proxy_and_target_proxy/1
                ]
            }
        }
    }.

parse_rep_doc_without_proxy(_) ->
    ?_test(begin
        NoProxyDoc =
            {[
                {<<"source">>, <<"http://unproxied.com">>},
                {<<"target">>, <<"http://otherunproxied.com">>}
            ]},
        Rep = couch_replicator_docs:parse_rep_doc(NoProxyDoc),
        ?assertEqual((Rep#rep.source)#httpdb.proxy_url, undefined),
        ?assertEqual((Rep#rep.target)#httpdb.proxy_url, undefined)
    end).

parse_rep_doc_with_proxy(_) ->
    ?_test(begin
        ProxyURL = <<"http://myproxy.com">>,
        ProxyDoc =
            {[
                {<<"source">>, <<"http://unproxied.com">>},
                {<<"target">>, <<"http://otherunproxied.com">>},
                {<<"proxy">>, ProxyURL}
            ]},
        Rep = couch_replicator_docs:parse_rep_doc(ProxyDoc),
        ?assertEqual((Rep#rep.source)#httpdb.proxy_url, binary_to_list(ProxyURL)),
        ?assertEqual((Rep#rep.target)#httpdb.proxy_url, binary_to_list(ProxyURL))
    end).

parse_rep_source_target_proxy(_) ->
    ?_test(begin
        SrcProxyURL = <<"http://mysrcproxy.com">>,
        TgtProxyURL = <<"http://mytgtproxy.com:9999">>,
        ProxyDoc =
            {[
                {<<"source">>, <<"http://unproxied.com">>},
                {<<"target">>, <<"http://otherunproxied.com">>},
                {<<"source_proxy">>, SrcProxyURL},
                {<<"target_proxy">>, TgtProxyURL}
            ]},
        Rep = couch_replicator_docs:parse_rep_doc(ProxyDoc),
        ?assertEqual(
            (Rep#rep.source)#httpdb.proxy_url,
            binary_to_list(SrcProxyURL)
        ),
        ?assertEqual(
            (Rep#rep.target)#httpdb.proxy_url,
            binary_to_list(TgtProxyURL)
        )
    end).

mutually_exclusive_proxy_and_source_proxy(_) ->
    ?_test(begin
        ProxyDoc =
            {[
                {<<"source">>, <<"http://unproxied.com">>},
                {<<"target">>, <<"http://otherunproxied.com">>},
                {<<"proxy">>, <<"oldstyleproxy.local">>},
                {<<"source_proxy">>, <<"sourceproxy.local">>}
            ]},
        ?assertThrow(
            {bad_rep_doc, _},
            couch_replicator_docs:parse_rep_doc(ProxyDoc)
        )
    end).

mutually_exclusive_proxy_and_target_proxy(_) ->
    ?_test(begin
        ProxyDoc =
            {[
                {<<"source">>, <<"http://unproxied.com">>},
                {<<"target">>, <<"http://otherunproxied.com">>},
                {<<"proxy">>, <<"oldstyleproxy.local">>},
                {<<"target_proxy">>, <<"targetproxy.local">>}
            ]},
        ?assertThrow(
            {bad_rep_doc, _},
            couch_replicator_docs:parse_rep_doc(ProxyDoc)
        )
    end).