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

test_client.c interface. More...

#include <cwist/sys/app/test_client.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/core/sstring/sstring.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
Include dependency graph for test_client.c:

Go to the source code of this file.

Functions

cwist_test_client * cwist_test_client_create (cwist_app *app)
 Create a test HTTP client bound to an app.
 
void cwist_test_client_destroy (cwist_test_client *client)
 Destroy a test client and free all its cookies.
 
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)
 Send a request with explicit method and options.
 
cwist_http_response * cwist_test_client_get (cwist_test_client *client, const char *path)
 Send a GET request.
 
cwist_http_response * cwist_test_client_post (cwist_test_client *client, const char *path, const char *body)
 Send a POST request with a plain-text body.
 
cwist_http_response * cwist_test_client_post_json (cwist_test_client *client, const char *path, const char *json_body)
 Send a POST request with a JSON body and application/json content type.
 
cwist_http_response * cwist_test_client_put (cwist_test_client *client, const char *path, const char *body)
 Send a PUT request with a plain-text body.
 
cwist_http_response * cwist_test_client_delete (cwist_test_client *client, const char *path)
 Send a DELETE request.
 
cwist_http_response * cwist_test_client_patch (cwist_test_client *client, const char *path, const char *body)
 Send a PATCH request with a plain-text body.
 
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)
 Send a POST request with a single-file multipart/form-data body. Builds the multipart body with a fixed boundary, including Content-Disposition and Content-Type headers for the file part. The body buffer is freed before returning.
 
void cwist_test_client_set_cookie (cwist_test_client *client, const char *name, const char *value, const char *path)
 Set or replace a cookie in the client's jar. A cookie with the same name gets its value replaced (and path, if path is given); otherwise a new cookie is prepended to the jar. Strings are copied.
 
const char * cwist_test_client_get_cookie (cwist_test_client *client, const char *name)
 Get the value of a cookie from the client's jar.
 
void cwist_test_client_clear_cookies (cwist_test_client *client)
 Remove and free all cookies in the client's jar.
 

Detailed Description

test_client.c interface.

Definition in file test_client.c.

Function Documentation

◆ cwist_test_client_clear_cookies()

void cwist_test_client_clear_cookies ( cwist_test_client *  client)

Remove and free all cookies in the client's 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 HTTP client bound to 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 
)

Send 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 and free all its cookies.

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 
)

Send 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 the value of a cookie from the client's jar.

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 
)

Send a PATCH request with a plain-text body.

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 
)

Send a POST request with a plain-text body.

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 
)

Send a POST request with a JSON body and application/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 
)

Send a POST request with a single-file multipart/form-data body. Builds the multipart body with a fixed boundary, including Content-Disposition and Content-Type headers for the file part. The body buffer is freed before returning.

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 
)

Send a PUT request with a plain-text body.

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 
)

Send a request with explicit method and options.

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 or replace a cookie in the client's jar. A cookie with the same name gets its value replaced (and path, if path is given); otherwise a new cookie is prepended to the jar. Strings are copied.

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().