Newer
Older
#ifndef MANTID_KERNEL_INTERNETSERVICETEST_H_
#define MANTID_KERNEL_INTERNETSERVICETEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/InternetHelper.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/NetworkProxy.h"
#include "MantidKernel/ProxyInfo.h"
#include <Poco/TemporaryFile.h>
#include <fstream>
#include <sstream>
using Mantid::Kernel::InternetHelper;
using namespace Mantid::Kernel;
namespace
{
/**
* Mock out the internet calls of this algorithm
*/
class MockedInternetHelper : public InternetHelper
{
protected:
virtual int sendHTTPSRequest(const std::string& url,
std::ostream& responseStream,
const StringToStringMap& headers = StringToStringMap())
{
responseStream << "HTTPS request succeeded";
return 200;
}
virtual int sendHTTPRequest(const std::string& url,
std::ostream& responseStream,
const StringToStringMap& headers = StringToStringMap())
{
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
responseStream << "HTTP request succeeded";
return 200;
}
};
}
class InternetHelperTest : 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 InternetHelperTest *createSuite() { return new InternetHelperTest(); }
static void destroySuite( InternetHelperTest *suite ) { delete suite; }
void test_sendRequest_HTTP()
{
MockedInternetHelper internetHelper;
std::string url = "http://www.google.com";
std::stringstream ss;
int response;
TS_ASSERT_THROWS_NOTHING (response = internetHelper.sendRequest(url, ss);)
TS_ASSERT_EQUALS (200, response);
TS_ASSERT_EQUALS ("HTTP request succeeded", ss.str());
}
void test_sendRequest_HTTPS()
{
MockedInternetHelper internetHelper;
std::string httpsUrl = "https://api.github.com/repos/mantidproject/mantid/contents";
std::stringstream ss;
int response;
TS_ASSERT_THROWS_NOTHING (response = internetHelper.sendRequest(httpsUrl, ss);)
TS_ASSERT_EQUALS (200, response);
TS_ASSERT_EQUALS ("HTTPS request succeeded", ss.str());
}
void test_DownloadFile_HTTP()
{
MockedInternetHelper internetHelper;
std::string url = "http://www.google.com";
Poco::TemporaryFile tmpFile;
int response = internetHelper.downloadFile(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(), 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;
int response = internetHelper.downloadFile(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(), 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_KERNEL_INTERNETSERVICETEST_H_ */