/
DownloadFileTest.h
146 lines (120 loc) · 4.01 KB
/
DownloadFileTest.h
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
#ifndef MANTID_DATAHANDLING_DOWNLOADFILETEST_H_
#define MANTID_DATAHANDLING_DOWNLOADFILETEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/DownloadFile.h"
#include "MantidKernel/InternetHelper.h"
#include <Poco/TemporaryFile.h>
#include <fstream>
#include <sstream>
using Mantid::Kernel::InternetHelper;
using Mantid::DataHandling::DownloadFile;
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace
{
/**
* Mock out the internet calls of this helper
*/
class MockedInternetHelper : public InternetHelper
{
protected:
virtual int sendHTTPSRequest(const std::string& url,
std::ostream& responseStream,
const StringToStringMap& headers = StringToStringMap())
{
UNUSED_ARG(url);
UNUSED_ARG(headers);
responseStream << "HTTPS request succeeded";
return 200;
}
virtual int sendHTTPRequest(const std::string& url,
std::ostream& responseStream,
const StringToStringMap& headers = StringToStringMap())
{
UNUSED_ARG(url);
UNUSED_ARG(headers);
responseStream << "HTTP request succeeded";
return 200;
}
};
/**
* Mock out the internet calls of this algorithm
*/
class MockedDownloadFile : public DownloadFile
{
public:
MockedDownloadFile()
{
delete m_internetHelper;
m_internetHelper = new MockedInternetHelper();
}
};
}
class DownloadFileTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static DownloadFileTest *createSuite() { return new DownloadFileTest(); }
static void destroySuite( DownloadFileTest *suite ) { delete suite; }
void test_Init()
{
DownloadFile alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_Bad_Address()
{
MockedInternetHelper internetHelper;
std::string url = "www.google.com";
Poco::TemporaryFile tmpFile;
exec_alg(url,tmpFile.path(), "http://" + url);
}
void exec_alg(std::string address, std::string filename, std::string newAddress = "")
{
MockedDownloadFile alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Address", address) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
if (newAddress.size() > 0)
{
TS_ASSERT_EQUALS(newAddress, alg.getPropertyValue("Address") );
}
}
void test_DownloadFile_HTTP()
{
MockedInternetHelper internetHelper;
std::string url = "http://www.google.com";
Poco::TemporaryFile tmpFile;
exec_alg(url,tmpFile.path());
TSM_ASSERT("File has not been created.",tmpFile.exists());
TSM_ASSERT("File is not a file.",tmpFile.isFile());
std::fstream fs;
TS_ASSERT_THROWS_NOTHING (fs.open (tmpFile.path().c_str(), std::fstream::in ));
TSM_ASSERT("Cannot open file.",fs.is_open());
std::stringstream ss;
ss << fs.rdbuf();//read the file
fs.close();
TS_ASSERT_EQUALS ("HTTP request succeeded", ss.str());
}
void test_DownloadFile_HTTPS()
{
MockedInternetHelper internetHelper;
std::string httpsUrl = "https://api.github.com/repos/mantidproject/mantid/contents";
Poco::TemporaryFile tmpFile;
exec_alg(httpsUrl,tmpFile.path());
TSM_ASSERT("File has not been created.",tmpFile.exists());
TSM_ASSERT("File is not a file.",tmpFile.isFile());
std::fstream fs;
TS_ASSERT_THROWS_NOTHING (fs.open (tmpFile.path().c_str(), std::fstream::in ));
TSM_ASSERT("Cannot open file.",fs.is_open());
std::stringstream ss;
ss << fs.rdbuf();//read the file
fs.close();
TS_ASSERT_EQUALS ("HTTPS request succeeded", ss.str());
}
};
#endif /* MANTID_DATAHANDLING_DOWNLOADFILETEST_H_ */