summaryrefslogtreecommitdiff
path: root/libadhocutil/unittests/testCurl.cpp
blob: 41ee0a1d54ab746ca0cf2e2eed2ea6947ec216aa (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
#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, (void*)&discard);
	ch.perform();
}

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);
	boost::iostreams::stream<css_ref> curlstrm(boost::ref(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);
		boost::iostreams::stream<css_ref> curlstrm(boost::ref(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"]);
}