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 Servlet request for when context path is configured #473

Merged
merged 3 commits into from
Jun 6, 2023
Merged
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
@@ -0,0 +1,46 @@
package io.micronaut.servlet.jetty

import io.micronaut.context.annotation.Property
import io.micronaut.http.HttpRequest
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.annotation.QueryValue
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
@Property(name = "micronaut.server.context-path", value = CONTEXT_PATH)
@Property(name = "spec.name", value = SPEC_NAME)
class JettyContextPathSpec extends Specification {

static final String SPEC_NAME = "JettyContextPathSpec"
static final String CONTEXT_PATH = "/test"

@Inject
@Client("/")
HttpClient client

void "context-path is supported"() {
when:
def request = HttpRequest.GET(CONTEXT_PATH + "?name=Fred")
String response = client.toBlocking().retrieve(request)

then:
response == "OK Fred"
}

@Controller
static class TestController {

@Get
@Produces(MediaType.TEXT_PLAIN)
String index(@QueryValue String name) {
"OK $name"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.micronaut.servlet.tomcat

import io.micronaut.context.annotation.Property
import io.micronaut.http.HttpRequest
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.annotation.QueryValue
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
@Property(name = "micronaut.server.context-path", value = CONTEXT_PATH)
@Property(name = "spec.name", value = SPEC_NAME)
class TomcatContextPathSpec extends Specification {

static final String SPEC_NAME = "TomcatContextPathSpec"
static final String CONTEXT_PATH = "/test"

@Inject
@Client("/")
HttpClient client

void "context-path is supported"() {
when:
def request = HttpRequest.GET(CONTEXT_PATH + "?name=Fred")
String response = client.toBlocking().retrieve(request)

then:
response == "OK Fred"
}

@Controller
static class TestController {

@Get
@Produces(MediaType.TEXT_PLAIN)
String index(@QueryValue String name) {
"OK $name"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.micronaut.servlet.undertow

import io.micronaut.context.annotation.Property
import io.micronaut.http.HttpRequest
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.annotation.QueryValue
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
@Property(name = "micronaut.server.context-path", value = CONTEXT_PATH)
@Property(name = "spec.name", value = SPEC_NAME)
class UndertowContextPathSpec extends Specification {

static final String SPEC_NAME = "UndertowContextPathSpec"
static final String CONTEXT_PATH = "/test"

@Inject
@Client("/")
HttpClient client

void "context-path is supported"() {
when:
def request = HttpRequest.GET(CONTEXT_PATH + "?name=Fred")
String response = client.toBlocking().retrieve(request)

then:
response == "OK Fred"
}

@Controller
static class TestController {

@Get
@Produces(MediaType.TEXT_PLAIN)
String index(@QueryValue String name) {
"OK $name"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,8 @@ protected DefaultServletHttpRequest(
MediaTypeCodecRegistry codecRegistry) {
this.delegate = delegate;
this.codecRegistry = codecRegistry;
final String contextPath = delegate.getContextPath();

String requestURI = delegate.getRequestURI();
if (StringUtils.isNotEmpty(contextPath) && requestURI.startsWith(contextPath)) {
requestURI = requestURI.substring(contextPath.length());
}

String queryString = delegate.getQueryString();
if (StringUtils.isNotEmpty(queryString)) {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pluginManagement {
}

plugins {
id 'io.micronaut.build.shared.settings' version '5.4.0'
id 'io.micronaut.build.shared.settings' version '5.4.9'
}

rootProject.name = 'servlet-parent'
Expand Down