|
| 1 | +#define BOOST_TEST_MODULE RegistrationTest |
| 2 | +#include <boost/test/included/unit_test.hpp> |
| 3 | + |
| 4 | +//Normally this is defined in the build file, it's usually the project name |
| 5 | +#define ANSWER_SERVICE_NAME "RegistrationTest" |
| 6 | + |
| 7 | +#include "answer/Operation.hh" |
| 8 | +#include <boost/algorithm/string.hpp> |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +class Operation{ |
| 13 | +public: |
| 14 | + int test(string X){ |
| 15 | + return X.size(); |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +ANSWER_REGISTER_OPERATION(Operation::test); |
| 20 | + |
| 21 | + |
| 22 | +class TestContext: public |
| 23 | + answer::Context, |
| 24 | + answer::TransportInfo, |
| 25 | + answer::OperationInfo{ |
| 26 | + |
| 27 | + string _service; |
| 28 | + string _operation; |
| 29 | + string _accept; |
| 30 | + list<string> _accepts; |
| 31 | + |
| 32 | +public: |
| 33 | + TestContext(const string &serviceName, const string &operationName, const string& acceptsType): |
| 34 | + _service(serviceName), _operation(operationName), _accepts({acceptsType}){ |
| 35 | + } |
| 36 | + |
| 37 | + //Context |
| 38 | + virtual answer::CookieJar& cookieJar(){ |
| 39 | + throw std::runtime_error("CookieJar not implemented"); |
| 40 | + } |
| 41 | + virtual answer::Environment& environment(){ |
| 42 | + throw std::runtime_error("Environment not implemented"); |
| 43 | + } |
| 44 | + virtual answer::OperationInfo& operationInfo(){ |
| 45 | + return *this; |
| 46 | + } |
| 47 | + virtual answer::ProviderStore& providerStore(){ |
| 48 | + throw std::runtime_error("ProviderStore not implemented"); |
| 49 | + } |
| 50 | + virtual answer::TransportInfo& transportInfo(){ |
| 51 | + return *this; |
| 52 | + } |
| 53 | + |
| 54 | + //TransportInfo |
| 55 | + virtual const list<string>& accepts() const{ |
| 56 | + return _accepts; |
| 57 | + } |
| 58 | + virtual void addHeader ( const string& /*key*/, const string& /*value*/ = "", bool /*replace*/ = true ){ |
| 59 | + throw std::runtime_error("addHeader not implemented"); |
| 60 | + } |
| 61 | + virtual const string& redirect() const{ |
| 62 | + throw std::runtime_error("redirect not implemented"); |
| 63 | + } |
| 64 | + virtual const string& redirect ( const string& /*uri*/ ){ |
| 65 | + throw std::runtime_error("redirect (set) not implemented"); |
| 66 | + } |
| 67 | + virtual bool redirectSet() const{ |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + //OperationInfo |
| 72 | + virtual const string& operation() const{ |
| 73 | + return _operation; |
| 74 | + } |
| 75 | + virtual const string& service() const{ |
| 76 | + return _service; |
| 77 | + } |
| 78 | + virtual const string& url() const{ |
| 79 | + throw std::runtime_error("url not implemented"); |
| 80 | + } |
| 81 | + |
| 82 | +}; |
| 83 | + |
| 84 | + |
| 85 | +BOOST_AUTO_TEST_CASE( registration ) |
| 86 | +{ |
| 87 | + list<string> operations = answer::OperationStore::Instance().operationList(); |
| 88 | + BOOST_CHECK(operations.front() == string("RegistrationTest/test")); |
| 89 | +} |
| 90 | + |
| 91 | +BOOST_AUTO_TEST_CASE( request_response ){ |
| 92 | + TestContext context("RegistrationTest", "test", "application/xml"); |
| 93 | + answer::Operation &operation = answer::OperationStore::Instance().operation("RegistrationTest", "test"); |
| 94 | + answer::Response response = operation.invoke("<test>foobar</test>"); |
| 95 | + string body = response.body(); |
| 96 | + boost::trim(body); |
| 97 | + BOOST_CHECK(body == "<test>6</test>"); |
| 98 | +} |
| 99 | + |
| 100 | +BOOST_AUTO_TEST_CASE( request_response_codec ){ |
| 101 | + TestContext context("RegistrationTest", "test", "application/json"); |
| 102 | + answer::Operation &operation = answer::OperationStore::Instance().operation("RegistrationTest", "test"); |
| 103 | + answer::Response response = operation.invoke("<test>foobar</test>"); |
| 104 | + string body = response.body(); |
| 105 | + boost::trim(body); |
| 106 | + BOOST_CHECK(body == "\"6\""); |
| 107 | +} |
| 108 | + |
0 commit comments