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
feat: Add HTTP Server TCK #8499
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ea9ed84
feat: Add HTTP Server TCK
sdelamo 9dd45de
use @Suite
sdelamo 8be6232
Update build.gradle
sdelamo 7eff7d2
allow assertions
sdelamo 71632d9
annotate with @Experimiental
sdelamo ebf86e9
define SPEC_NAME constant
sdelamo 28462ad
log: remove logger
sdelamo ee76cb8
use constant
sdelamo 4a1e672
extract constants
sdelamo 68dc6e2
use consant
sdelamo 1f50932
use static methods to reduce repetition
sdelamo 1256cb7
fix chekcstyle and javadoc
sdelamo 861002d
sonar: use ifPresent and entrySet
sdelamo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
plugins { | ||
id("io.micronaut.build.internal.convention-library") | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
annotationProcessor(projects.injectJava) | ||
annotationProcessor(projects.validation) | ||
implementation(projects.validation) | ||
implementation(projects.runtime) | ||
implementation(projects.inject) | ||
api(projects.httpServer) | ||
api(libs.junit.jupiter.api) | ||
api(libs.junit.jupiter.params) | ||
api(libs.managed.reactor) | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.toVersion("1.8") | ||
targetCompatibility = JavaVersion.toVersion("1.8") | ||
} | ||
micronautBuild { | ||
binaryCompatibility { | ||
enabled.set(false) | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
http-server-tck/src/main/java/io/micronaut/http/server/tck/AssertionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright 2017-2022 original authors | ||
* | ||
* 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 | ||
* | ||
* https://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.micronaut.http.server.tck; | ||
|
||
import io.micronaut.core.annotation.Experimental; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.http.HttpHeaders; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.HttpResponse; | ||
import io.micronaut.http.HttpStatus; | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.function.Executable; | ||
import org.junit.jupiter.api.function.ThrowingSupplier; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* Utility class used to perform assertions. | ||
* @author Sergio del Amo | ||
* @since 3.8.0 | ||
*/ | ||
@SuppressWarnings({ | ||
"java:S5960", // We're allowed assertions, as these are used in tests only | ||
}) | ||
@Experimental | ||
public final class AssertionUtils { | ||
|
||
private AssertionUtils() { | ||
|
||
} | ||
|
||
public static void assertThrows(@NonNull ServerUnderTest server, | ||
@NonNull HttpRequest<?> request, | ||
@NonNull HttpResponseAssertion assertion) { | ||
assertThrows(server, request, assertion.getHttpStatus(), assertion.getBody(), assertion.getHeaders()); | ||
} | ||
|
||
public static void assertThrows(@NonNull ServerUnderTest server, | ||
@NonNull HttpRequest<?> request, | ||
@NonNull HttpStatus expectedStatus, | ||
@Nullable String expectedBody, | ||
@Nullable Map<String, String> expectedHeaders) { | ||
Executable e = expectedBody != null ? | ||
() -> server.exchange(request, String.class) : | ||
() -> server.exchange(request); | ||
HttpClientResponseException thrown = Assertions.assertThrows(HttpClientResponseException.class, e); | ||
HttpResponse<?> response = thrown.getResponse(); | ||
assertEquals(expectedStatus, response.getStatus()); | ||
assertHeaders(response, expectedHeaders); | ||
assertBody(response, expectedBody); | ||
} | ||
|
||
public static <T> void assertDoesNotThrow(@NonNull ServerUnderTest server, | ||
@NonNull HttpRequest<T> request, | ||
@NonNull HttpResponseAssertion assertion) { | ||
assertDoesNotThrow(server, request, assertion.getHttpStatus(), assertion.getBody(), assertion.getHeaders()); | ||
} | ||
|
||
public static <T> void assertDoesNotThrow(@NonNull ServerUnderTest server, | ||
@NonNull HttpRequest<T> request, | ||
@NonNull HttpStatus expectedStatus, | ||
@Nullable String expectedBody, | ||
@Nullable Map<String, String> expectedHeaders) { | ||
ThrowingSupplier<HttpResponse<?>> executable = expectedBody != null ? | ||
() -> server.exchange(request, String.class) : | ||
() -> server.exchange(request); | ||
HttpResponse<?> response = Assertions.assertDoesNotThrow(executable); | ||
assertEquals(expectedStatus, response.getStatus()); | ||
assertHeaders(response, expectedHeaders); | ||
assertBody(response, expectedBody); | ||
} | ||
|
||
private static void assertBody(@NonNull HttpResponse<?> response, @Nullable String expectedBody) { | ||
if (expectedBody != null) { | ||
Optional<String> bodyOptional = response.getBody(String.class); | ||
assertTrue(bodyOptional.isPresent()); | ||
assertTrue(bodyOptional.get().contains(expectedBody)); | ||
} | ||
} | ||
|
||
private static void assertHeaders(@NonNull HttpResponse<?> response, @Nullable Map<String, String> expectedHeaders) { | ||
|
||
if (expectedHeaders != null) { | ||
for (String headerName : expectedHeaders.keySet()) { | ||
Optional<String> headerOptional = response.getHeaders().getFirst(headerName); | ||
assertTrue(headerOptional.isPresent(), () -> "Header " + headerName + " not present"); | ||
String headerValue = headerOptional.get(); | ||
String expectedValue = expectedHeaders.get(headerName); | ||
if (headerName.equals(HttpHeaders.CONTENT_TYPE)) { | ||
if (headerValue.contains(";charset=")) { | ||
assertTrue(headerValue.startsWith(expectedValue), () -> "header value " + headerValue + " does not start with " + expectedValue); | ||
} else { | ||
assertEquals(expectedValue, headerOptional.get()); | ||
} | ||
} else { | ||
assertEquals(expectedValue, headerOptional.get()); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
http-server-tck/src/main/java/io/micronaut/http/server/tck/EmbeddedServerUnderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2017-2022 original authors | ||
* | ||
* 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 | ||
* | ||
* https://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.micronaut.http.server.tck; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.core.annotation.Experimental; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.HttpResponse; | ||
import io.micronaut.http.client.BlockingHttpClient; | ||
import io.micronaut.http.client.HttpClient; | ||
import io.micronaut.runtime.server.EmbeddedServer; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* {@link ServerUnderTest} implementation for {@link EmbeddedServer}. | ||
* @author Sergio del Amo | ||
* @since 3.0.0 | ||
*/ | ||
@Experimental | ||
public class EmbeddedServerUnderTest implements ServerUnderTest { | ||
|
||
private EmbeddedServer embeddedServer; | ||
private HttpClient httpClient; | ||
private BlockingHttpClient client; | ||
|
||
public EmbeddedServerUnderTest(@NonNull Map<String, Object> properties) { | ||
this.embeddedServer = ApplicationContext.run(EmbeddedServer.class, properties); | ||
} | ||
|
||
@Override | ||
public <I, O> HttpResponse<O> exchange(HttpRequest<I> request, Argument<O> bodyType) { | ||
return getBlockingHttpClient().exchange(request, bodyType); | ||
} | ||
|
||
@Override | ||
public ApplicationContext getApplicationContext() { | ||
return embeddedServer.getApplicationContext(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (httpClient != null) { | ||
httpClient.close(); | ||
} | ||
if (embeddedServer != null) { | ||
embeddedServer.close(); | ||
} | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public Optional<Integer> getPort() { | ||
return Optional.ofNullable(embeddedServer).map(EmbeddedServer::getPort); | ||
} | ||
|
||
@NonNull | ||
private HttpClient getHttpClient() { | ||
if (httpClient == null) { | ||
this.httpClient = getApplicationContext().createBean(HttpClient.class, embeddedServer.getURL()); | ||
} | ||
return httpClient; | ||
} | ||
|
||
@NonNull | ||
private BlockingHttpClient getBlockingHttpClient() { | ||
if (client == null) { | ||
this.client = getHttpClient().toBlocking(); | ||
} | ||
return client; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...erver-tck/src/main/java/io/micronaut/http/server/tck/EmbeddedServerUnderTestProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2017-2022 original authors | ||
* | ||
* 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 | ||
* | ||
* https://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.micronaut.http.server.tck; | ||
|
||
import io.micronaut.core.annotation.Experimental; | ||
import io.micronaut.core.annotation.NonNull; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* {@link ServerUnderTestProvider} implemntation which returns an instance of {@link EmbeddedServerUnderTest}. | ||
* @author Sergio del Amo | ||
* @since 3.8.0 | ||
*/ | ||
@Experimental | ||
public class EmbeddedServerUnderTestProvider implements ServerUnderTestProvider { | ||
@Override | ||
@NonNull | ||
public ServerUnderTest getServer(@NonNull Map<String, Object> properties) { | ||
return new EmbeddedServerUnderTest(properties); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module should be internal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, other modules may want to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but not outside of Micronaut org