diff --git a/flecs.c b/flecs.c index 4c3e37f64..017d0944b 100644 --- a/flecs.c +++ b/flecs.c @@ -25980,6 +25980,7 @@ typedef struct ecs_http_request_key_t { typedef struct ecs_http_request_entry_t { char *content; int32_t content_length; + int code; ecs_ftime_t time; } ecs_http_request_entry_t; @@ -26437,6 +26438,7 @@ void http_insert_request_entry( entry->time = (ecs_ftime_t)ecs_time_measure(&t); entry->content_length = ecs_strbuf_written(&reply->body); entry->content = ecs_strbuf_get(&reply->body); + entry->code = reply->code; ecs_strbuf_appendstrn(&reply->body, entry->content, entry->content_length); } @@ -26927,7 +26929,7 @@ void http_recv_connection( if (entry) { ecs_http_reply_t reply; reply.body = ECS_STRBUF_INIT; - reply.code = 200; + reply.code = entry->code; reply.content_type = "application/json"; reply.headers = ECS_STRBUF_INIT; reply.status = "OK"; @@ -27504,7 +27506,7 @@ int ecs_http_server_http_request( http_find_request_entry(srv, request.res, request.req_len); if (entry) { reply_out->body = ECS_STRBUF_INIT; - reply_out->code = 200; + reply_out->code = entry->code; reply_out->content_type = "application/json"; reply_out->headers = ECS_STRBUF_INIT; reply_out->status = "OK"; @@ -34501,6 +34503,7 @@ void flecs_rest_reply_set_captured_log( { char *err = flecs_rest_get_captured_log(); if (err) { + printf("ERROR!\n"); char *escaped_err = ecs_astresc('"', err); flecs_reply_error(reply, escaped_err); reply->code = 400; @@ -34529,7 +34532,6 @@ int flecs_rest_iter_to_reply( if (offset < 0 || limit < 0) { flecs_reply_error(reply, "invalid offset/limit parameter"); - reply->code = 400; return -1; } diff --git a/src/addons/http.c b/src/addons/http.c index 6650d023a..b466e71ea 100644 --- a/src/addons/http.c +++ b/src/addons/http.c @@ -133,6 +133,7 @@ typedef struct ecs_http_request_key_t { typedef struct ecs_http_request_entry_t { char *content; int32_t content_length; + int code; ecs_ftime_t time; } ecs_http_request_entry_t; @@ -590,6 +591,7 @@ void http_insert_request_entry( entry->time = (ecs_ftime_t)ecs_time_measure(&t); entry->content_length = ecs_strbuf_written(&reply->body); entry->content = ecs_strbuf_get(&reply->body); + entry->code = reply->code; ecs_strbuf_appendstrn(&reply->body, entry->content, entry->content_length); } @@ -1080,7 +1082,7 @@ void http_recv_connection( if (entry) { ecs_http_reply_t reply; reply.body = ECS_STRBUF_INIT; - reply.code = 200; + reply.code = entry->code; reply.content_type = "application/json"; reply.headers = ECS_STRBUF_INIT; reply.status = "OK"; @@ -1657,7 +1659,7 @@ int ecs_http_server_http_request( http_find_request_entry(srv, request.res, request.req_len); if (entry) { reply_out->body = ECS_STRBUF_INIT; - reply_out->code = 200; + reply_out->code = entry->code; reply_out->content_type = "application/json"; reply_out->headers = ECS_STRBUF_INIT; reply_out->status = "OK"; diff --git a/src/addons/rest.c b/src/addons/rest.c index 1acae6e00..23bc80212 100644 --- a/src/addons/rest.c +++ b/src/addons/rest.c @@ -361,6 +361,7 @@ void flecs_rest_reply_set_captured_log( { char *err = flecs_rest_get_captured_log(); if (err) { + printf("ERROR!\n"); char *escaped_err = ecs_astresc('"', err); flecs_reply_error(reply, escaped_err); reply->code = 400; @@ -389,7 +390,6 @@ int flecs_rest_iter_to_reply( if (offset < 0 || limit < 0) { flecs_reply_error(reply, "invalid offset/limit parameter"); - reply->code = 400; return -1; } diff --git a/test/addons/project.json b/test/addons/project.json index be2ae6962..9754f4ecb 100644 --- a/test/addons/project.json +++ b/test/addons/project.json @@ -1818,7 +1818,8 @@ "testcases": [ "teardown", "get", - "get_cached" + "get_cached", + "get_cached_invalid" ] }, { "id": "Metrics", diff --git a/test/addons/src/Rest.c b/test/addons/src/Rest.c index 99b01829c..5f4be055b 100644 --- a/test/addons/src/Rest.c +++ b/test/addons/src/Rest.c @@ -71,3 +71,43 @@ void Rest_get_cached(void) { ecs_fini(world); } + +void Rest_get_cached_invalid(void) { + ecs_world_t *world = ecs_init(); + + ecs_http_server_t *srv = ecs_rest_server_init(world, + &(ecs_http_server_desc_t){ + .cache_timeout = 1.0 + }); + test_assert(srv != NULL); + + { + ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT; + test_int(-1, ecs_http_server_request(srv, "GET", + "/entity/flecs/core/Wor", &reply)); + test_int(reply.code, 404); + + char *reply_str = ecs_strbuf_get(&reply.body); + test_assert(reply_str != NULL); + test_str(reply_str, + "{\"error\":\"entity \'flecs/core/Wor\' not found\"}"); + ecs_os_free(reply_str); + } + + { + ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT; + test_int(-1, ecs_http_server_request(srv, "GET", + "/entity/flecs/core/Wor", &reply)); + test_int(reply.code, 404); + + char *reply_str = ecs_strbuf_get(&reply.body); + test_assert(reply_str != NULL); + test_str(reply_str, + "{\"error\":\"entity \'flecs/core/Wor\' not found\"}"); + ecs_os_free(reply_str); + } + + ecs_rest_server_fini(srv); + + ecs_fini(world); +} diff --git a/test/addons/src/main.c b/test/addons/src/main.c index 29460010d..a546a1ba9 100644 --- a/test/addons/src/main.c +++ b/test/addons/src/main.c @@ -1748,6 +1748,7 @@ void Http_stop_start(void); void Rest_teardown(void); void Rest_get(void); void Rest_get_cached(void); +void Rest_get_cached_invalid(void); // Testsuite 'Metrics' void Metrics_member_gauge_1_entity(void); @@ -8585,6 +8586,10 @@ bake_test_case Rest_testcases[] = { { "get_cached", Rest_get_cached + }, + { + "get_cached_invalid", + Rest_get_cached_invalid } }; @@ -9123,7 +9128,7 @@ static bake_test_suite suites[] = { "Rest", NULL, NULL, - 3, + 4, Rest_testcases }, {