Files
kryon/tests/kry_http_test.c
T
waozi a502700ed0 kry std: kry_json, kry_http, kry_zai — the AI-harness foundation
- kry_json: dependency-free recursive-descent parser (objects/arrays/
  strings with \u escapes + surrogate pairs, numbers, bools) and a
  compact KryJsonBuf emitter. Depth-capped against hostile input.
- kry_http: async libcurl-easy client (HAS_LIBCURL) on the existing
  KryThread worker pattern — POST-JSON/GET, poll for state, body owned
  by the request. Stub when curl is absent. file:// counts as success
  (HTTP code 0), which the hermetic tests use.
- kry_zai: z.ai GLM chat-completions client (OpenAI-compatible) on the
  two above. Coding-Plan endpoint default, glm-4.6, ZAI_API_KEY/
  ZAI_BASE_URL/ZAI_MODEL env with an in-process override; thinking
  disabled for fast answers. Live test gated on ZAI_API_KEY.
- mk/vendor.mk: KRYON_CURL_TRANSITIVE_LDLIBS (brotli/zstd) the vendored
  static curl needs at final link.
2026-08-14 22:06:36 -05:00

125 lines
2.9 KiB
C

/*
* kry_http_test.c - async HTTP client tests.
*
* Hermetic core: a GET against file:// exercises the worker thread, write
* callback, and poll lifecycle without a network. A live HTTPS round trip
* runs only when KRYON_HTTP_TEST_URL is set in the environment.
*/
#include "kry_http.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
static int failures;
#define CHECK(cond) do { \
if(!(cond)) { \
failures++; \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
} \
} while(0)
static KryHttpStatus
poll_until_terminal(KryHttpRequest *r, int spins)
{
KryHttpStatus s = kry_http_poll(r);
int i;
for(i = 0; i < spins && s != KRY_HTTP_DONE && s != KRY_HTTP_FAILED; i++) {
struct timespec ts = {0, 10 * 1000 * 1000};
nanosleep(&ts, NULL);
s = kry_http_poll(r);
}
return s;
}
static void
test_file_get(void)
{
char path[256];
char url[300];
FILE *f;
KryHttpRequest *r;
KryHttpStatus s;
snprintf(path, sizeof(path), "/tmp/kry_http_test.%d.txt", (int)getpid());
f = fopen(path, "wb");
if(f == NULL)
return;
fprintf(f, "hello from the filesystem");
fclose(f);
snprintf(url, sizeof(url), "file://%s", path);
r = kry_http_get(url, 10);
if(r == NULL) {
printf("kry_http unavailable (no libcurl); skipping\n");
remove(path);
return;
}
s = poll_until_terminal(r, 500);
CHECK(s == KRY_HTTP_DONE);
CHECK(kry_http_response(r) != NULL);
if(kry_http_response(r) != NULL)
CHECK(strcmp(kry_http_response(r), "hello from the filesystem") == 0);
kry_http_free(r);
r = kry_http_get("file:///tmp/definitely-not-here-kryon", 5);
if(r != NULL) {
s = poll_until_terminal(r, 200);
CHECK(s == KRY_HTTP_FAILED);
CHECK(kry_http_response(r) != NULL);
kry_http_free(r);
}
remove(path);
}
static void
test_bad_url_fails(void)
{
KryHttpRequest *r = kry_http_get("not-a-url", 5);
if(r == NULL)
return;
CHECK(poll_until_terminal(r, 300) == KRY_HTTP_FAILED);
CHECK(kry_http_response(r) != NULL);
kry_http_free(r);
}
static void
test_live_gated(void)
{
const char *url = getenv("KRYON_HTTP_TEST_URL");
KryHttpRequest *r;
KryHttpStatus s;
if(url == NULL || url[0] == '\0')
return;
r = kry_http_get(url, 30);
if(r == NULL) {
printf("kry_http unavailable; live test skipped\n");
return;
}
s = poll_until_terminal(r, 6000);
if(s != KRY_HTTP_DONE) {
printf("live test failed: %s\n",
kry_http_response(r) != NULL ? kry_http_response(r) : "?");
failures++;
}
kry_http_free(r);
}
int
main(void)
{
test_file_get();
test_bad_url_fails();
test_live_gated();
if(failures == 0)
printf("kry_http tests passed\n");
return failures == 0 ? 0 : 1;
}