Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...eter/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/UriTagCorsTest.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,104 @@ | ||
package io.quarkus.micrometer.deployment.binder; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.micrometer.core.instrument.Metrics; | ||
import io.micrometer.core.instrument.Timer; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import io.quarkus.micrometer.test.HelloResource; | ||
import io.quarkus.micrometer.test.Util; | ||
import io.quarkus.micrometer.test.VertxWebEndpoint; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class UriTagCorsTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("test-logging.properties") | ||
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false") | ||
.overrideConfigKey("quarkus.micrometer.binder.http-server.enabled", "true") | ||
.overrideConfigKey("quarkus.micrometer.binder.vertx.enabled", "true") | ||
.overrideConfigKey("quarkus.http.cors", "true") | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(Util.class, | ||
VertxWebEndpoint.class, | ||
HelloResource.class)); | ||
|
||
static SimpleMeterRegistry registry = new SimpleMeterRegistry(); | ||
|
||
@BeforeAll | ||
static void setRegistry() { | ||
registry = new SimpleMeterRegistry(); | ||
Metrics.addRegistry(registry); | ||
} | ||
|
||
@AfterAll() | ||
static void removeRegistry() { | ||
Metrics.removeRegistry(registry); | ||
} | ||
|
||
@Test | ||
public void testCORSPreflightRequest() { | ||
String origin = "http://custom.origin.quarkus"; | ||
String methods = "GET,POST"; | ||
String headers = "X-Custom"; | ||
RestAssured.given() | ||
.header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when().options("/hello/world").then() | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers) | ||
.statusCode(200); | ||
|
||
RestAssured.given() | ||
.header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when().options("/vertx/echo/anything").then() | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers) | ||
.statusCode(200); | ||
|
||
RestAssured.given() | ||
.header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when().options("/vertx/item/123").then() | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers) | ||
.statusCode(200); | ||
|
||
RestAssured.given() | ||
.when().options("/vertx/echo/anything").then() | ||
.statusCode(200); | ||
|
||
RestAssured.given() | ||
.when().options("/hello/world").then() | ||
.statusCode(200); | ||
|
||
// CORS pre-flight | ||
Assertions.assertEquals(1, registry.find("http.server.requests").tag("uri", "/cors-preflight").timers().size(), | ||
Util.foundServerRequests(registry, "/cors-preflight should be used for preflight requests")); | ||
Timer t = registry.find("http.server.requests").tag("uri", "/cors-preflight").timer(); | ||
Assertions.assertEquals(3, t.count(), "/cors-preflight checked twice"); | ||
|
||
// Normal OPTIONS requests | ||
Assertions.assertEquals(1, registry.find("http.server.requests").tag("uri", "/vertx/echo/{msg}").timers().size(), | ||
Util.foundServerRequests(registry, "/vertx/echo/{msg} for options request")); | ||
Assertions.assertEquals(1, registry.find("http.server.requests").tag("uri", "/hello/{message}").timers().size(), | ||
Util.foundServerRequests(registry, "/hello/{message} for options request")); | ||
} | ||
} |
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