Skip to content

Commit f2c0fa8

Browse files
pwhelanedsiper
authored andcommitted
in_http: add tests for 400 responses to bad requests and buffer write errors.
Signed-off-by: Phillip Whelan <[email protected]>
1 parent 61d97c4 commit f2c0fa8

File tree

1 file changed

+151
-2
lines changed

1 file changed

+151
-2
lines changed

tests/runtime/in_http.c

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ void flb_test_http()
277277
flb_upstream_conn_release(ctx->httpc->u_conn);
278278
test_ctx_destroy(ctx);
279279
}
280-
281280
void flb_test_http_successful_response_code(char *response_code)
282281
{
283282
struct flb_lib_out_cb cb_data;
@@ -361,6 +360,155 @@ void flb_test_http_successful_response_code_204()
361360
flb_test_http_successful_response_code("204");
362361
}
363362

363+
void flb_test_http_failure_400_bad_json() {
364+
struct flb_lib_out_cb cb_data;
365+
struct test_ctx *ctx;
366+
struct flb_http_client *c;
367+
int ret;
368+
size_t b_sent;
369+
370+
char *buf = "\"INVALIDJSON";
371+
372+
clear_output_num();
373+
374+
cb_data.cb = cb_check_result_json;
375+
cb_data.data = "\"test\":\"msg\"";
376+
377+
ctx = test_ctx_create(&cb_data);
378+
if (!TEST_CHECK(ctx != NULL)) {
379+
TEST_MSG("test_ctx_create failed");
380+
exit(EXIT_FAILURE);
381+
}
382+
383+
ret = flb_input_set(ctx->flb, ctx->i_ffd, NULL);
384+
TEST_CHECK(ret == 0);
385+
386+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
387+
"match", "*",
388+
"format", "json",
389+
NULL);
390+
TEST_CHECK(ret == 0);
391+
392+
/* Start the engine */
393+
ret = flb_start(ctx->flb);
394+
TEST_CHECK(ret == 0);
395+
396+
ctx->httpc = http_client_ctx_create();
397+
TEST_CHECK(ctx->httpc != NULL);
398+
399+
c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_POST, "/", buf, strlen(buf),
400+
"127.0.0.1", 9880, NULL, 0);
401+
ret = flb_http_add_header(c, FLB_HTTP_HEADER_CONTENT_TYPE, strlen(FLB_HTTP_HEADER_CONTENT_TYPE),
402+
JSON_CONTENT_TYPE, strlen(JSON_CONTENT_TYPE));
403+
TEST_CHECK(ret == 0);
404+
if (!TEST_CHECK(c != NULL)) {
405+
TEST_MSG("http_client failed");
406+
exit(EXIT_FAILURE);
407+
}
408+
409+
ret = flb_http_do(c, &b_sent);
410+
if (!TEST_CHECK(ret == 0)) {
411+
TEST_MSG("ret error. ret=%d\n", ret);
412+
}
413+
else if (!TEST_CHECK(b_sent > 0)){
414+
TEST_MSG("b_sent size error. b_sent = %lu\n", b_sent);
415+
}
416+
else if (!TEST_CHECK(c->resp.status == 400)) {
417+
TEST_MSG("http response code error. expect: %d, got: %d\n", 400, c->resp.status);
418+
}
419+
420+
/* waiting to flush */
421+
flb_time_msleep(1500);
422+
423+
flb_http_client_destroy(c);
424+
flb_upstream_conn_release(ctx->httpc->u_conn);
425+
test_ctx_destroy(ctx);
426+
}
427+
428+
void flb_test_http_failure_400_bad_disk_write()
429+
{
430+
struct flb_lib_out_cb cb_data;
431+
struct test_ctx *ctx;
432+
struct flb_http_client *c;
433+
int ret;
434+
size_t b_sent;
435+
436+
char *buf = "{\"foo\": \"bar\"}";
437+
438+
clear_output_num();
439+
440+
cb_data.cb = cb_check_result_json;
441+
cb_data.data = "\"test\":\"msg\"";
442+
443+
ctx = test_ctx_create(&cb_data);
444+
if (!TEST_CHECK(ctx != NULL)) {
445+
TEST_MSG("test_ctx_create failed");
446+
exit(EXIT_FAILURE);
447+
}
448+
449+
ret = flb_service_set(ctx->flb,
450+
"storage.path", "/tmp/http-input-test-404-bad-write",
451+
NULL);
452+
TEST_CHECK(ret == 0);
453+
454+
ret = flb_input_set(ctx->flb, ctx->i_ffd,
455+
"storage.type", "filesystem",
456+
NULL);
457+
TEST_CHECK(ret == 0);
458+
459+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
460+
"match", "*",
461+
"format", "json",
462+
NULL);
463+
TEST_CHECK(ret == 0);
464+
465+
/* Start the engine */
466+
ret = flb_start(ctx->flb);
467+
TEST_CHECK(ret == 0);
468+
469+
flb_time_msleep(5000);
470+
471+
ret = chmod("/tmp/http-input-test-404-bad-write", 000);
472+
TEST_CHECK(ret == 0);
473+
474+
ctx->httpc = http_client_ctx_create();
475+
TEST_CHECK(ctx->httpc != NULL);
476+
477+
c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_POST, "/", buf, strlen(buf),
478+
"127.0.0.1", 9880, NULL, 0);
479+
ret = flb_http_add_header(c, FLB_HTTP_HEADER_CONTENT_TYPE, strlen(FLB_HTTP_HEADER_CONTENT_TYPE),
480+
JSON_CONTENT_TYPE, strlen(JSON_CONTENT_TYPE));
481+
TEST_CHECK(ret == 0);
482+
if (!TEST_CHECK(c != NULL)) {
483+
TEST_MSG("http_client failed");
484+
exit(EXIT_FAILURE);
485+
}
486+
487+
ret = flb_http_do(c, &b_sent);
488+
if (!TEST_CHECK(ret == 0)) {
489+
TEST_MSG("ret error. ret=%d\n", ret);
490+
}
491+
else if (!TEST_CHECK(b_sent > 0)){
492+
TEST_MSG("b_sent size error. b_sent = %lu\n", b_sent);
493+
}
494+
else if (!TEST_CHECK(c->resp.status == 400)) {
495+
TEST_MSG("http response code error. expect: %d, got: %d\n", 400, c->resp.status);
496+
}
497+
498+
chmod("/tmp/http-input-test-404-bad-write/http.0", 0700);
499+
rmdir("/tmp/http-input-test-404-bad-write/http.0");
500+
501+
chmod("/tmp/http-input-test-404-bad-write", 0700);
502+
rmdir("/tmp/http-input-test-404-bad-write");
503+
504+
/* waiting to flush */
505+
flb_time_msleep(1500);
506+
507+
flb_http_client_destroy(c);
508+
flb_upstream_conn_release(ctx->httpc->u_conn);
509+
test_ctx_destroy(ctx);
510+
}
511+
364512
void flb_test_http_tag_key()
365513
{
366514
struct flb_lib_out_cb cb_data;
@@ -438,7 +586,8 @@ TEST_LIST = {
438586
{"http", flb_test_http},
439587
{"successful_response_code_200", flb_test_http_successful_response_code_200},
440588
{"successful_response_code_204", flb_test_http_successful_response_code_204},
589+
{"failure_response_code_400_bad_json", flb_test_http_failure_400_bad_json},
590+
{"failure_response_code_400_bad_disk_write", flb_test_http_failure_400_bad_disk_write},
441591
{"tag_key", flb_test_http_tag_key},
442592
{NULL, NULL}
443593
};
444-

0 commit comments

Comments
 (0)