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(),
const std::string &method = std::string(),
const std::string &body = std::string()) {
UNUSED_ARG(url);
UNUSED_ARG(headers);
UNUSED_ARG(method);
UNUSED_ARG(body);
responseStream << "HTTPS request succeeded";
return 200;
}
virtual int
sendHTTPRequest(const std::string &url, std::ostream &responseStream,
const StringToStringMap &headers = StringToStringMap(),
const std::string &method = std::string(),
const std::string &body = std::string()) {
UNUSED_ARG(url);
UNUSED_ARG(headers);
UNUSED_ARG(method);
UNUSED_ARG(body);
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;
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";
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());
TS_ASSERT_EQUALS(200, response);
TSM_ASSERT("File has not been created.", tmpFile.exists());
TSM_ASSERT("File is not a file.", tmpFile.isFile());
TS_ASSERT_THROWS_NOTHING(fs.open(tmpFile.path().c_str(), std::fstream::in));
TSM_ASSERT("Cannot open file.", fs.is_open());
ss << fs.rdbuf(); // read the file
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());
TS_ASSERT_EQUALS(200, response);
TSM_ASSERT("File has not been created.", tmpFile.exists());
TSM_ASSERT("File is not a file.", tmpFile.isFile());
TS_ASSERT_THROWS_NOTHING(fs.open(tmpFile.path().c_str(), std::fstream::in));
TSM_ASSERT("Cannot open file.", fs.is_open());
ss << fs.rdbuf(); // read the file
TS_ASSERT_EQUALS("HTTPS request succeeded", ss.str());
#endif /* MANTID_KERNEL_INTERNETSERVICETEST_H_ */