summaryrefslogtreecommitdiff
path: root/libadhocutil/unittests/testCurl.cpp
blob: 5365f583c62fa258de039b6153aceb2aeaa57a3c (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
124
125
126
127
128
129
130
131
#define BOOST_TEST_MODULE Curl
#include <boost/test/unit_test.hpp>

#include <boost/bind.hpp>
#include "curlHandle.h"
#include "curlMultiHandle.h"
#include "curlStream.h"
#include "definedDirs.h"
#include "net.h"

size_t discard(void *, size_t sz, size_t nm, void *)
{
	return sz * nm;
}

BOOST_AUTO_TEST_CASE( fetch_file )
{
	auto url = "file://" + rootDir.string() + "/testCurl.cpp";
	CurlHandle ch(url);
	ch.setopt(CURLOPT_WRITEFUNCTION, discard);
	ch.perform();
}

BOOST_AUTO_TEST_CASE( setAndGetOptions )
{
	auto url = "file://" + rootDir.string() + "/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(totalTime > 0);
	BOOST_REQUIRE(totalTime < 50);
}

BOOST_AUTO_TEST_CASE( fetch_missing )
{
	auto url = "file://" + rootDir.string() + "/nothere";
	CurlHandle ch(url);
	BOOST_REQUIRE_THROW(ch.perform(), AdHoc::Net::CurlException);
}

BOOST_AUTO_TEST_CASE( fetch_file_stream )
{
	auto url = "file://" + rootDir.string() + "/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 = "file://" + rootDir.string() + "/nothere";
	BOOST_REQUIRE_THROW({
		CurlStreamSource css(url);
		CurlStream curlstrm(css);
		std::string tok;
		curlstrm >> tok;
	}, AdHoc::Net::CurlException);
}

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 )
{
	CurlMultiHandle cmh;
	std::map<std::string, std::string> files;
	cmh.addCurl("file://" + rootDir.string() + "/testBuffer.cpp",
			boost::bind(&mapFileToName, boost::ref(files), "testBuffer.cpp", _1));
	cmh.addCurl("file://" + rootDir.string() + "/testCurl.cpp",
			boost::bind(&mapFileToName, boost::ref(files), "testCurl.cpp", _1));
	cmh.addCurl("file://" + rootDir.string() + "/testLocks.cpp",
			boost::bind(&mapFileToName, boost::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("http://sys.randomdan.homeip.net/missing", [&finished, &errored](std::istream & s) {
			try {
				std::string tok;
				while (!s.eof()) {
					s >> tok;
				}
				finished = true;
			} catch (...) {
				errored = true;
			}
		});
	cmh.performAll();
	BOOST_REQUIRE(!finished);
	BOOST_REQUIRE(errored);
}