use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class WebSecurityQueryParamTest method testQueryParams.
@Test
public void testQueryParams() {
SecurityHandler securityHandler = SecurityHandler.create().queryParam("jwt", TokenHandler.builder().tokenHeader("BEARER_TOKEN").tokenPattern(Pattern.compile("bearer (.*)")).build()).queryParam("name", TokenHandler.builder().tokenHeader("NAME_FROM_REQUEST").build());
ServerRequest req = Mockito.mock(ServerRequest.class);
Parameters params = Mockito.mock(Parameters.class);
when(params.all("jwt")).thenReturn(List.of("bearer jwt_content"));
when(params.all("name")).thenReturn(List.of("name_content"));
when(req.queryParams()).thenReturn(params);
SecurityContext context = Mockito.mock(SecurityContext.class);
SecurityEnvironment env = SecurityEnvironment.create();
when(context.env()).thenReturn(env);
// context is a stub
securityHandler.extractQueryParams(context, req);
// captor captures the argument
ArgumentCaptor<SecurityEnvironment> newHeaders = ArgumentCaptor.forClass(SecurityEnvironment.class);
verify(context).env(newHeaders.capture());
// now validate the value we were called with
env = newHeaders.getValue();
assertThat(env.headers().get("BEARER_TOKEN"), is(List.of("jwt_content")));
assertThat(env.headers().get("NAME_FROM_REQUEST"), is(List.of("name_content")));
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class TransactionUpdateService method executeTest.
// Common test execution code
private JsonObject executeTest(final ServerRequest request, final ServerResponse response, final String testName, final TestFunction test) {
LOGGER.fine(() -> String.format("Running SimpleUpdateService.%s on server", testName));
try {
String name = param(request, QUERY_NAME_PARAM);
String idStr = param(request, QUERY_ID_PARAM);
int id = Integer.parseInt(idStr);
Pokemon srcPokemon = Pokemon.POKEMONS.get(id);
Pokemon updatedPokemon = new Pokemon(id, name, srcPokemon.getTypesArray());
test.apply(updatedPokemon).thenAccept(result -> response.send(AppResponse.okStatus(Json.createValue(result)))).exceptionally(t -> {
response.send(exceptionStatus(t));
return null;
});
} catch (RemoteTestException | NumberFormatException ex) {
LOGGER.fine(() -> String.format("Error in SimpleUpdateService.%s on server", testName));
response.send(exceptionStatus(ex));
}
return null;
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class InitService method testInitPokemonTypes.
// Initialize pokemon types relation
private void testInitPokemonTypes(final ServerRequest request, final ServerResponse response) {
LOGGER.fine(() -> "Running InitResource.testInitPokemonTypes on server");
sendDmlResponse(response, () -> dbClient.inTransaction(tx -> {
Single<Long> stage = null;
for (Map.Entry<Integer, Pokemon> entry : Pokemon.POKEMONS.entrySet()) {
Pokemon pokemon = entry.getValue();
for (Type type : pokemon.getTypes()) {
if (stage == null) {
stage = tx.namedDml("insert-poketype", pokemon.getId(), type.getId());
} else {
stage = stage.flatMapSingle(result -> tx.namedDml("insert-poketype", pokemon.getId(), type.getId()));
}
}
}
return stage;
}).toCompletableFuture());
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class SimpleInsertService method executeTest.
// Common test execution code
private void executeTest(final ServerRequest request, final ServerResponse response, final String testName, final String pokemonName, final List<Type> pokemonTypes, final TestFunction test) {
LOGGER.fine(() -> String.format("Running SimpleInsertService.%s on server", testName));
try {
String idStr = param(request, QUERY_ID_PARAM);
int id = Integer.parseInt(idStr);
Pokemon pokemon = new Pokemon(id, pokemonName, pokemonTypes);
test.apply(pokemon).thenAccept(result -> response.send(AppResponse.okStatus(pokemon.toJsonObject()))).exceptionally(t -> {
response.send(AppResponse.exceptionStatus(t));
return null;
});
} catch (RemoteTestException | NumberFormatException ex) {
LOGGER.fine(() -> String.format("Error in SimpleInsertService.%s on server", testName));
response.send(AppResponse.exceptionStatus(ex));
}
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class GreetService method basicAuthOutbound.
private void basicAuthOutbound(ServerRequest serverRequest, ServerResponse response) {
WebClient webClient = WebClient.builder().baseUri("http://localhost:" + Main.serverPort + "/greet/secure/basic").addService(WebClientSecurity.create()).build();
webClient.get().request().thenAccept(clientResponse -> {
response.status(clientResponse.status());
response.send(clientResponse.content());
}).exceptionally(throwable -> {
response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
response.send();
return null;
});
}
Aggregations