summaryrefslogtreecommitdiff
path: root/site/test.cpp
blob: ab71f836d94b1bfc8607dfd606f8ed3f82eedd4c (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
#define BOOST_TEST_MODULE mirrorsearchsite
#include <boost/test/unit_test.hpp>

#include <testRequest.h>
#include <core.h>
#include <api.h>

#include <Ice/ObjectAdapter.h>

using namespace IceSpider;
using namespace MirrorSearch;

class TestSerice : public Search {
	public:
		virtual SearchServices getServices(const ::Ice::Current& = ::Ice::Current()) override
		{
			return {};
		}
		virtual SearchHits getMatches(const ::std::string & fn, const ::Ice::Current& = ::Ice::Current()) override
		{
			BOOST_TEST_INFO(fn);
			if (fn == "good.txt") {
				return {
					new SearchHit(1, 1, "file:///some/file/path"),
					new SearchHit(1, 1, "file:///some/other/path")
				};
			}
			return {};
		}
		virtual ::IceUtil::Optional<::std::string> feelingLucky(const ::std::string & fn, const ::Ice::Current & c = ::Ice::Current()) override
		{
			auto ms = getMatches(fn, c);
			if (ms.empty()) {
				return IceUtil::None;

			}
			return ms.front()->url;
		}
};

class TestApp : public CoreWithDefaultRouter {
	public:
		TestApp() :
			CoreWithDefaultRouter({
				"--MirrorSearch.Search=Search"
			}),
			adp(communicator->createObjectAdapterWithEndpoints("test", "default"))
		{
			adp->activate();
			adp->add(new TestSerice(), communicator->stringToIdentity("Search"));
		}

		~TestApp()
		{
			adp->deactivate();
			adp->destroy();
		}

		Ice::ObjectAdapterPtr adp;
};

BOOST_FIXTURE_TEST_SUITE(ta, TestApp);

BOOST_AUTO_TEST_CASE( testCallIndexNoFile )
{
	TestRequest requestGetIndex(this, HttpMethod::GET, "/list");
	process(&requestGetIndex);
	auto h = requestGetIndex.getResponseHeaders();
	BOOST_CHECK_EQUAL(h["Status"], "404 Not found");
}

BOOST_AUTO_TEST_CASE( testCallServiceList )
{
	TestRequest requestGetIndex(this, HttpMethod::GET, "/services");
	process(&requestGetIndex);
	auto h = requestGetIndex.getResponseHeaders();
	BOOST_CHECK_EQUAL(h["Status"], "200 OK");
	BOOST_CHECK_EQUAL(h["Content-Type"], "application/json");
}

BOOST_AUTO_TEST_CASE( testCallIndex )
{
	TestRequest requestGetIndex(this, HttpMethod::GET, "/list/somefile.txt");
	process(&requestGetIndex);
	auto h = requestGetIndex.getResponseHeaders();
	BOOST_CHECK_EQUAL(h["Status"], "200 OK");
	BOOST_CHECK_EQUAL(h["Content-Type"], "application/json");
}

BOOST_AUTO_TEST_CASE( testCallDownloadNotFile )
{
	TestRequest requestGetIndex(this, HttpMethod::GET, "/download");
	process(&requestGetIndex);
	auto h = requestGetIndex.getResponseHeaders();
	BOOST_CHECK_EQUAL(h["Status"], "404 Not found");
}

BOOST_AUTO_TEST_CASE( testCallDownloadBad )
{
	TestRequest requestGetIndex(this, HttpMethod::GET, "/download/bad.txt");
	process(&requestGetIndex);
	auto h = requestGetIndex.getResponseHeaders();
	BOOST_CHECK_EQUAL(h["Status"], "404 No mirror found");
}

BOOST_AUTO_TEST_CASE( testCallDownloadGood )
{
	TestRequest requestGetIndex(this, HttpMethod::GET, "/download/good.txt");
	process(&requestGetIndex);
	auto h = requestGetIndex.getResponseHeaders();
	BOOST_CHECK_EQUAL(h["Status"], "303 Mirror found");
	BOOST_CHECK_EQUAL(h["Location"], "file:///some/file/path");
}

BOOST_AUTO_TEST_SUITE_END();