diff --git a/gateleen-hook/src/main/java/org/swisspush/gateleen/hook/HookHandler.java b/gateleen-hook/src/main/java/org/swisspush/gateleen/hook/HookHandler.java index 1957e2b9..49e30042 100755 --- a/gateleen-hook/src/main/java/org/swisspush/gateleen/hook/HookHandler.java +++ b/gateleen-hook/src/main/java/org/swisspush/gateleen/hook/HookHandler.java @@ -583,10 +583,10 @@ public boolean handle(final RoutingContext ctx) { String queryParam = request.getParam("q"); // 2. Check if the URI is for listeners or routes and has a query parameter if (queryParam != null && !queryParam.isEmpty()) { - if (uri.contains(HOOK_LISTENER_STORAGE_PATH)) { + if (uri.contains(LISTENERS_KEY)) { handleListenerSearch(queryParam, request.response()); return true; - } else if (uri.contains(HOOK_ROUTE_STORAGE_PATH)) { + } else if (uri.contains(ROUTES_KEY)) { handleRouteSearch(queryParam, request.response()); return true; } @@ -649,11 +649,15 @@ private void handleSearch(Map repository, Function get JsonObject result = new JsonObject(); result.put(resultKey, matchingResults); - // Set headers safely before writing the response + String encodedResult = result.encode(); // Convert the result to a string + + // Set Content-Length header before sending the response response.putHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON); - response.write(result.encode()); - response.end(); + response.putHeader("Content-Length", String.valueOf(encodedResult.length())); // Set content length + // Write and end the response + response.write(encodedResult); + response.end(); } /** diff --git a/gateleen-test/src/test/java/org/swisspush/gateleen/hook/ListenerTest.java b/gateleen-test/src/test/java/org/swisspush/gateleen/hook/ListenerTest.java index 06dc038d..7cdba97f 100755 --- a/gateleen-test/src/test/java/org/swisspush/gateleen/hook/ListenerTest.java +++ b/gateleen-test/src/test/java/org/swisspush/gateleen/hook/ListenerTest.java @@ -995,10 +995,12 @@ public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestConte // Send GET request with non-matching query param given().queryParam("q", nonMatchingQueryParam) .when().get(requestUrl) - .then().assertThat().statusCode(404); // Expecting 404 as no listener matches the query + .then().assertThat() + .statusCode(200) // Expecting 200 as the request is valid but no match found + .body("listeners", org.hamcrest.Matchers.empty()); // Expecting an empty list of listeners - // Validate that the response is not found - checkGETStatusCodeWithAwait(requestUrl, 404); + // Validate that the response is 200 and the result is an empty array + checkGETStatusCodeWithAwait(requestUrl, 200); // Unregister the listener TestUtils.unregisterListener(requestUrlBase + listenerPath); @@ -1006,4 +1008,34 @@ public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestConte async.complete(); } + /** + * Test for hookHandleSearch with listener storage path and valid query param but no listeners registered.
+ * eg. register / unregister: http://localhost:7012/gateleen/server/listenertest/_hooks/listeners/listener/1
+ * requestUrl: http://localhost:7012/gateleen/server/listenertest/listener/test?q=someQuery + */ + @Test + public void testHookHandleSearch_NoListenersRegistered(TestContext context) { + Async async = context.async(); + delete(); + initRoutingRules(); + + String queryParam = "someQuery"; + String listenerPath = "/_hooks/listeners"; + String requestUrl = requestUrlBase + listenerPath + "?q=" + queryParam; + + // No listeners registered + + // Send GET request with a query param + given().queryParam("q", queryParam) + .when().get(requestUrl) + .then().assertThat() + .statusCode(200) // Expecting 200 as the request is valid but no listeners are registered + .body("listeners", org.hamcrest.Matchers.empty()); // Expecting an empty list of listeners + + // Validate that the response is 200 and the result is an empty array + checkGETStatusCodeWithAwait(requestUrl, 200); + + async.complete(); + } + }