Skip to content

Commit

Permalink
in_http: add tests for 400 responses to bad requests and buffer write…
Browse files Browse the repository at this point in the history
… errors.

Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan authored and edsiper committed May 29, 2024
1 parent 61d97c4 commit f2c0fa8
Showing 1 changed file with 151 additions and 2 deletions.
153 changes: 151 additions & 2 deletions tests/runtime/in_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ void flb_test_http()
flb_upstream_conn_release(ctx->httpc->u_conn);
test_ctx_destroy(ctx);
}

void flb_test_http_successful_response_code(char *response_code)
{
struct flb_lib_out_cb cb_data;
Expand Down Expand Up @@ -361,6 +360,155 @@ void flb_test_http_successful_response_code_204()
flb_test_http_successful_response_code("204");
}

void flb_test_http_failure_400_bad_json() {
struct flb_lib_out_cb cb_data;
struct test_ctx *ctx;
struct flb_http_client *c;
int ret;
size_t b_sent;

char *buf = "\"INVALIDJSON";

clear_output_num();

cb_data.cb = cb_check_result_json;
cb_data.data = "\"test\":\"msg\"";

ctx = test_ctx_create(&cb_data);
if (!TEST_CHECK(ctx != NULL)) {
TEST_MSG("test_ctx_create failed");
exit(EXIT_FAILURE);
}

ret = flb_input_set(ctx->flb, ctx->i_ffd, NULL);
TEST_CHECK(ret == 0);

ret = flb_output_set(ctx->flb, ctx->o_ffd,
"match", "*",
"format", "json",
NULL);
TEST_CHECK(ret == 0);

/* Start the engine */
ret = flb_start(ctx->flb);
TEST_CHECK(ret == 0);

ctx->httpc = http_client_ctx_create();
TEST_CHECK(ctx->httpc != NULL);

c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_POST, "/", buf, strlen(buf),
"127.0.0.1", 9880, NULL, 0);
ret = flb_http_add_header(c, FLB_HTTP_HEADER_CONTENT_TYPE, strlen(FLB_HTTP_HEADER_CONTENT_TYPE),
JSON_CONTENT_TYPE, strlen(JSON_CONTENT_TYPE));
TEST_CHECK(ret == 0);
if (!TEST_CHECK(c != NULL)) {
TEST_MSG("http_client failed");
exit(EXIT_FAILURE);
}

ret = flb_http_do(c, &b_sent);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("ret error. ret=%d\n", ret);
}
else if (!TEST_CHECK(b_sent > 0)){
TEST_MSG("b_sent size error. b_sent = %lu\n", b_sent);
}
else if (!TEST_CHECK(c->resp.status == 400)) {
TEST_MSG("http response code error. expect: %d, got: %d\n", 400, c->resp.status);
}

/* waiting to flush */
flb_time_msleep(1500);

flb_http_client_destroy(c);
flb_upstream_conn_release(ctx->httpc->u_conn);
test_ctx_destroy(ctx);
}

void flb_test_http_failure_400_bad_disk_write()
{
struct flb_lib_out_cb cb_data;
struct test_ctx *ctx;
struct flb_http_client *c;
int ret;
size_t b_sent;

char *buf = "{\"foo\": \"bar\"}";

clear_output_num();

cb_data.cb = cb_check_result_json;
cb_data.data = "\"test\":\"msg\"";

ctx = test_ctx_create(&cb_data);
if (!TEST_CHECK(ctx != NULL)) {
TEST_MSG("test_ctx_create failed");
exit(EXIT_FAILURE);
}

ret = flb_service_set(ctx->flb,
"storage.path", "/tmp/http-input-test-404-bad-write",
NULL);
TEST_CHECK(ret == 0);

ret = flb_input_set(ctx->flb, ctx->i_ffd,
"storage.type", "filesystem",
NULL);
TEST_CHECK(ret == 0);

ret = flb_output_set(ctx->flb, ctx->o_ffd,
"match", "*",
"format", "json",
NULL);
TEST_CHECK(ret == 0);

/* Start the engine */
ret = flb_start(ctx->flb);
TEST_CHECK(ret == 0);

flb_time_msleep(5000);

ret = chmod("/tmp/http-input-test-404-bad-write", 000);
TEST_CHECK(ret == 0);

ctx->httpc = http_client_ctx_create();
TEST_CHECK(ctx->httpc != NULL);

c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_POST, "/", buf, strlen(buf),
"127.0.0.1", 9880, NULL, 0);
ret = flb_http_add_header(c, FLB_HTTP_HEADER_CONTENT_TYPE, strlen(FLB_HTTP_HEADER_CONTENT_TYPE),
JSON_CONTENT_TYPE, strlen(JSON_CONTENT_TYPE));
TEST_CHECK(ret == 0);
if (!TEST_CHECK(c != NULL)) {
TEST_MSG("http_client failed");
exit(EXIT_FAILURE);
}

ret = flb_http_do(c, &b_sent);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("ret error. ret=%d\n", ret);
}
else if (!TEST_CHECK(b_sent > 0)){
TEST_MSG("b_sent size error. b_sent = %lu\n", b_sent);
}
else if (!TEST_CHECK(c->resp.status == 400)) {
TEST_MSG("http response code error. expect: %d, got: %d\n", 400, c->resp.status);
}

chmod("/tmp/http-input-test-404-bad-write/http.0", 0700);
rmdir("/tmp/http-input-test-404-bad-write/http.0");

chmod("/tmp/http-input-test-404-bad-write", 0700);
rmdir("/tmp/http-input-test-404-bad-write");

/* waiting to flush */
flb_time_msleep(1500);

flb_http_client_destroy(c);
flb_upstream_conn_release(ctx->httpc->u_conn);
test_ctx_destroy(ctx);
}

void flb_test_http_tag_key()
{
struct flb_lib_out_cb cb_data;
Expand Down Expand Up @@ -438,7 +586,8 @@ TEST_LIST = {
{"http", flb_test_http},
{"successful_response_code_200", flb_test_http_successful_response_code_200},
{"successful_response_code_204", flb_test_http_successful_response_code_204},
{"failure_response_code_400_bad_json", flb_test_http_failure_400_bad_json},
{"failure_response_code_400_bad_disk_write", flb_test_http_failure_400_bad_disk_write},
{"tag_key", flb_test_http_tag_key},
{NULL, NULL}
};

0 comments on commit f2c0fa8

Please sign in to comment.