Commit 369ed5d290fac1f69fe8a25c4742da97ce654fcf

Authored by Moritz W
1 parent ea418d25

Refactor HttpHandler to inherit from abstract IHttpHandler class, so it can be mocked for testing

hueplusplus/HttpHandler.cpp
... ... @@ -37,7 +37,7 @@ class SocketCloser {
37 37 private: int s;
38 38 };
39 39  
40   -std::string HttpHandler::send(const std::string & msg, const std::string & adr, int port)
  40 +std::string HttpHandler::send(const std::string & msg, const std::string & adr, int port) const
41 41 {
42 42 // create socket
43 43 int socketFD = socket(AF_INET, SOCK_STREAM, 0);
... ... @@ -126,7 +126,7 @@ std::string HttpHandler::send(const std::string & msg, const std::string & adr,
126 126 return response;
127 127 }
128 128  
129   -std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::string & adr, int port)
  129 +std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::string & adr, int port) const
130 130 {
131 131 std::string response = send(msg, adr, port);
132 132 size_t start = response.find("\r\n\r\n");
... ... @@ -139,7 +139,7 @@ std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::str
139 139 return response;
140 140 }
141 141  
142   -std::vector<std::string> HttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout)
  142 +std::vector<std::string> HttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const
143 143 {
144 144 hostent *server; // host information
145 145 sockaddr_in server_addr; // server address
... ... @@ -218,7 +218,7 @@ std::vector&lt;std::string&gt; HttpHandler::sendMulticast(const std::string &amp; msg, con
218 218 return returnString;
219 219 }
220 220  
221   -std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  221 +std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const
222 222 {
223 223 std::string request;
224 224 // Protocol reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
... ... @@ -244,23 +244,23 @@ std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, st
244 244 return sendGetHTTPBody(request.c_str(), adr, port);
245 245 }
246 246  
247   -std::string HttpHandler::GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  247 +std::string HttpHandler::GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const
248 248 {
249 249 return sendHTTPRequest("GET", uri, content_type, body, adr, port);
250 250 }
251 251  
252   -std::string HttpHandler::POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  252 +std::string HttpHandler::POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const
253 253 {
254 254 return sendHTTPRequest("POST", uri, content_type, body, adr, port);
255 255 }
256 256  
257   -std::string HttpHandler::PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  257 +std::string HttpHandler::PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const
258 258 {
259 259 return sendHTTPRequest("PUT", uri, content_type, body, adr, port);
260 260 }
261 261  
262 262 //! \todo Get rid of duplicate code in GETJson, POSTJson and PUTJson
263   -Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port)
  263 +Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const
264 264 {
265 265 std::string response = GETString(uri, "application/json", body.toStyledString(), adr, port);
266 266  
... ... @@ -277,7 +277,7 @@ Json::Value HttpHandler::GETJson(std::string uri, const Json::Value&amp; body, const
277 277 return result;
278 278 }
279 279  
280   -Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port)
  280 +Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const
281 281 {
282 282 std::string response = POSTString(uri, "application/json", body.toStyledString(), adr, port);
283 283  
... ... @@ -294,7 +294,7 @@ Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value&amp; body, cons
294 294 return result;
295 295 }
296 296  
297   -Json::Value HttpHandler::PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port)
  297 +Json::Value HttpHandler::PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const
298 298 {
299 299 std::string response = PUTString(uri, "application/json", body.toStyledString(), adr, port);
300 300  
... ...
hueplusplus/include/HttpHandler.h
... ... @@ -24,9 +24,10 @@
24 24 #include <vector>
25 25  
26 26 #include "json/json.h"
  27 +#include "IHttpHandler.h"
27 28  
28 29 //! Class to handle http requests and multicast requests
29   -class HttpHandler
  30 +class HttpHandler : public IHttpHandler
30 31 {
31 32 public:
32 33 //! \brief Function that sends a given message to the specified host and returns the response.
... ... @@ -36,7 +37,7 @@ public:
36 37 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
37 38 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
38 39 //! \return String containing the response of the host
39   - std::string send(const std::string &msg, const std::string &adr, int port=80);
  40 + std::string send(const std::string &msg, const std::string &adr, int port=80) const;
40 41  
41 42 //! \brief Function that sends a given message to the specified host and returns the body of the response.
42 43 //!
... ... @@ -46,7 +47,7 @@ public:
46 47 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
47 48 //! \param port Optional integer that specifies the port to which the request is sent. Default is 80
48 49 //! \return String containing the body of the response of the host
49   - std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80);
  50 + std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const;
50 51  
51 52 //! \brief Function that sends a multicast request with the specified message.
52 53 //!
... ... @@ -56,7 +57,7 @@ public:
56 57 //! \param port Optional integer that specifies the port to which the request is sent. Default is 1900
57 58 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5
58 59 //! \return Vector containing strings of each answer received
59   - std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5);
  60 + std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const;
60 61  
61 62 //! \brief Function that sends a HTTP request with the given method to the specified host and returns the body of the response.
62 63 //!
... ... @@ -69,7 +70,7 @@ public:
69 70 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
70 71 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
71 72 //! \return String containing the body of the response of the host
72   - std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  73 + std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const;
73 74  
74 75 //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response.
75 76 //!
... ... @@ -81,7 +82,7 @@ public:
81 82 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
82 83 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
83 84 //! \return String containing the body of the response of the host
84   - std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  85 + std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const;
85 86  
86 87 //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response.
87 88 //!
... ... @@ -93,7 +94,7 @@ public:
93 94 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
94 95 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
95 96 //! \return String containing the body of the response of the host
96   - std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  97 + std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const;
97 98  
98 99 //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response.
99 100 //!
... ... @@ -105,7 +106,7 @@ public:
105 106 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
106 107 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
107 108 //! \return String containing the body of the response of the host
108   - std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  109 + std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const;
109 110  
110 111 //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response.
111 112 //!
... ... @@ -116,7 +117,7 @@ public:
116 117 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
117 118 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
118 119 //! \return Json::Value containing the parsed body of the response of the host
119   - Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80);
  120 + Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const;
120 121  
121 122 //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response.
122 123 //!
... ... @@ -127,7 +128,7 @@ public:
127 128 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
128 129 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
129 130 //! \return Json::Value containing the parsed body of the response of the host
130   - Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80);
  131 + Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const;
131 132  
132 133 //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response.
133 134 //!
... ... @@ -138,7 +139,7 @@ public:
138 139 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
139 140 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
140 141 //! \return Json::Value containing the parsed body of the response of the host
141   - Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80);
  142 + Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const;
142 143 };
143 144  
144 145 #endif
... ...
hueplusplus/include/IHttpHandler.h 0 โ†’ 100644
  1 +/**
  2 + \file IHttpHandler.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This program is free software; you can redistribute it and/or modify
  8 + it under the terms of the GNU General Public License as published by
  9 + the Free Software Foundation; either version 3 of the License, or
  10 + (at your option) any later version.
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program; if not, write to the Free Software Foundation,
  17 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18 +**/
  19 +
  20 +#ifndef _IHTTPHANDLER_H
  21 +#define _IHTTPHANDLER_H
  22 +
  23 +#include <string>
  24 +#include <vector>
  25 +
  26 +#include "json/json.h"
  27 +
  28 +class IHttpHandler
  29 +{
  30 +public:
  31 +
  32 + //! \brief Virtual dtor
  33 + virtual ~IHttpHandler() = default;
  34 +
  35 + //! \brief Virtual function that should send a given message to a specified host and return the response.
  36 + //!
  37 + //! It should return a string containing the response of the host.
  38 + //! \param msg String that contains the message that should be sent to the specified address
  39 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  40 + //! \param port Optional integer that specifies the port to which the request should be sent to. Default is 80
  41 + //! \return String containing the response of the host
  42 + virtual std::string send(const std::string &msg, const std::string &adr, int port=80) const = 0;
  43 +
  44 + //! \brief Virtual Function that should given message to a specified host and return the body of the response.
  45 + //!
  46 + //! It should return a string containing only the body of the response of the host.
  47 + //! Note if no body is found a runtime error is thrown!
  48 + //! \param msg String that contains the message that should sent to the specified address
  49 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  50 + //! \param port Optional integer that specifies the port to which the request should be sent. Default is 80
  51 + //! \return String containing the body of the response of the host
  52 + virtual std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const = 0;
  53 +
  54 + //! \brief Virtual function that should send a multicast request with a specified message.
  55 + //!
  56 + //! It should return a vector containing all responses the multicast request got back
  57 + //! \param msg String that contains the request that should be sent to the specified address
  58 + //! \param adr Optional String that contains an ip or hostname in dotted decimal notation, default is "239.255.255.250"
  59 + //! \param port Optional integer that specifies the port to which the request should be sent. Default is 1900
  60 + //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5
  61 + //! \return Vector containing strings of each answer received
  62 + virtual std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0;
  63 +
  64 + //! \brief Virtual function that should send a HTTP request with the given method to the specified host and return the body of the response.
  65 + //!
  66 + //! It should return a string containing only the body of the response of the host.
  67 + //! Note body can also be left empty!
  68 + //! \param method String that contains the HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ...
  69 + //! \param uri String that contains the uniform resource identifier
  70 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  71 + //! \param body String that contains the data of the request
  72 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  73 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  74 + //! \return String containing the body of the response of the host
  75 + virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0;
  76 +
  77 + //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response.
  78 + //!
  79 + //! It should return a string containing only the body of the response of the host.
  80 + //! Note body can also be left empty!
  81 + //! \param uri String that contains the uniform resource identifier
  82 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  83 + //! \param body String that contains the data of the request
  84 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  85 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  86 + //! \return String containing the body of the response of the host
  87 + virtual std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0;
  88 +
  89 + //! \brief Virtual function that should send a HTTP POST request to the specified host and returns the body of the response.
  90 + //!
  91 + //! It should return a string containing only the body of the response of the host.
  92 + //! Note body can also be left empty!
  93 + //! \param uri String that contains the uniform resource identifier
  94 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  95 + //! \param body String that contains the data of the request
  96 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  97 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  98 + //! \return String containing the body of the response of the host
  99 + virtual std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0;
  100 +
  101 + //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response.
  102 + //!
  103 + //! It should return a string containing only the body of the response of the host.
  104 + //! Note body can also be left empty!
  105 + //! \param uri String that contains the uniform resource identifier
  106 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  107 + //! \param body String that contains the data of the request
  108 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  109 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  110 + //! \return String containing the body of the response of the host
  111 + virtual std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0;
  112 +
  113 + //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response.
  114 + //!
  115 + //! It should return a Json::Value parsed from the body of the response of the host.
  116 + //! Note body can also be left empty!
  117 + //! \param uri String that contains the uniform resource identifier
  118 + //! \param body Json::Value that contains the data of the request
  119 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  120 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  121 + //! \return Json::Value containing the parsed body of the response of the host
  122 + virtual Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0;
  123 +
  124 + //! \brief Virtual function that should send a HTTP POST request to the specified host and return the body of the response.
  125 + //!
  126 + //! It should return a Json::Value parsed from the body of the response of the host.
  127 + //! Note body can also be left empty!
  128 + //! \param uri String that contains the uniform resource identifier
  129 + //! \param body Json::Value that contains the data of the request
  130 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  131 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  132 + //! \return Json::Value containing the parsed body of the response of the host
  133 + virtual Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0;
  134 +
  135 + //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response.
  136 + //!
  137 + //! It should return a Json::Value parsed from the body of the response of the host.
  138 + //! Note body can also be left empty!
  139 + //! \param uri String that contains the uniform resource identifier
  140 + //! \param body Json::Value that contains the data of the request
  141 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  142 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  143 + //! \return Json::Value containing the parsed body of the response of the host
  144 + virtual Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0;
  145 +};
  146 +
  147 +#endif
... ...