38#define MOCK_PORT 19090
53 if (strcmp(req->
method,
"GET") == 0 &&
54 strcmp(req->
path,
"/api/test") == 0) {
78int main(
int argc,
char* argv[]) {
82 while ((
getoptret = getopt(argc, argv,
"V:h")) != -1) {
85 if (strcmp(optarg,
"LOG_DEBUG") == 0)
87 else if (strcmp(optarg,
"LOG_INFO") == 0)
89 else if (strcmp(optarg,
"LOG_NOTICE") == 0)
91 else if (strcmp(optarg,
"LOG_ERR") == 0)
96 fprintf(stderr,
"Usage: %s [-V LOG_LEVEL]\n", argv[0]);
105 fprintf(stderr,
"FAIL: could not start mock server\n");
108 printf(
"CHECK start server ... PASS\n");
113 fprintf(stderr,
"FAIL: could not create server thread\n");
124 struct addrinfo hints;
125 struct addrinfo* res = NULL;
126 memset(&hints, 0,
sizeof(hints));
127 hints.ai_family = AF_INET;
128 hints.ai_socktype = SOCK_STREAM;
131 if (getaddrinfo(
"127.0.0.1",
port_str, &hints, &res) != 0 || !res) {
132 fprintf(stderr,
"FAIL: getaddrinfo\n");
134 pthread_join(thr, NULL);
138 sock_fd = (int)socket(res->ai_family, res->ai_socktype,
141 connect(sock_fd, res->ai_addr, res->ai_addrlen) != 0) {
142 fprintf(stderr,
"FAIL: connect\n");
145 pthread_join(thr, NULL);
153 const char* http_req =
154 "GET /api/test HTTP/1.1\r\n"
155 "Host: localhost\r\n"
156 "Connection: close\r\n"
158 ssize_t sent = send(sock_fd, http_req, strlen(http_req), 0);
160 fprintf(stderr,
"FAIL: send\n");
163 pthread_join(thr, NULL);
170 memset(resp_buf, 0,
sizeof(resp_buf));
173 while ((nr = recv(sock_fd, resp_buf + total,
174 sizeof(resp_buf) - 1 - (
size_t)total, 0)) > 0) {
179 printf(
"Response:\n%s\n", resp_buf);
183 if (strstr(resp_buf,
"200") == NULL) {
184 fprintf(stderr,
"FAIL: expected 200 in response\n");
187 if (strstr(resp_buf,
"{\"status\":\"ok\"}") == NULL) {
188 fprintf(stderr,
"FAIL: expected JSON body in response\n");
191 printf(
"CHECK GET /api/test ... %s\n", pass ?
"PASS" :
"FAIL");
195 pthread_join(thr, NULL);
198 printf(
"CHECK cleanup ... PASS\n");
static void * server_thread(void *arg)
Thread function that runs the server accept loop.
#define MOCK_PORT
mock server port
static void on_request(N_HTTP_REQUEST *req, N_HTTP_RESPONSE *resp, void *user_data)
Request handler: returns JSON for GET /api/test, 404 otherwise.
static N_MOCK_SERVER * g_server
global server pointer for thread
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
N_STR * body
response body
char path[2048]
request path
char method[16]
HTTP method.
int status_code
HTTP status code.
char content_type[128]
Content-Type header value.
void n_mock_server_free(N_MOCK_SERVER **server)
Free a mock server and close the listening socket.
void n_mock_server_stop(N_MOCK_SERVER *server)
Signal the mock server to stop accepting connections.
void n_mock_server_run(N_MOCK_SERVER *server)
Run the mock server accept loop.
N_MOCK_SERVER * n_mock_server_start(int port, void(*on_request)(N_HTTP_REQUEST *, N_HTTP_RESPONSE *, void *), void *user_data)
Start a mock HTTP server: set up listener and return immediately.
parsed HTTP request for mock server callback
HTTP response to send from mock server callback.
Common headers and low-level functions & define.
N_STR and string function declaration.