1 module handy_httpd;
2 
3 public import handy_httpd.server;
4 public import handy_httpd.server_config;
5 public import handy_httpd.request;
6 public import handy_httpd.response;
7 public import handy_httpd.handler;
8 public import handy_httpd.responses;
9 
10 /** 
11  * General-purpose testing for the HTTP server and its behavior.
12  */
13 unittest {
14     import std.stdio;
15     import core.thread;
16     import core.time;
17     const ushort PORT = 45_312;
18 
19     /** 
20      * Helper function to prepare an HTTP server for testing.
21      * Returns: A simple HTTP server for testing.
22      */
23     HttpServer getSimpleServer() {
24         ServerConfig config = ServerConfig.defaultValues();
25         config.port = PORT;
26         config.verbose = true;
27         config.workerPoolSize = 10;
28         auto s = new HttpServer(
29             simpleHandler((ref request, ref response) {
30                 if (request.method == "GET") {
31                     response.writeBody("Hello world!");
32                 } else {
33                     response.methodNotAllowed();
34                 }
35             }),
36             config
37         );
38         // Start up the server in its own thread.
39         new Thread(() {s.start();}).start();
40         return s;
41     }
42 
43     // Test that the server can start up and shut down properly.
44     auto s = getSimpleServer();
45     while (!s.isReady) {
46         writeln("Waiting for server to be ready...");
47         Thread.sleep(msecs(10));
48     }
49     assert(s.isReady);
50 
51     // Test basic HTTP request behavior.
52     import std.net.curl;
53     import std.string;
54     import std.exception;
55     string url = std..string.format!"http://localhost:%d"(PORT);
56     
57     assert(get(url) == "Hello world!");
58     assertThrown!CurlException(post(url, ["hello"]));
59 
60     s.stop();
61 }