diff --git a/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureAnnotatedMethodRouteBuilder.java b/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureAnnotatedMethodRouteBuilder.java index 9145a9a06..2314cf4df 100644 --- a/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureAnnotatedMethodRouteBuilder.java +++ b/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureAnnotatedMethodRouteBuilder.java @@ -58,11 +58,9 @@ public AzureAnnotatedMethodRouteBuilder( @Override protected UriRoute buildBeanRoute(String httpMethodName, HttpMethod httpMethod, String uri, BeanDefinition beanDefinition, ExecutableMethod method) { - String cp = contextPathProvider.getContextPath(); - if (cp == null) { - cp = "/api"; + if (contextPathProvider.getContextPath() == null) { + uri = StringUtils.prependUri("/api", uri); } - uri = StringUtils.prependUri(cp, uri); return super.buildBeanRoute(httpMethodName, httpMethod, uri, beanDefinition, method); } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3565af312..bf6700866 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -34,6 +34,9 @@ kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" } system-lambda = { module = "com.github.stefanbirkner:system-lambda", version.ref = "system-lambda" } +junit-jupiter-engine = { module = 'org.junit.jupiter:junit-jupiter-engine' } +junit-platform-engine = { module = "org.junit.platform:junit-platform-suite-engine" } + # TestContainers testcontainers-azure = { module = "org.testcontainers:azure" } diff --git a/settings.gradle b/settings.gradle index 38ff23524..345c8953c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -18,6 +18,9 @@ include 'azure-function-http' include 'azure-function-http-test' include 'azure-sdk' include 'azure-secret-manager' + +include 'test-suite-azure-function-http-context-path' + include 'doc-examples:example-java' include 'doc-examples:example-groovy' include 'doc-examples:example-kotlin' diff --git a/test-suite-azure-function-http-context-path/build.gradle.kts b/test-suite-azure-function-http-context-path/build.gradle.kts new file mode 100644 index 000000000..f236c8e85 --- /dev/null +++ b/test-suite-azure-function-http-context-path/build.gradle.kts @@ -0,0 +1,22 @@ +plugins { + id("java-library") + id("groovy") +} + +dependencies { + testAnnotationProcessor(platform(mn.micronaut.bom)) + testAnnotationProcessor(mn.micronaut.inject.java) + + testImplementation(project(":azure-function-http")) + + testImplementation(libs.managed.azure.functions.java.library) + + testImplementation(platform(mn.micronaut.bom)) + testImplementation(libs.junit.platform.engine) + testImplementation(libs.junit.jupiter.engine) + testImplementation(mn.micronaut.test.spock) +} + +tasks.withType(Test::class.java) { + useJUnitPlatform() +} diff --git a/test-suite-azure-function-http-context-path/src/test/groovy/io/micronaut/azure/DemoFunctionSpec.groovy b/test-suite-azure-function-http-context-path/src/test/groovy/io/micronaut/azure/DemoFunctionSpec.groovy new file mode 100644 index 000000000..738631d98 --- /dev/null +++ b/test-suite-azure-function-http-context-path/src/test/groovy/io/micronaut/azure/DemoFunctionSpec.groovy @@ -0,0 +1,35 @@ +package io.micronaut.azure + +import com.microsoft.azure.functions.HttpStatus +import io.micronaut.azure.function.http.HttpRequestMessageBuilder +import io.micronaut.http.HttpMethod +import io.micronaut.web.router.Router +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +class DemoFunctionSpec extends Specification { + + @Shared + @AutoCleanup + Function function = new Function() + + void "test function"() { + when:"The function is executed" + HttpRequestMessageBuilder.AzureHttpResponseMessage response = + function.request(HttpMethod.GET, "/demo") + .invoke() + + then:"The response is correct" + response.status == HttpStatus.OK + response.bodyAsString == "Example Response" + } + + void "check routes"() { + given: + def router = function.applicationContext.getBean(Router) + + expect: + router.GET("/test/demo").present + } +} diff --git a/test-suite-azure-function-http-context-path/src/test/java/io/micronaut/azure/DemoController.java b/test-suite-azure-function-http-context-path/src/test/java/io/micronaut/azure/DemoController.java new file mode 100644 index 000000000..7d0f26503 --- /dev/null +++ b/test-suite-azure-function-http-context-path/src/test/java/io/micronaut/azure/DemoController.java @@ -0,0 +1,16 @@ +package io.micronaut.azure; + +import io.micronaut.http.annotation.*; +import io.micronaut.http.annotation.Produces; +import io.micronaut.http.MediaType; + +@Controller("/demo") +public class DemoController { + + @Produces(MediaType.TEXT_PLAIN) + @Get + public String index() { + return "Example Response"; + } +} + diff --git a/test-suite-azure-function-http-context-path/src/test/java/io/micronaut/azure/Function.java b/test-suite-azure-function-http-context-path/src/test/java/io/micronaut/azure/Function.java new file mode 100644 index 000000000..00031ba76 --- /dev/null +++ b/test-suite-azure-function-http-context-path/src/test/java/io/micronaut/azure/Function.java @@ -0,0 +1,21 @@ +package io.micronaut.azure; + +import java.util.*; +import com.microsoft.azure.functions.annotation.*; +import com.microsoft.azure.functions.*; +import io.micronaut.azure.function.http.AzureHttpFunction; + +public class Function extends AzureHttpFunction { + + @FunctionName("ExampleTrigger") + public HttpResponseMessage invoke( + @HttpTrigger( + name = "req", + methods = {HttpMethod.GET, HttpMethod.POST}, + route = "{*route}", + authLevel = AuthorizationLevel.ANONYMOUS) + HttpRequestMessage> request, + final ExecutionContext context) { + return super.route(request, context); + } +} diff --git a/test-suite-azure-function-http-context-path/src/test/resources/application.yml b/test-suite-azure-function-http-context-path/src/test/resources/application.yml new file mode 100644 index 000000000..e1afa71ba --- /dev/null +++ b/test-suite-azure-function-http-context-path/src/test/resources/application.yml @@ -0,0 +1,5 @@ +micronaut: + application: + name: demo + server: + context-path: /test \ No newline at end of file diff --git a/test-suite-azure-function-http-context-path/src/test/resources/logback.xml b/test-suite-azure-function-http-context-path/src/test/resources/logback.xml new file mode 100644 index 000000000..ade55f896 --- /dev/null +++ b/test-suite-azure-function-http-context-path/src/test/resources/logback.xml @@ -0,0 +1,16 @@ + + + + true + + + %cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n + + + + + + + +