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

Fix Azure HTTP functions when you define a contextPath #493

Open
wants to merge 3 commits into
base: 4.1.x
Choose a base branch
from
Open
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
Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 22 additions & 0 deletions test-suite-azure-function-http-context-path/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}

Original file line number Diff line number Diff line change
@@ -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<Optional<String>> request,
final ExecutionContext context) {
return super.route(request, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
micronaut:
application:
name: demo
server:
context-path: /test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
<logger name="io.micronaut.web.router" level="trace" />
</configuration>