Skip to content

Commit

Permalink
Remove URI validation when @TestHTTPResource is injected as String
Browse files Browse the repository at this point in the history
  • Loading branch information
Eng-Fouad committed Jul 25, 2023
1 parent 48bd9c7 commit 11d6a01
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkus.resteasy.reactive.server.test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.common.http.TestHTTPResource;

public class StringTestHTTPResourceWithPathParamsTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest().withApplicationRoot((jar) -> jar.addClass(UserResource.class));

@TestHTTPEndpoint(UserResource.class)
@TestHTTPResource("{userId}/order/{orderId}")
String getUserOrderUrl;

@Test
void testGettingUserOrder() {
int userId = 123;
int orderId = 456;
given().when().get(getUserOrderUrl, userId, orderId)
.then().statusCode(200).body(equalTo(String.format("Order (%d) of user (%d)", userId, orderId)));
}

@Path("/user")
public static class UserResource {
@Path("{userId}/order/{orderId}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getUserOrder(@PathParam("userId") int userId, @PathParam("orderId") int orderId) {
return String.format("Order (%d) of user (%d)", userId, orderId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.test.common.http;

import java.lang.reflect.Field;
import java.net.URI;

public class StringTestHTTPResourceProvider implements TestHTTPResourceProvider<String> {
@Override
Expand All @@ -10,7 +9,7 @@ public Class<String> getProvidedType() {
}

@Override
public String provide(URI testUri, Field field) {
return testUri.toASCIIString();
public String provide(String testUri, Field field) {
return testUri;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.test.common.http;

import java.lang.reflect.Field;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -102,7 +101,7 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
}
f.setAccessible(true);
try {
f.set(testCase, provider.provide(new URI(val), f));
f.set(testCase, provider.provide(val, f));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.test.common.http;

import java.lang.reflect.Field;
import java.net.URI;

public interface TestHTTPResourceProvider<T> {

Expand All @@ -13,6 +12,6 @@ public interface TestHTTPResourceProvider<T> {
* Note that there is no need to directly call set() on the field, it is only provided
* to allow you to examine the generic type and any additional annotations.
*/
T provide(URI testUri, Field field);
T provide(String testUri, Field field);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;

public class URITestHTTPResourceProvider implements TestHTTPResourceProvider<URI> {
@Override
Expand All @@ -10,7 +11,11 @@ public Class<URI> getProvidedType() {
}

@Override
public URI provide(URI testUri, Field field) {
return testUri;
public URI provide(String testUri, Field field) {
try {
return new URI(testUri);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class URLTestHTTPResourceProvider implements TestHTTPResourceProvider<URL> {
Expand All @@ -12,10 +13,10 @@ public Class<URL> getProvidedType() {
}

@Override
public URL provide(URI testUri, Field field) {
public URL provide(String testUri, Field field) {
try {
return testUri.toURL();
} catch (MalformedURLException e) {
return new URI(testUri).toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
Expand Down

0 comments on commit 11d6a01

Please sign in to comment.