Imported Upstream version 17.23.5
[platform/upstream/libzypp.git] / tests / lib / WebServer.h
1
2 #ifndef ZYPP_TEST_WEBSERVER_H
3 #define ZYPP_TEST_WEBSERVER_H
4
5 #include <zypp/Url.h>
6 #include <zypp/Pathname.h>
7 #include <zypp/base/PtrTypes.h>
8 #include <zypp/media/TransferSettings.h>
9
10 #include <functional>
11
12 /**
13  *
14  * Starts a webserver to simulate remote transfers in
15  * testcases
16  * \author Duncan Mac-Vicar P. <dmacvicar@suse.de>
17  *
18  * \code
19  * #include "WebServer.h"
20  *
21  * BOOST_AUTO_TEST_CASE(Foo)
22  * {
23  *
24  *      WebServer web((Pathname(TESTS_SRC_DIR) + "/datadir").c_str() );
25  *      web.start();
26  *
27  *     MediaSetAccess media( Url("http://localhost:9099"), "/" );
28  *
29  *     // do something with the url
30  *
31  *
32  *     web.stop();
33  *
34  * \endcode
35  */
36 class WebServer
37 {
38  public:
39
40    struct Request {
41      Request ( std::istream::__streambuf_type *rinbuf,
42                std::ostream::__streambuf_type *routbuf,
43                std::ostream::__streambuf_type *rerrbuf );
44      std::string path;
45      std::map< std::string, std::string > params;
46      std::istream rin;
47      std::ostream rout;
48      std::ostream rerr;
49    };
50
51    using RequestHandler = std::function<void ( Request &req )>;
52
53   /**
54    * creates a web server on \ref root and \port
55    */
56   WebServer(const zypp::Pathname &root, unsigned int port=10001, bool useSSL = false );
57   ~WebServer();
58   /**
59    * Starts the webserver worker thread
60    * Waits up to 10 seconds and returns whether the port is now active.
61    */
62   bool start();
63
64   /**
65    * Stops the worker thread
66    */
67   void stop();
68
69   /**
70    * returns the port we are listening to
71    */
72   int port() const;
73
74   /**
75    * returns the base url where the webserver is listening
76    */
77   zypp::Url url() const;
78
79   /**
80    * generates required transfer settings
81    */
82   zypp::media::TransferSettings transferSettings () const;
83
84   /**
85    * shows the log of last run
86    */
87   std::string log() const;
88
89   /**
90    * Checks if the server was stopped
91    */
92   bool isStopped() const;
93
94   /**
95    * Sets the request handler callback, if a callback for the path does already exist
96    * it is replaced, can be called at any time
97    *
98    * \note a request handler path is always prefixed with /handler so to call a handler named test, the
99    *       URL is "http://localhost/handler/test"
100    */
101   void addRequestHandler ( const std::string &path, RequestHandler &&handler );
102
103   /*!
104    * Removes a registered request hander, can be called at any time
105    */
106   void removeRequestHandler ( const std::string &path );
107
108   /*!
109    * Creates a request handler that simply returns the string in \a resp
110    */
111   static RequestHandler makeResponse(std::string resp);
112
113   /*!
114    * Creates a request handler that sends a HTTP response with \a status and \a content
115    * in the respose
116    */
117   static RequestHandler makeResponse(std::string status, std::string content);
118
119   /*!
120    * Creates a request handler that sends a HTTP response with \a status \a content
121    * and \a headers in the respose
122    */
123   static RequestHandler makeResponse(const std::string &status, const std::vector<std::string> &headers, const std::string &content);
124
125   /*!
126    * Creates the corresponding HTTP response to the given parameters
127    */
128   static std::string makeResponseString ( const std::string &status, const std::vector<std::string> &headers, const std::string &content );
129
130   class Impl;
131 private:
132   /** Pointer to implementation */
133   zypp::RW_pointer<Impl> _pimpl;
134 };
135
136 #endif