1 module handy_httpd;
2 
3 public import handy_httpd.server;
4 public import handy_httpd.request;
5 public import handy_httpd.response;
6 public import handy_httpd.handler;
7 public import handy_httpd.responses;
8 
9 /** 
10  * General-purpose testing for the HTTP server and its behavior.
11  */
12 unittest {
13     import std.stdio;
14     import core.thread;
15     import core.time;
16     const ushort PORT = 45_312;
17 
18     /** 
19      * Helper function to prepare an HTTP server for testing.
20      * Returns: A simple HTTP server for testing.
21      */
22     HttpServer getSimpleServer() {
23         auto s = new HttpServer(
24             simpleHandler((ref request, ref response) {
25                 if (request.method == "GET") {
26                     response.writeBody("Hello world!");
27                 } else {
28                     response.methodNotAllowed();
29                 }
30             }),
31             "127.0.0.1",
32             PORT,
33             8192,
34             100,
35             true,
36             10
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 }