25 #include <vtkDebugLeaks.h> 29 CPPUNIT_TEST_SUITE(mitkRESTClientTestSuite);
31 MITK_TEST(MultipleGetRequestValidURI_AllTasksFinish);
34 MITK_TEST(GetRequestInvalidURI_ThrowsException);
35 MITK_TEST(PutRequestInvalidURI_ThrowsException);
36 MITK_TEST(PostRequestInvalidURI_ThrowsException);
37 CPPUNIT_TEST_SUITE_END();
41 web::json::value m_Data;
44 const web::json::value &,
45 const web::http::method &,
49 response.set_body(m_Data);
61 m_Data = web::json::value();
62 m_Data[U(
"userId")] = web::json::value(1);
63 m_Data[U(
"id")] = web::json::value(1);
64 m_Data[U(
"title")] = web::json::value(U(
"this is a title"));
65 m_Data[U(
"body")] = web::json::value(U(
"this is a body"));
76 CPPUNIT_FAIL(
"Getting Service in setUp() failed");
79 m_Service->
ReceiveRequest(U(
"http://localhost:8080/clienttest"),
this);
84 void GetRequestValidURI_ReturnsExpectedJSON()
86 web::json::value result;
88 m_Service->
SendRequest(U(
"http://localhost:8080/clienttest"))
89 .then([&](pplx::task<web::json::value> resultTask) {
92 result = resultTask.get();
102 CPPUNIT_ASSERT_MESSAGE(
"Result is the expected JSON value", result == m_Data);
105 void MultipleGetRequestValidURI_AllTasksFinish()
110 std::vector<pplx::task<void>> tasks;
111 for (
int i = 0; i < 20; ++i)
113 pplx::task<void> singleTask = m_Service->
SendRequest(U(
"http://localhost:8080/clienttest"))
114 .then([&](pplx::task<web::json::value> resultTask) {
127 tasks.emplace_back(singleTask);
130 auto joinTask = pplx::when_all(begin(tasks), end(tasks));
133 .then([&](pplx::task<void> resultTask) {
148 CPPUNIT_ASSERT_MESSAGE(
"Multiple Requests", 21 == count);
151 void PutRequestValidURI_ReturnsExpectedJSON()
154 web::json::value result;
159 .then([&](pplx::task<web::json::value> resultTask) {
162 result = resultTask.get();
172 CPPUNIT_ASSERT_MESSAGE(
173 "Result is the expected JSON value, check if the link is still valid since this is an optional test",
177 void PostRequestValidURI_ReturnsExpectedJSON()
180 web::json::value result;
181 web::json::value data;
183 data[U(
"userId")] = m_Data[U(
"userId")];
184 data[U(
"title")] = m_Data[U(
"title")];
185 data[U(
"body")] = m_Data[U(
"body")];
189 .then([&](pplx::task<web::json::value> resultTask) {
192 result = resultTask.get();
202 data[U(
"id")] = web::json::value(101);
203 CPPUNIT_ASSERT_MESSAGE(
204 "Result is the expected JSON value, check if the link is still valid since this is an optional test",
208 void PostRequestHeaders_Success()
211 headers.insert(mitk::RESTUtil::ParamMap::value_type(
212 U(
"Content-Type"), U(
"multipart/related; type=\"application/dicom\"; boundary=boundary")));
214 m_Service->
SendRequest(U(
"http://localhost:8080/clienttest")).then([&](pplx::task<web::json::value> resultTask) {
231 web::json::value result;
233 m_Service->
SendRequest(U(
"http://localhost:1234/invalid"))
234 .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
237 void GetRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(GetException(),
mitk::Exception); }
242 web::json::value result;
245 .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
248 void PutRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(PutException(),
mitk::Exception); }
253 web::json::value result;
256 .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
259 void PostRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(PostException(),
mitk::Exception); }
virtual pplx::task< web::json::value > SendJSONRequest(const web::uri &uri, const RequestType &type=RequestType::Get, const web::json::value *body=nullptr, const std::map< utility::string_t, utility::string_t > headers={}, const utility::string_t &filePath={})=0
Executes a HTTP request in the mitkRESTClient class.
ServiceReferenceU GetServiceReference(const std::string &clazz)
virtual void ReceiveRequest(const web::uri &uri, IRESTObserver *observer)=0
starts listening for requests if there isn't another observer listening and the port is free ...
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
This is a microservice interface for managing REST requests.
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
void * GetService(const ServiceReferenceBase &reference)
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Test fixture for parameterized tests.
virtual pplx::task< web::json::value > SendRequest(const web::uri &uri, const RequestType &type=RequestType::Get, const std::map< utility::string_t, utility::string_t > headers={})=0
Executes a HTTP request in the mitkRESTClient class.
virtual web::http::http_response Notify(const web::uri &uri, const web::json::value &data, const web::http::method &method, const mitk::RESTUtil::ParamMap &headers)=0
Called if there's an incoming request for the observer, observer implements how to handle request...
web::http::http_response http_response
mitk::PlanePositionManagerService * m_Service
std::map< utility::string_t, utility::string_t > ParamMap
virtual void HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri={})=0
Handles the deletion of an observer for all or a specific uri.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.