cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
test_client.h File Reference

In-process HTTP test client (ala Flask test_client). More...

#include <cwist/sys/app/app.h>
#include <stddef.h>
Include dependency graph for test_client.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_test_client_cookie
 
struct  cwist_test_client
 
struct  cwist_test_client_kv
 A single key/value pair for headers or ad-hoc cookies. More...
 
struct  cwist_test_client_request_options
 Optional parameters for advanced test requests. More...
 

Macros

Fluent Test Assertions
#define CWIST_ASSERT_STATUS(res, expected_status)
 
#define CWIST_ASSERT_HEADER(res, header_name, expected_value)
 
#define CWIST_ASSERT_BODY_CONTAINS(res, snippet)
 

Typedefs

typedef struct cwist_test_client_cookie cwist_test_client_cookie
 
typedef struct cwist_test_client cwist_test_client
 
typedef struct cwist_test_client_kv cwist_test_client_kv
 A single key/value pair for headers or ad-hoc cookies.
 
typedef struct cwist_test_client_request_options cwist_test_client_request_options
 Optional parameters for advanced test requests.
 

Functions

Lifecycle
cwist_test_client * cwist_test_client_create (cwist_app *app)
 Create a test client for an app.
 
void cwist_test_client_destroy (cwist_test_client *client)
 Destroy a test client.
 
HTTP Methods
cwist_http_response * cwist_test_client_get (cwist_test_client *client, const char *path)
 Perform a GET request.
 
cwist_http_response * cwist_test_client_post (cwist_test_client *client, const char *path, const char *body)
 Perform a POST request.
 
cwist_http_response * cwist_test_client_post_json (cwist_test_client *client, const char *path, const char *json_body)
 Perform a POST request with JSON content-type.
 
cwist_http_response * cwist_test_client_put (cwist_test_client *client, const char *path, const char *body)
 Perform a PUT request.
 
cwist_http_response * cwist_test_client_delete (cwist_test_client *client, const char *path)
 Perform a DELETE request.
 
cwist_http_response * cwist_test_client_patch (cwist_test_client *client, const char *path, const char *body)
 Perform a PATCH request.
 
cwist_http_response * cwist_test_client_request_ex (cwist_test_client *client, cwist_http_method_t method, const char *path, const cwist_test_client_request_options *opts)
 Perform a request with full control over headers, cookies and query.
 
cwist_http_response * cwist_test_client_post_multipart (cwist_test_client *client, const char *path, const char *field_name, const char *file_name, const char *content_type, const char *data, size_t data_len)
 Perform a POST request with multipart/form-data body simulating a file upload.
 
Cookie Jar
void cwist_test_client_set_cookie (cwist_test_client *client, const char *name, const char *value, const char *path)
 Set a persistent cookie in the client's cookie jar.
 
const char * cwist_test_client_get_cookie (cwist_test_client *client, const char *name)
 Get a cookie value from the jar.
 
void cwist_test_client_clear_cookies (cwist_test_client *client)
 Clear all cookies in the jar.
 

Detailed Description

In-process HTTP test client (ala Flask test_client).

Definition in file test_client.h.

Macro Definition Documentation

◆ CWIST_ASSERT_BODY_CONTAINS

#define CWIST_ASSERT_BODY_CONTAINS (   res,
  snippet 
)
Value:
do { \
const char *bdata = ((res) && (res)->body) ? (res)->body->data : NULL; \
if (!bdata || strstr(bdata, (snippet)) == NULL) { \
fprintf(stderr, "[ASSERT FAIL] %s:%d: body snippet '%s' not found in body '%s'\n", \
__FILE__, __LINE__, (snippet), bdata ? bdata : "<null>"); \
exit(1); \
} \
} while (0)
size_t cap
Definition otel.c:56

Definition at line 170 of file test_client.h.

◆ CWIST_ASSERT_HEADER

#define CWIST_ASSERT_HEADER (   res,
  header_name,
  expected_value 
)
Value:
do { \
const char *actual_val = NULL; \
if (res) { \
for (cwist_http_header_node *h = (res)->headers; h; h = h->next) { \
if (h->key && strcasecmp(h->key->data, (header_name)) == 0) { \
actual_val = h->value ? h->value->data : ""; \
break; \
} \
} \
} \
fprintf(stderr, "[ASSERT FAIL] %s:%d: expected header '%s' = '%s', got '%s'\n", \
actual_val ? actual_val : "<null>"); \
exit(1); \
} \
} while (0)
Linked list for headers to handle multiple headers easily.
Definition http.h:59
struct cwist_http_header_node * next
Definition http.h:62

Definition at line 151 of file test_client.h.

◆ CWIST_ASSERT_STATUS

#define CWIST_ASSERT_STATUS (   res,
  expected_status 
)
Value:
do { \
if (!(res) || (res)->status_code != (expected_status)) { \
fprintf(stderr, "[ASSERT FAIL] %s:%d: expected status %d, got %d\n", __FILE__, \
__LINE__, (int)(expected_status), (res) ? (int)(res)->status_code : -1); \
exit(1); \
} \
} while (0)

Definition at line 142 of file test_client.h.

Typedef Documentation

◆ cwist_test_client

◆ cwist_test_client_cookie

◆ cwist_test_client_kv

A single key/value pair for headers or ad-hoc cookies.

◆ cwist_test_client_request_options

Optional parameters for advanced test requests.

Function Documentation

◆ cwist_test_client_clear_cookies()

void cwist_test_client_clear_cookies ( cwist_test_client *  client)

Clear all cookies in the jar.

Clear all cookies in the jar.

Parameters
clientClient owning the jar; NULL is a no-op.

Definition at line 459 of file test_client.c.

References cap, cwist_test_client::cookies, and cwist_test_client_cookie::next.

Referenced by cwist_test_client_destroy().

◆ cwist_test_client_create()

cwist_test_client * cwist_test_client_create ( cwist_app *  app)

Create a test client for an app.

Create a test client for an app.

Parameters
appApp to dispatch requests to; must not be NULL.
Returns
Newly allocated client with an empty cookie jar, or NULL on invalid input or allocation failure. Free with cwist_test_client_destroy().

Definition at line 187 of file test_client.c.

References cwist_test_client::app, cap, cwist_test_client::cookies, and cwist_alloc().

◆ cwist_test_client_delete()

cwist_http_response * cwist_test_client_delete ( cwist_test_client *  client,
const char *  path 
)

Perform a DELETE request.

Perform a DELETE request.

Parameters
clientClient to send through.
pathRequest path.
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 346 of file test_client.c.

References cap, and CWIST_HTTP_DELETE.

◆ cwist_test_client_destroy()

void cwist_test_client_destroy ( cwist_test_client *  client)

Destroy a test client.

Destroy a test client.

Parameters
clientClient to destroy; NULL is a no-op.

Definition at line 199 of file test_client.c.

References cwist_free(), and cwist_test_client_clear_cookies().

◆ cwist_test_client_get()

cwist_http_response * cwist_test_client_get ( cwist_test_client *  client,
const char *  path 
)

Perform a GET request.

Perform a GET request.

Parameters
clientClient to send through.
pathRequest path, optionally containing a "?query" suffix.
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 297 of file test_client.c.

References cap, and CWIST_HTTP_GET.

◆ cwist_test_client_get_cookie()

const char * cwist_test_client_get_cookie ( cwist_test_client *  client,
const char *  name 
)

Get a cookie value from the jar.

Returns
Cookie value or NULL if not found. Do not free.

Get a cookie value from the jar.

Parameters
clientClient owning the jar; NULL yields NULL.
nameCookie name to look up.
Returns
Cookie value, or NULL if not found or on invalid input. The returned pointer is owned by the client and must not be freed.

Definition at line 450 of file test_client.c.

References cap, and cwist_test_client::cookies.

◆ cwist_test_client_patch()

cwist_http_response * cwist_test_client_patch ( cwist_test_client *  client,
const char *  path,
const char *  body 
)

Perform a PATCH request.

Perform a PATCH request.

Parameters
clientClient to send through.
pathRequest path.
bodyNull-terminated request body; may be NULL.
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 356 of file test_client.c.

References cwist_test_client_request_options::body, and CWIST_HTTP_PATCH.

◆ cwist_test_client_post()

cwist_http_response * cwist_test_client_post ( cwist_test_client *  client,
const char *  path,
const char *  body 
)

Perform a POST request.

Perform a POST request.

Parameters
clientClient to send through.
pathRequest path.
bodyNull-terminated request body; may be NULL.
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 307 of file test_client.c.

References cwist_test_client_request_options::body, and CWIST_HTTP_POST.

◆ cwist_test_client_post_json()

cwist_http_response * cwist_test_client_post_json ( cwist_test_client *  client,
const char *  path,
const char *  json_body 
)

Perform a POST request with JSON content-type.

Perform a POST request with JSON content-type.

Parameters
clientClient to send through.
pathRequest path.
json_bodyNull-terminated JSON body; may be NULL.
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 320 of file test_client.c.

References cwist_test_client_request_options::body, cap, cwist_test_client_request_options::content_type, and CWIST_HTTP_POST.

◆ cwist_test_client_post_multipart()

cwist_http_response * cwist_test_client_post_multipart ( cwist_test_client *  client,
const char *  path,
const char *  field_name,
const char *  file_name,
const char *  content_type,
const char *  data,
size_t  data_len 
)

Perform a POST request with multipart/form-data body simulating a file upload.

Parameters
field_nameForm field name.
file_nameFile name to report.
content_typeMIME type, e.g. "text/plain".
dataFile contents.
data_lenLength of file contents.

Perform a POST request with multipart/form-data body simulating a file upload.

Parameters
clientClient to send through.
pathRequest path.
field_nameForm field name for the file part; must not be NULL.
file_nameFile name for the Content-Disposition header; must not be NULL.
content_typeMIME type of the file data; must not be NULL.
dataRaw file bytes; must not be NULL.
data_lenLength of data in bytes.
Returns
Response owned by the caller, or NULL on invalid input or allocation failure.

Definition at line 375 of file test_client.c.

References cwist_test_client::app, cwist_test_client_request_options::body, cwist_test_client_request_options::body_len, cap, cwist_test_client_request_options::content_type, cwist_alloc(), cwist_free(), and CWIST_HTTP_POST.

◆ cwist_test_client_put()

cwist_http_response * cwist_test_client_put ( cwist_test_client *  client,
const char *  path,
const char *  body 
)

Perform a PUT request.

Perform a PUT request.

Parameters
clientClient to send through.
pathRequest path.
bodyNull-terminated request body; may be NULL.
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 334 of file test_client.c.

References cwist_test_client_request_options::body, and CWIST_HTTP_PUT.

◆ cwist_test_client_request_ex()

cwist_http_response * cwist_test_client_request_ex ( cwist_test_client *  client,
cwist_http_method_t  method,
const char *  path,
const cwist_test_client_request_options *  opts 
)

Perform a request with full control over headers, cookies and query.

Perform a request with full control over headers, cookies and query.

Parameters
clientClient to send through.
methodHTTP method to use.
pathRequest path, optionally containing a "?query" suffix.
optsOptional request options (body, headers, content type, ad-hoc cookies).
Returns
Response owned by the caller, or NULL on failure. See do_request().

Definition at line 286 of file test_client.c.

◆ cwist_test_client_set_cookie()

void cwist_test_client_set_cookie ( cwist_test_client *  client,
const char *  name,
const char *  value,
const char *  path 
)

Set a persistent cookie in the client's cookie jar.

Set a persistent cookie in the client's cookie jar.

Parameters
clientClient owning the jar; NULL is a no-op.
nameCookie name; must not be NULL.
valueCookie value; NULL clears the stored value.
pathCookie path; NULL leaves the path unchanged for existing cookies or stores no path for new ones.

Definition at line 422 of file test_client.c.

References cap, cwist_test_client::cookies, cwist_alloc(), and cwist_free().