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
124
125
126
127
128
129
130
131
132
133
134
|
#!/usr/bin/env escript
%% -*- erlang -*-
% 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.
%% XXX: Figure out how to -include("couch_rep.hrl")
-record(http_db, {
url,
auth = [],
resource = "",
headers = [
{"User-Agent", "CouchDb/"++couch_server:get_version()},
{"Accept", "application/json"},
{"Accept-Encoding", "gzip"}
],
qs = [],
method = get,
body = nil,
options = [
{response_format,binary},
{inactivity_timeout, 30000}
],
retries = 10,
pause = 1,
conn = nil
}).
-define(SERVER, "http://127.0.0.1:5984/").
-define(DBNAME, "etap-test-db").
main(_) ->
code:add_pathz("src/couchdb"),
code:add_pathz("src/ibrowse"),
code:add_pathz("src/mochiweb"),
code:add_pathz("src/erlang-oauth"),
etap:plan(7),
case (catch test()) of
ok ->
etap:end_tests();
Other ->
etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
etap:bail(Other)
end,
ok.
test() ->
couch_server:start(
["etc/couchdb/default_dev.ini", "etc/couchdb/local_dev.ini"]
),
ibrowse:start(),
crypto:start(),
couch_server:delete(list_to_binary(?DBNAME), []),
{ok, Db} = couch_db:create(list_to_binary(?DBNAME), []),
test_welcome(),
test_binary_url(),
test_put(),
test_qs(),
test_db_exists(),
couch_db:close(Db),
couch_server:delete(list_to_binary(?DBNAME), []),
ok.
test_welcome() ->
WelcomeReq = #http_db{url=?SERVER},
Expect = {[
{<<"couchdb">>, <<"Welcome">>},
{<<"version">>, list_to_binary(couch_server:get_version())}
]},
etap:is(
couch_rep_httpc:request(WelcomeReq),
Expect,
"welcome request with url-as-list"
).
test_binary_url() ->
Req = #http_db{url=list_to_binary(?SERVER)},
Expect = {[
{<<"couchdb">>, <<"Welcome">>},
{<<"version">>, list_to_binary(couch_server:get_version())}
]},
Fun = fun(Expect) -> true; (_) -> false end,
etap:is(
couch_rep_httpc:request(Req),
Expect,
"welcome request with url-as-binary"
).
test_put() ->
Req = #http_db{
url = ?SERVER ++ ?DBNAME ++ "/",
resource = "test_put",
body = {[{<<"foo">>, <<"bar">>}]},
method = put
},
{Resp} = couch_rep_httpc:request(Req),
etap:ok(proplists:get_value(<<"ok">>, Resp), "ok:true on upload"),
etap:is(<<"test_put">>, proplists:get_value(<<"id">>, Resp), "id is correct").
test_qs() ->
Req = #http_db{
url = ?SERVER ++ ?DBNAME ++ "/",
resource = "foo",
qs = [
{bar, true},
{baz, 1.03},
{bif, mochijson2:encode(<<"1-23456">>)}
]
},
Expect = ?SERVER ++ ?DBNAME ++ "/foo?bar=true&baz=1.03&bif=\"1-23456\"",
etap:is(
couch_rep_httpc:full_url(Req),
Expect,
"query-string proplist encoding ok"
).
test_db_exists() ->
Req1 = #http_db{url=?SERVER ++ ?DBNAME ++ "/"},
Req2 = #http_db{url=?SERVER ++ ?DBNAME ++ "_foo/"},
etap:ok(couch_rep_httpc:db_exists(Req1), "db_exists true check"),
etap:is(couch_rep_httpc:db_exists(Req2), false, "db_exists false check").
|