summaryrefslogtreecommitdiffstats
path: root/tests/test_server.h
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 12:25:48 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 12:25:48 -0300
commite2c6ae13af335b311da2429c903032124f926df2 (patch)
tree73776fab7f3e52124a217e2768968fc9a8a76c34 /tests/test_server.h
downloadc-e2c6ae13af335b311da2429c903032124f926df2.tar.gz
Initial importv0.1.0
Diffstat (limited to 'tests/test_server.h')
-rw-r--r--tests/test_server.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/test_server.h b/tests/test_server.h
new file mode 100644
index 0000000..cbcd4e0
--- /dev/null
+++ b/tests/test_server.h
@@ -0,0 +1,69 @@
+/**
+ * @file test_server.h
+ * @brief A throwaway HTTP/1.1 server on a random loopback port.
+ *
+ * The suite points the client at one of these through `base_url`. Nothing is
+ * mocked: the code path under test is the one that runs in production, libcurl
+ * and response-body reading included. It doubles as the test that `base_url`
+ * works, since without it none of the suite would run.
+ */
+#ifndef CEPIS_TEST_SERVER_H
+#define CEPIS_TEST_SERVER_H
+
+#include <stddef.h>
+
+typedef struct test_server test_server_t;
+
+/** One canned reply. */
+typedef struct {
+ long status;
+ /** Body, copied. `NULL` means empty. */
+ const char *body;
+ /** Extra header lines without the trailing CRLF, or `NULL`. */
+ const char *headers;
+} ts_reply_t;
+
+/** @brief Start a server. @return `NULL` if it could not bind or spawn. */
+test_server_t *ts_start(void);
+
+/** @brief Stop and release a server. `NULL` is ignored. */
+void ts_stop(test_server_t *server);
+
+/** @brief `http://127.0.0.1:PORT`, owned by the server. */
+const char *ts_base_url(const test_server_t *server);
+
+/** @brief Register one reply for @p path, replacing any previous route. */
+void ts_respond(test_server_t *server, const char *path, long status,
+ const char *body, const char *headers);
+
+/**
+ * @brief Register a sequence of replies for @p path.
+ *
+ * Each request consumes the next entry; the last one repeats once the sequence
+ * runs out. This is how retrying is tested.
+ */
+void ts_respond_seq(test_server_t *server, const char *path,
+ const ts_reply_t *replies, size_t count);
+
+/**
+ * @brief Register the sequence used for any path with no explicit route.
+ *
+ * Used by the chunking test, where the request paths are thousands of
+ * characters long and matching them exactly buys nothing.
+ */
+void ts_respond_default(test_server_t *server, const ts_reply_t *replies,
+ size_t count);
+
+/** @brief How many requests so far had a path starting with @p prefix. */
+size_t ts_count(test_server_t *server, const char *prefix);
+
+/** @brief How many requests the server has served in total. */
+size_t ts_total(test_server_t *server);
+
+/**
+ * @brief The path of request @p index, or `NULL` when there is no such
+ * request. Owned by the server.
+ */
+const char *ts_path(test_server_t *server, size_t index);
+
+#endif