Skip to content

Commit

Permalink
Add tests for hookHandleSearch with no matching listeners and no list…
Browse files Browse the repository at this point in the history
…eners registered.

- Test for handling searches with no matching listeners, returning an empty list.
- Test for handling searches when no listeners are registered, ensuring an empty list is returned.
- Fix hookHandler Search
  • Loading branch information
steniobhz committed Oct 11, 2024
1 parent a0161a2 commit 322508f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -649,11 +649,15 @@ private <T> void handleSearch(Map<String, T> repository, Function<T, String> 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,15 +995,47 @@ 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);

async.complete();
}

/**
* Test for hookHandleSearch with listener storage path and valid query param but no listeners registered. <br />
* eg. register / unregister: http://localhost:7012/gateleen/server/listenertest/_hooks/listeners/listener/1 <br />
* 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();
}

}

0 comments on commit 322508f

Please sign in to comment.