Skip to content
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
6 changes: 3 additions & 3 deletions jooby/src/main/java/io/jooby/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public interface Context extends Registry {
@Nonnull Context setUser(@Nullable Object user);

/**
* Recreates full/entire request url using the <code>Host</code> header.
* Recreates full/entire url of the current request using the <code>Host</code> header.
*
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
* please consider to set {@link Router#setTrustProxy(boolean)} option.
Expand All @@ -537,12 +537,12 @@ public interface Context extends Registry {
@Nonnull String getRequestURL();

/**
* Recreates full/entire request url using the <code>Host</code> header.
* Recreates full/entire request url using the <code>Host</code> header with a custom path/suffix.
*
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
* please consider to set {@link Router#setTrustProxy(boolean)} option.
*
* @param path Path to use.
* @param path Path or suffix to use, can also include query string parameters.
* @return Full/entire request url using the <code>Host</code> header.
*/
@Nonnull String getRequestURL(@Nonnull String path);
Expand Down
15 changes: 5 additions & 10 deletions jooby/src/main/java/io/jooby/DefaultContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public interface DefaultContext extends Context {
}

@Override default @Nonnull String getRequestURL() {
return getRequestURL("");
return getRequestURL(getRequestPath() + queryString());
}

@Override default @Nonnull String getRequestURL(@Nonnull String path) {
Expand All @@ -240,16 +240,11 @@ public interface DefaultContext extends Context {
if (port > 0 && port != PORT && port != SECURE_PORT) {
url.append(":").append(port);
}
if (path == null || path.length() == 0) {
url.append(getRequestPath());
} else {
String contextPath = getContextPath();
if (!contextPath.equals("/") && !path.startsWith(contextPath)) {
url.append(contextPath);
}
url.append(path);
String contextPath = getContextPath();
if (!contextPath.equals("/") && !path.startsWith(contextPath)) {
url.append(contextPath);
}
url.append(queryString());
url.append(path);

return url.toString();
}
Expand Down
48 changes: 48 additions & 0 deletions jooby/src/test/java/io/jooby/DefaultContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.jooby;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DefaultContextTest {
private final DefaultContext ctx = mock(DefaultContext.class);

@BeforeEach
public void setUp() {
when(ctx.getScheme()).thenReturn("https");
when(ctx.getHost()).thenReturn("some-host");
when(ctx.getPort()).thenReturn(443);
when(ctx.getContextPath()).thenReturn("/");
when(ctx.getRequestPath()).thenReturn("/path");
when(ctx.queryString()).thenReturn("?query");

when(ctx.getRequestURL()).thenCallRealMethod();
when(ctx.getRequestURL(any())).thenCallRealMethod();
}

@Test
public void getRequestURL() {
assertEquals("https://some-host/path?query", ctx.getRequestURL());
}

@Test
public void getRequestURL_withContextPath() {
when(ctx.getContextPath()).thenReturn("/context");
assertEquals("https://some-host/context/path?query", ctx.getRequestURL());
}

@Test
public void getRequestURL_withNonStandardPort() {
when(ctx.getPort()).thenReturn(999);
assertEquals("https://some-host:999/path?query", ctx.getRequestURL());
}

@Test
public void getRequestURL_withCustomPathWithoutQueryString() {
assertEquals("https://some-host/my-path", ctx.getRequestURL("/my-path"));
}
}