Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…inoko-master
  • Loading branch information
dilipkrish committed Aug 3, 2019
2 parents 55e7991 + fccc5b6 commit 486b9d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ private static ServletUriComponentsBuilder fromServletMapping(
ServletUriComponentsBuilder builder = fromContextPath(request);

XForwardPrefixPathAdjuster adjuster = new XForwardPrefixPathAdjuster(request);
builder.replacePath(adjuster.adjustedPath(basePath));
String adjustedPath = adjuster.adjustedPath(basePath);
if (!adjustedPath.equals(basePath)) {
builder.replacePath(adjustedPath);
}
if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
builder.path(request.getServletPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import spock.lang.Specification

import javax.servlet.http.HttpServletRequest

import static springfox.documentation.swagger.common.HostNameProvider.*
import static springfox.documentation.swagger.common.XForwardPrefixPathAdjuster.*
import static java.util.Collections.emptyEnumeration
import static springfox.documentation.swagger.common.HostNameProvider.componentsFrom
import static springfox.documentation.swagger.common.XForwardPrefixPathAdjuster.X_FORWARDED_PREFIX

class HostNameProviderSpec extends Specification {
def "should prefix path with x-forwarded-prefix"() {
given:
def request = mockRequest()
def request = mockRequest(true)

when:
def result = componentsFrom(request, "/basePath")
Expand All @@ -20,6 +21,17 @@ class HostNameProviderSpec extends Specification {
result.toUriString() == "http://localhost/prefix"
}

def "should preserve contextPath from request if no x-forwarded-prefix"() {
given:
def request = mockRequest(false)

when:
def result = componentsFrom(request, "/basePath")

then:
result.toUriString() == "http://localhost/contextPath"
}

def "should not be allowed to create object from utility class"() {
when:
new HostNameProvider()
Expand All @@ -28,10 +40,14 @@ class HostNameProviderSpec extends Specification {
thrown UnsupportedOperationException
}

def mockRequest() {
def mockRequest(boolean addXForwardedHeaders) {
def request = Mock(HttpServletRequest.class)
request.getHeader(X_FORWARDED_PREFIX) >> "/prefix"
request.getHeaders(X_FORWARDED_PREFIX) >> headerValues()
if (addXForwardedHeaders) {
request.getHeader(X_FORWARDED_PREFIX) >> "/prefix"
request.getHeaders(X_FORWARDED_PREFIX) >> headerValues()
} else {
request.getHeaders(X_FORWARDED_PREFIX) >> emptyEnumeration()
}
request.headerNames >> headerNames()
request.requestURL >> new StringBuffer("http://localhost/contextPath")
request.requestURI >> new URI("http://localhost/contextPath")
Expand Down

0 comments on commit 486b9d2

Please sign in to comment.