chore: add CMake test infrastructure and placeholder unit test files for future coverage
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
#include "../../test_framework.h"
|
||||
#include "../http_endpoints.h"
|
||||
|
||||
TEST_SUITE(http_head)
|
||||
|
||||
TEST(head_existing_file) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
const char *content = "Test content for HEAD";
|
||||
storage_write_file(ctx->fixture->storage, "/test.txt", content, strlen(content));
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/test.txt", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
ASSERT_TRUE(ctx->response.body == NULL || ctx->response.body_len == 0);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_includes_content_length) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
const char *content = "exactly 25 bytes of data!";
|
||||
storage_write_file(ctx->fixture->storage, "/sized.txt", content, strlen(content));
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/sized.txt", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
const char *cl = endpoint_response_get_header(ctx, "Content-Length");
|
||||
ASSERT_NOT_NULL(cl);
|
||||
ASSERT_EQUAL(25, atoi(cl));
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_includes_content_type) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
storage_write_file(ctx->fixture->storage, "/page.html", "<html></html>", 13);
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/page.html", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
ASSERT_TRUE(endpoint_response_has_header(ctx, "Content-Type", NULL));
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_not_found) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/nonexistent.txt", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_404_NOT_FOUND, ctx->response.status);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_collection) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
storage_create_collection(ctx->fixture->storage, "/folder");
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/folder", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
ASSERT_TRUE(ctx->response.body == NULL || ctx->response.body_len == 0);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_root) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_includes_etag) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
storage_write_file(ctx->fixture->storage, "/etag.txt", "content", 7);
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/etag.txt", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
ASSERT_TRUE(endpoint_response_has_header(ctx, "ETag", NULL));
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(head_includes_last_modified) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
storage_write_file(ctx->fixture->storage, "/modified.txt", "data", 4);
|
||||
|
||||
endpoint_test_execute(ctx, "HEAD", "/modified.txt", NULL, 0);
|
||||
|
||||
ASSERT_EQUAL(HTTP_200_OK, ctx->response.status);
|
||||
ASSERT_TRUE(endpoint_response_has_header(ctx, "Last-Modified", NULL));
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
#include "../../test_framework.h"
|
||||
#include "../http_endpoints.h"
|
||||
|
||||
TEST_SUITE(http_post)
|
||||
|
||||
TEST(post_not_implemented) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
endpoint_test_execute(ctx, "POST", "/", "data=test", 9);
|
||||
|
||||
ASSERT_TRUE(ctx->response.status == HTTP_405_METHOD_NOT_ALLOWED ||
|
||||
ctx->response.status == HTTP_501_NOT_IMPLEMENTED);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(post_to_file) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
storage_write_file(ctx->fixture->storage, "/file.txt", "content", 7);
|
||||
|
||||
endpoint_test_execute(ctx, "POST", "/file.txt", "posted=data", 11);
|
||||
|
||||
ASSERT_TRUE(ctx->response.status == HTTP_405_METHOD_NOT_ALLOWED ||
|
||||
ctx->response.status == HTTP_501_NOT_IMPLEMENTED);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(post_to_collection) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
storage_create_collection(ctx->fixture->storage, "/folder");
|
||||
|
||||
endpoint_test_execute(ctx, "POST", "/folder", "data", 4);
|
||||
|
||||
ASSERT_TRUE(ctx->response.status == HTTP_405_METHOD_NOT_ALLOWED ||
|
||||
ctx->response.status == HTTP_501_NOT_IMPLEMENTED);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(post_with_content_type) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
http_request_init(&ctx->request);
|
||||
strncpy(ctx->request.method, "POST", sizeof(ctx->request.method) - 1);
|
||||
strncpy(ctx->request.uri, "/", sizeof(ctx->request.uri) - 1);
|
||||
http_request_add_header(&ctx->request, "Content-Type", "application/x-www-form-urlencoded");
|
||||
ctx->request.body = strdup("key=value");
|
||||
ctx->request.body_len = 9;
|
||||
ctx->request.content_length = 9;
|
||||
|
||||
http_response_init(&ctx->response);
|
||||
webdav_handle_request(ctx->fixture->storage, ctx->fixture->locks,
|
||||
ctx->fixture->props, &ctx->request, &ctx->response);
|
||||
|
||||
ASSERT_TRUE(ctx->response.status == HTTP_405_METHOD_NOT_ALLOWED ||
|
||||
ctx->response.status == HTTP_501_NOT_IMPLEMENTED);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
TEST(post_empty_body) {
|
||||
endpoint_test_ctx_t *ctx = endpoint_test_create();
|
||||
ASSERT_NOT_NULL(ctx);
|
||||
|
||||
endpoint_test_execute(ctx, "POST", "/", NULL, 0);
|
||||
|
||||
ASSERT_TRUE(ctx->response.status == HTTP_405_METHOD_NOT_ALLOWED ||
|
||||
ctx->response.status == HTTP_501_NOT_IMPLEMENTED);
|
||||
|
||||
endpoint_test_destroy(ctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user