Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception when route does not finish, reroute, or next #5834

Merged
merged 4 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -248,7 +248,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "HttpPrologueRecord["
return "HttpPrologue["
+ "protocol=" + protocol + ", "
+ "protocolVersion=" + protocolVersion + ", "
+ "method=" + method + ", "
Expand Down
5 changes: 5 additions & 0 deletions microprofile/lra/jax-rs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>helidon-nima-webclient</artifactId>
<groupId>io.helidon.nima.webclient</groupId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
import io.helidon.microprofile.tests.junit5.AddExtension;
import io.helidon.microprofile.tests.junit5.DisableDiscovery;
import io.helidon.microprofile.tests.junit5.HelidonTest;
import io.helidon.nima.webclient.http1.Http1Client;
import io.helidon.nima.webclient.http1.Http1ClientResponse;
import io.helidon.nima.webserver.http.HttpService;
import io.helidon.reactive.webclient.WebClient;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -104,6 +105,7 @@ class CoordinatorHeaderPropagationTest {
private static final String PROPAGATED_HEADER = "xxx-tmm-propagated-header";
private static final String EXTRA_COORDINATOR_PROPAGATED_HEADER = "xBb-tmm-extra-start-header";
private static final String NOT_PROPAGATED_HEADER = "non-propagated-header";
private static final Http.HeaderName LRA_HTTP_CONTEXT_HEADER_NAME = Http.Header.create(LRA_HTTP_CONTEXT_HEADER);

private static volatile int port = -1;

Expand Down Expand Up @@ -135,14 +137,14 @@ HttpService mockCoordinator() {
.post("/start", (req, res) -> {
startHeadersCoordinator.putAll(req.headers().toMap());
String lraId = URI.create("http://localhost:"
+ port
+ "/lra-coordinator/xxx-xxx-"
+ lraIndex.incrementAndGet()).toASCIIString();
+ port
+ "/lra-coordinator/xxx-xxx-"
+ lraIndex.incrementAndGet()).toASCIIString();

lraMap.put(lraId, new ConcurrentHashMap<>());

res.status(Http.Status.CREATED_201)
.header(LRA_HTTP_CONTEXT_HEADER, lraId)
.header(LRA_HTTP_CONTEXT_HEADER_NAME, lraId)
.header(NOT_PROPAGATED_HEADER, "not this extra one!")
.header(EXTRA_COORDINATOR_PROPAGATED_HEADER, "yes extra start header!")
.send();
Expand All @@ -160,60 +162,68 @@ HttpService mockCoordinator() {
//no complete resource
// after lra
if (lraMap.get(lraId).get("after") != null) {
WebClient.builder()
try (Http1ClientResponse clientResponse = Http1Client.builder()
.baseUri(lraMap.get(lraId).get("after").toASCIIString())
.build()
.put()
.addHeader(LRA_HTTP_CONTEXT_HEADER, lraId)
.method(Http.Method.PUT)
.header(LRA_HTTP_CONTEXT_HEADER_NAME, lraId)
.headers(reqHeaders -> {
// relay all incoming headers
req.headers().forEach(reqHeaders::add);
return reqHeaders;
})
.submit(LRAStatus.Closing.name())
.onError(res::send)
.forSingle(wcr2 -> {
res.send();
});
.submit(LRAStatus.Closing.name())) {
if (clientResponse.status().family() != Http.Status.Family.SUCCESSFUL) {
res.status(clientResponse.status());
}
res.send();
}
} else {
res.send();
}

return;
}

WebClient.builder()
try (Http1ClientResponse clientResponse = Http1Client.builder()
.baseUri(lraMap.get(lraId).get("complete").toASCIIString())
.build()
.put()
.addHeader(LRA_HTTP_CONTEXT_HEADER, lraId)
.method(Http.Method.PUT)
.header(LRA_HTTP_CONTEXT_HEADER_NAME, lraId)
.headers(reqHeaders -> {
// relay all incoming headers
req.headers().forEach(reqHeaders::add);
return reqHeaders;
})
.submit()
.onError(res::send)
.forSingle(wcr1 -> res.send());
.request()) {
if (clientResponse.status().family() != Http.Status.Family.SUCCESSFUL) {
res.status(clientResponse.status());
}
res.send();
}
})
.put("/{lraId}/cancel", (req, res) -> {
closeHeadersCoordinator.putAll(req.headers().toMap());
String lraId = "http://localhost:" + port + "/lra-coordinator/" + req.path()
.pathParameters()
.value("lraId");
WebClient.builder()

try (Http1ClientResponse clientResponse = Http1Client.builder()
.baseUri(lraMap.get(lraId).get("compensate").toASCIIString())
.build()
.put()
.addHeader(LRA_HTTP_CONTEXT_HEADER, lraId)
.method(Http.Method.PUT)
.header(LRA_HTTP_CONTEXT_HEADER_NAME, lraId)
.headers(reqHeaders -> {
// relay all incoming headers
req.headers().forEach(reqHeaders::add);
return reqHeaders;
})
.submit()
.onError(res::send)
.forSingle(wcResponse -> res.send());
.request()) {
if (clientResponse.status().family() != Http.Status.Family.SUCCESSFUL) {
res.status(clientResponse.status());
}
res.send();
}
})
//join
.put("/{lraId}", (req, res) -> {
Expand All @@ -232,6 +242,7 @@ HttpService mockCoordinator() {
lraMap.get(lraId).put(uriType.trim(), uri);

}
res.send();
});
}

Expand All @@ -241,6 +252,7 @@ private void ready(
@Initialized(ApplicationScoped.class) Object event,
BeanManager bm) {
port = bm.getExtension(ServerCdiExtension.class).port();

// Provide LRA client with coordinator loadbalancer url
coordinatorLocatorService.overrideCoordinatorUriSupplier(() ->
URI.create("http://localhost:" + port + "/lra-coordinator"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,8 +24,10 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.function.Function;

import io.helidon.common.buffers.BufferData;
import io.helidon.common.http.ClientRequestHeaders;
import io.helidon.common.http.Http;
import io.helidon.common.http.Http.Header;
import io.helidon.common.http.Http.HeaderValue;
Expand All @@ -42,7 +44,7 @@
class ClientRequestImpl implements Http2ClientRequest {
private static final Map<ConnectionKey, Http2ClientConnectionHandler> CHANNEL_CACHE = new ConcurrentHashMap<>();

private final WritableHeaders<?> explicitHeaders = WritableHeaders.create();
private WritableHeaders<?> explicitHeaders = WritableHeaders.create();

private final Http2ClientImpl client;

Expand Down Expand Up @@ -89,6 +91,12 @@ public Http2ClientRequest header(HeaderValue header) {
return this;
}

@Override
public Http2ClientRequest headers(Function<ClientRequestHeaders, WritableHeaders<?>> headersConsumer) {
this.explicitHeaders = headersConsumer.apply(ClientRequestHeaders.create(explicitHeaders));
return this;
}

@Override
public Http2ClientRequest pathParam(String name, String value) {
throw new UnsupportedOperationException("Not implemented");
Expand Down Expand Up @@ -118,7 +126,7 @@ public Http2ClientResponse submit(Object entity) {
} else {
entityBytes = entityBytes(entity);
}
headers.setIfAbsent(Header.create(Header.CONTENT_LENGTH, String.valueOf(entityBytes.length)));
headers.set(Header.create(Header.CONTENT_LENGTH, entityBytes.length));

Http2Headers http2Headers = prepareHeaders(headers);
stream.write(http2Headers, entityBytes.length == 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,11 +22,13 @@
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

import io.helidon.common.http.Http;
import io.helidon.common.testing.http.junit5.SocketHttpClient;
import io.helidon.nima.testing.junit5.webserver.ServerTest;
import io.helidon.nima.testing.junit5.webserver.SetUpRoute;
import io.helidon.nima.webclient.http1.Http1Client;
Expand All @@ -44,16 +46,19 @@
import static io.helidon.common.testing.junit5.MatcherWithRetry.assertThatWithRetry;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;

@ServerTest
class AccessLogTest {
private static final AtomicReference<MemoryLogHandler> LOG_HANDLER = new AtomicReference<>();

private final Http1Client client;
private final SocketHttpClient socketClient;

AccessLogTest(Http1Client client) {
AccessLogTest(Http1Client client, SocketHttpClient socketClient) {
this.client = client;
this.socketClient = socketClient;
}

@SetUpRoute
Expand Down Expand Up @@ -83,10 +88,11 @@ void testRequestsAndValidateAccessLog() {
response = client.get("/wrong").request();
assertThat(response.status(), is(Http.Status.NOT_FOUND_404));

response = client.get("/access")
.header(Http.Header.create(Http.Header.CONTENT_LENGTH, "47a"))
.request();
assertThat(response.status(), is(Http.Status.BAD_REQUEST_400));
String socketResponse = socketClient.sendAndReceive("/access",
Http.Method.GET,
null,
List.of("Content-Length: 47a"));
assertThat(socketResponse, startsWith("HTTP/1.1 " + Http.Status.BAD_REQUEST_400.text()));

// Use retry since no happens-before relationship between log entry and assertion
assertThatWithRetry("Check log entry for /access exist",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2022 Oracle and/or its affiliates.
# Copyright (c) 2022, 2023 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@ java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tH:%1$tM:%1$tS %4$s %3$s %5$s%6$s%n
# Global logging level. Can be overridden by specific loggers
.level=INFO
io.helidon.nima.level=FINEST

io.helidon.nima.webserver.accesslog.MemoryLogHandler.level=FINEST
io.helidon.nima.webserver.accesslog.MemoryLogHandler.append=false
io.helidon.nima.webserver.AccessLog.level=INFO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -77,10 +77,9 @@ static void startServer(HttpRules rules) {
@Test
void testContentLengthExceeded() {
try (Http1ClientResponse response = client.method(Http.Method.POST)
.header(Header.CONTENT_LENGTH, "512")
.path("/maxpayload")
.header(HeaderValues.CONTENT_TYPE_OCTET_STREAM)
.request()) {
.submit(new byte[512])) {
assertThat(response.status(), is(Http.Status.REQUEST_ENTITY_TOO_LARGE_413));
assertThat(response.headers(), hasHeader(HeaderValues.CONNECTION_CLOSE));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.tests.integration.server;

import io.helidon.common.http.Http;
import io.helidon.nima.testing.junit5.webserver.DirectClient;
import io.helidon.nima.testing.junit5.webserver.RoutingTest;
import io.helidon.nima.testing.junit5.webserver.SetUpRoute;
import io.helidon.nima.webclient.http1.Http1ClientResponse;
import io.helidon.nima.webserver.http.Handler;
import io.helidon.nima.webserver.http.HttpRules;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@RoutingTest
class UnsentResponseTest {
@SetUpRoute
static void routing(HttpRules rules) {
rules.get("/no-response", new NoResponseHandler())
.get("/response", (req, res) -> {
res.send();
});
}

@Test
void testUnsentResponseThrowsException(DirectClient client) {
try (Http1ClientResponse response = client.get("/no-response")
.request()) {

assertThat(response.status(), is(Http.Status.INTERNAL_SERVER_ERROR_500));
assertThat(response.entity().as(String.class), is("Internal Server Error"));
}
}

@Test
void testNormalResponse(DirectClient client) {
try (Http1ClientResponse response = client.get("/response")
.request()) {

assertThat(response.status(), is(Http.Status.OK_200));
}
}

// to have a bit nicer error output for the internal server error
private static final class NoResponseHandler implements Handler {
@Override
public void handle(ServerRequest req, ServerResponse res) {
}

@Override
public String toString() {
return "NoResponseHandler";
}
}
}
Loading