2 #ifndef ZYPP_TEST_WEBSERVER_H
3 #define ZYPP_TEST_WEBSERVER_H
6 #include <zypp/Pathname.h>
7 #include <zypp/base/PtrTypes.h>
8 #include <zypp/media/TransferSettings.h>
14 * Starts a webserver to simulate remote transfers in
16 * \author Duncan Mac-Vicar P. <dmacvicar@suse.de>
19 * #include "WebServer.h"
21 * BOOST_AUTO_TEST_CASE(Foo)
24 * WebServer web((Pathname(TESTS_SRC_DIR) + "/datadir").c_str() );
27 * MediaSetAccess media( Url("http://localhost:9099"), "/" );
29 * // do something with the url
41 Request ( std::istream::__streambuf_type *rinbuf,
42 std::ostream::__streambuf_type *routbuf,
43 std::ostream::__streambuf_type *rerrbuf );
45 std::map< std::string, std::string > params;
51 using RequestHandler = std::function<void ( Request &req )>;
54 * creates a web server on \ref root and \port
56 WebServer(const zypp::Pathname &root, unsigned int port=10001, bool useSSL = false );
59 * Starts the webserver worker thread
60 * Waits up to 10 seconds and returns whether the port is now active.
65 * Stops the worker thread
70 * returns the port we are listening to
75 * returns the base url where the webserver is listening
77 zypp::Url url() const;
80 * generates required transfer settings
82 zypp::media::TransferSettings transferSettings () const;
85 * shows the log of last run
87 std::string log() const;
90 * Checks if the server was stopped
92 bool isStopped() const;
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
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"
101 void addRequestHandler ( const std::string &path, RequestHandler &&handler );
104 * Removes a registered request hander, can be called at any time
106 void removeRequestHandler ( const std::string &path );
109 * Creates a request handler that simply returns the string in \a resp
111 static RequestHandler makeResponse(std::string resp);
114 * Creates a request handler that sends a HTTP response with \a status and \a content
117 static RequestHandler makeResponse(std::string status, std::string content);
120 * Creates a request handler that sends a HTTP response with \a status \a content
121 * and \a headers in the respose
123 static RequestHandler makeResponse(const std::string &status, const std::vector<std::string> &headers, const std::string &content);
126 * Creates the corresponding HTTP response to the given parameters
128 static std::string makeResponseString ( const std::string &status, const std::vector<std::string> &headers, const std::string &content );
132 /** Pointer to implementation */
133 zypp::RW_pointer<Impl> _pimpl;