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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#define BOOST_TEST_MODULE Curl
#include <boost/test/unit_test.hpp>
#include <functional>
#include "curlHandle.h"
#include "curlMultiHandle.h"
#include "curlStream.h"
#include "compileTimeFormatter.h"
#include "definedDirs.h"
#include "net.h"
#include <boost/algorithm/string/predicate.hpp>
using namespace AdHoc::Net;
static
size_t
discard(void *, size_t sz, size_t nm, void *)
{
return sz * nm;
}
AdHocFormatter(FileUrl, "file://%?/%?");
const auto urlGen = std::bind((std::string(*)(const std::string &, const std::string_view &))&FileUrl::get, rootDir, std::placeholders::_1);
BOOST_AUTO_TEST_CASE( fetch_file )
{
auto url = urlGen("testCurl.cpp");
BOOST_TEST_CONTEXT(url) {
CurlHandle ch(url);
ch.setopt(CURLOPT_WRITEFUNCTION, discard);
ch.perform();
}
}
BOOST_AUTO_TEST_CASE( setAndGetOptions )
{
auto url = urlGen("testCurl.cpp");
CurlHandle ch(url);
// function
ch.setopt(CURLOPT_WRITEFUNCTION, discard);
// object
ch.setopt(CURLOPT_WRITEDATA, this);
// int
ch.setopt(CURLOPT_LOCALPORT, 8000);
// long
ch.setopt(CURLOPT_LOCALPORT, 8000L);
ch.perform();
// char *
char * eurl;
ch.getinfo(CURLINFO_EFFECTIVE_URL, eurl);
BOOST_REQUIRE_EQUAL(url, eurl);
// double
double totalTime;
ch.getinfo(CURLINFO_TOTAL_TIME, totalTime);
BOOST_REQUIRE_GT(totalTime, 0);
BOOST_REQUIRE_LT(totalTime, 50);
}
BOOST_AUTO_TEST_CASE( fetch_missing )
{
auto url = urlGen("nothere");
CurlHandle ch(url);
BOOST_REQUIRE_THROW(ch.perform(), AdHoc::Net::CurlException);
}
BOOST_AUTO_TEST_CASE( fetch_http_stream )
{
CurlStreamSource css("https://sys.randomdan.homeip.net/env.cgi");
css.appendHeader("X-POWERED-BY: mature-cheddar");
CurlStream curlstrm(css);
std::string tok;
int expected = 0;
while (!curlstrm.eof()) {
curlstrm >> tok;
if (boost::algorithm::starts_with(tok, "HTTP_X_POWERED_BY=")) {
expected += 1;
BOOST_REQUIRE_EQUAL("HTTP_X_POWERED_BY=mature-cheddar", tok);
}
}
BOOST_REQUIRE_EQUAL(1, expected);
}
BOOST_AUTO_TEST_CASE( fetch_file_stream )
{
auto url = urlGen("testCurl.cpp");
CurlStreamSource css(url);
CurlStream curlstrm(css);
std::string tok;
curlstrm >> tok;
BOOST_REQUIRE_EQUAL("#define", tok);
curlstrm >> tok;
BOOST_REQUIRE_EQUAL("BOOST_TEST_MODULE", tok);
curlstrm >> tok;
BOOST_REQUIRE_EQUAL("Curl", tok);
while (!curlstrm.eof()) {
curlstrm >> tok;
}
}
BOOST_AUTO_TEST_CASE( fetch_missing_stream )
{
auto url = urlGen("nothere");
CurlStreamSource css(url);
css.setopt(CURLOPT_FAILONERROR, 1L);
CurlStream curlstrm(css);
std::string tok;
curlstrm >> tok;
BOOST_REQUIRE(!curlstrm.good());
}
static
void
mapFileToName(std::map<std::string, std::string> & map, const std::string & file, std::istream & curlstrm)
{
std::string tok;
curlstrm >> tok; // #define
curlstrm >> tok; // BOOST_TEST_MODULE
curlstrm >> tok; // name :)
map[file] = tok;
}
BOOST_AUTO_TEST_CASE( fetch_multi )
{
using std::placeholders::_1;
CurlMultiHandle cmh;
std::map<std::string, std::string> files;
cmh.addCurl(urlGen("/testBuffer.cpp"),
std::bind(&mapFileToName, std::ref(files), "testBuffer.cpp", _1));
cmh.addCurl(urlGen("/testCurl.cpp"),
std::bind(&mapFileToName, std::ref(files), "testCurl.cpp", _1));
cmh.addCurl(urlGen("/testLocks.cpp"),
std::bind(&mapFileToName, std::ref(files), "testLocks.cpp", _1));
cmh.performAll();
BOOST_REQUIRE_EQUAL(3, files.size());
BOOST_REQUIRE_EQUAL("Locks", files["testLocks.cpp"]);
BOOST_REQUIRE_EQUAL("Buffer", files["testBuffer.cpp"]);
BOOST_REQUIRE_EQUAL("Curl", files["testCurl.cpp"]);
}
BOOST_AUTO_TEST_CASE( fetch_multi_fail )
{
CurlMultiHandle cmh;
bool errored = false;
bool finished = false;
cmh.addCurl("https://sys.randomdan.homeip.net/missing", [&finished, &errored](std::istream & s) {
std::string tok;
while (!s.eof()) {
if (!s.good()) {
errored = true;
return;
}
s >> tok;
}
finished = true;
});
cmh.performAll();
BOOST_REQUIRE(!finished);
BOOST_REQUIRE(errored);
}
|