mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-24 23:30:03 +02:00
enabled tests
code cleaned added exceptions git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@801 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
parent
87407b5529
commit
7dccc4fc1a
@ -16,7 +16,7 @@
|
|||||||
// HTTP timeout in seconds, default is 8
|
// HTTP timeout in seconds, default is 8
|
||||||
#define HTTP_TIMEOUT 10
|
#define HTTP_TIMEOUT 10
|
||||||
// Number of connection tries
|
// Number of connection tries
|
||||||
#define N_CONNECTION_TRIES 0
|
#define N_CONNECTION_TRIES 3
|
||||||
|
|
||||||
using namespace Poco::Net;
|
using namespace Poco::Net;
|
||||||
using namespace Poco;
|
using namespace Poco;
|
||||||
@ -25,91 +25,72 @@ using namespace std;
|
|||||||
class RestHelper {
|
class RestHelper {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
~RestHelper(){};
|
||||||
|
|
||||||
void init(string hostname, int port){
|
void init(string hostname, int port){
|
||||||
/*
|
/*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
full_hostname = "http://"+hostname;
|
full_hostname = "http://"+hostname;
|
||||||
cout << full_hostname << endl;
|
|
||||||
session = new HTTPClientSession(hostname,port );
|
session = new HTTPClientSession(hostname,port );
|
||||||
session->setKeepAliveTimeout( Timespan( HTTP_TIMEOUT,0) );
|
session->setKeepAliveTimeout( Timespan( HTTP_TIMEOUT,0) );
|
||||||
};
|
};
|
||||||
|
|
||||||
int get_json(string request, string* answer){
|
int get_json(string request, string* answer){
|
||||||
//TODO: implement a timeout and max_retries
|
/*
|
||||||
|
|
||||||
uri = new URI(full_hostname+"/"+request);
|
*/
|
||||||
string path(uri->getPathAndQuery());
|
|
||||||
if (path.empty()) path = "/";
|
|
||||||
|
|
||||||
// send request
|
|
||||||
//HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
|
||||||
req = new HTTPRequest(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
|
||||||
|
|
||||||
try {
|
|
||||||
session->sendRequest(*req);
|
|
||||||
}
|
|
||||||
catch (exception& e){
|
|
||||||
cout << "Exception:"<< e.what() << '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
int code = send_request(session, req);
|
|
||||||
|
|
||||||
// get response
|
|
||||||
//if (code!=0){
|
|
||||||
HTTPResponse res;
|
|
||||||
cout << res.getStatus() << " " << res.getReason() << endl;
|
|
||||||
istream &is = session->receiveResponse(res);
|
|
||||||
StreamCopier::copyToString(is, *answer);
|
|
||||||
|
|
||||||
return res.getStatus();
|
|
||||||
//}
|
|
||||||
//else
|
|
||||||
//return code;
|
|
||||||
};
|
|
||||||
|
|
||||||
int get_json(string request, JsonBox::Value* json_value){
|
|
||||||
//TODO: implement a timeout and max_retries
|
|
||||||
|
|
||||||
uri = new URI(full_hostname+"/"+request);
|
uri = new URI(full_hostname+"/"+request);
|
||||||
string path(uri->getPathAndQuery());
|
string path(uri->getPathAndQuery());
|
||||||
if (path.empty()) path = "/";
|
if (path.empty()) path = "/";
|
||||||
|
|
||||||
// send request
|
// send request
|
||||||
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
||||||
session->sendRequest(req);
|
int code = send_request(session, req, answer);
|
||||||
|
return code;
|
||||||
// get response
|
|
||||||
HTTPResponse res;
|
|
||||||
//cout << res.getStatus() << " " << res.getReason() << endl;
|
|
||||||
string answer;
|
|
||||||
istream &is = session->receiveResponse(res);
|
|
||||||
StreamCopier::copyToString(is, answer);
|
|
||||||
|
|
||||||
cout << answer << endl;
|
|
||||||
//returning a Json struct
|
|
||||||
json_value->loadFromString(answer);
|
|
||||||
|
|
||||||
return res.getStatus();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int get_json(string request, JsonBox::Value* json_value){
|
||||||
|
/*
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
uri = new URI(full_hostname+"/"+request);
|
||||||
|
string path(uri->getPathAndQuery());
|
||||||
|
if (path.empty()) path = "/";
|
||||||
|
// send request
|
||||||
|
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
||||||
|
string answer;
|
||||||
|
int code = send_request(session, req, &answer);
|
||||||
|
json_value->loadFromString(answer);
|
||||||
|
return code;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
URI * uri;
|
URI * uri;
|
||||||
HTTPClientSession *session;
|
HTTPClientSession *session;
|
||||||
HTTPRequest *req;
|
|
||||||
|
|
||||||
string full_hostname;
|
string full_hostname;
|
||||||
|
|
||||||
int send_request(HTTPClientSession *session, HTTPRequest *req){
|
int send_request(HTTPClientSession *session, HTTPRequest &req, string *answer){
|
||||||
int n=0;
|
int n=0;
|
||||||
int code = -1;
|
int code = -1;
|
||||||
while(n<N_CONNECTION_TRIES){
|
while(n<N_CONNECTION_TRIES){
|
||||||
try {
|
try {
|
||||||
session->sendRequest( (*req) );
|
session->sendRequest( (req) );
|
||||||
code = 0;
|
HTTPResponse res;
|
||||||
|
istream &is = session->receiveResponse(res);
|
||||||
|
StreamCopier::copyToString(is, *answer);
|
||||||
|
code = res.getStatus();
|
||||||
|
if (code != 200){
|
||||||
|
cout << "HTTP ERROR " << res.getStatus() << ": " << res.getReason() << endl;
|
||||||
|
code = -1;
|
||||||
|
}
|
||||||
|
return code;
|
||||||
}
|
}
|
||||||
catch (exception& e){
|
catch (exception& e){
|
||||||
cout << "Exception:"<< e.what() << ", sleeping " << HTTP_TIMEOUT << " seconds\n";
|
cout << "Exception connecting to "<< full_hostname << ": "<< e.what() << ", sleeping " << HTTP_TIMEOUT << " seconds\n";
|
||||||
sleep(HTTP_TIMEOUT);
|
sleep(HTTP_TIMEOUT);
|
||||||
}
|
}
|
||||||
n+=1;
|
n+=1;
|
||||||
|
@ -72,12 +72,32 @@ public:
|
|||||||
RestHelper rest ;
|
RestHelper rest ;
|
||||||
rest.init("localhost",8080);
|
rest.init("localhost",8080);
|
||||||
std::string answer;
|
std::string answer;
|
||||||
|
std::cout << "---- REST test 1: true, string "<< std::endl;
|
||||||
int code = rest.get_json("status", &answer);
|
int code = rest.get_json("status", &answer);
|
||||||
std::cout << "Answer: " << answer << std::endl;
|
std::cout << "Answer: " << answer << std::endl;
|
||||||
|
|
||||||
//JsonBox::Value json_value;
|
std::cout << "---- REST test 2: 404, string "<< std::endl;
|
||||||
//code = rest.get_json("status", &json_value);
|
code = rest.get_json("statuss", &answer);
|
||||||
//std::cout << "JSON " << json_value["status"] << std::endl;
|
if (code != 0){
|
||||||
|
//throw -1;
|
||||||
|
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "---- REST test 3: true, json object "<< std::endl;
|
||||||
|
JsonBox::Value json_value;
|
||||||
|
code = rest.get_json("status", &json_value);
|
||||||
|
std::cout << "JSON " << json_value["status"] << std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
RestHelper rest2 ;
|
||||||
|
rest2.init("reallyfake",8080);
|
||||||
|
std::cout << "---- REST test 4: host not found, json object "<< std::endl;
|
||||||
|
JsonBox::Value json_value2;
|
||||||
|
code = rest2.get_json("status", &json_value2);
|
||||||
|
if (code != 0){
|
||||||
|
//throw -1;
|
||||||
|
std::cout << "I SHOULD THROW AN EXCEPTION!!!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user