Skip to content

Commit 2ea2363

Browse files
authored
Merge pull request jooby-project#2327 from codeborne/fix-get-request-path
fixes jooby-project#2324 getRequestURL(path) should not include current request's query string
2 parents c7f232e + 6de2588 commit 2ea2363

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

‎jooby/src/main/java/io/jooby/Context.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public interface Context extends Registry {
527527
@Nonnull Context setUser(@Nullable Object user);
528528

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

539539
/**
540-
* Recreates full/entire request url using the <code>Host</code> header.
540+
* Recreates full/entire request url using the <code>Host</code> header with a custom path/suffix.
541541
*
542542
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
543543
* please consider to set {@link Router#setTrustProxy(boolean)} option.
544544
*
545-
* @param path Path to use.
545+
* @param path Path or suffix to use, can also include query string parameters.
546546
* @return Full/entire request url using the <code>Host</code> header.
547547
*/
548548
@Nonnull String getRequestURL(@Nonnull String path);

‎jooby/src/main/java/io/jooby/DefaultContext.java‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public interface DefaultContext extends Context {
228228
}
229229

230230
@Override default @Nonnull String getRequestURL() {
231-
return getRequestURL("");
231+
return getRequestURL(getRequestPath() + queryString());
232232
}
233233

234234
@Override default @Nonnull String getRequestURL(@Nonnull String path) {
@@ -240,16 +240,11 @@ public interface DefaultContext extends Context {
240240
if (port > 0 && port != PORT && port != SECURE_PORT) {
241241
url.append(":").append(port);
242242
}
243-
if (path == null || path.length() == 0) {
244-
url.append(getRequestPath());
245-
} else {
246-
String contextPath = getContextPath();
247-
if (!contextPath.equals("/") && !path.startsWith(contextPath)) {
248-
url.append(contextPath);
249-
}
250-
url.append(path);
243+
String contextPath = getContextPath();
244+
if (!contextPath.equals("/") && !path.startsWith(contextPath)) {
245+
url.append(contextPath);
251246
}
252-
url.append(queryString());
247+
url.append(path);
253248

254249
return url.toString();
255250
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.jooby;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.mockito.ArgumentMatchers.any;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.when;
10+
11+
public class DefaultContextTest {
12+
private final DefaultContext ctx = mock(DefaultContext.class);
13+
14+
@BeforeEach
15+
public void setUp() {
16+
when(ctx.getScheme()).thenReturn("https");
17+
when(ctx.getHost()).thenReturn("some-host");
18+
when(ctx.getPort()).thenReturn(443);
19+
when(ctx.getContextPath()).thenReturn("/");
20+
when(ctx.getRequestPath()).thenReturn("/path");
21+
when(ctx.queryString()).thenReturn("?query");
22+
23+
when(ctx.getRequestURL()).thenCallRealMethod();
24+
when(ctx.getRequestURL(any())).thenCallRealMethod();
25+
}
26+
27+
@Test
28+
public void getRequestURL() {
29+
assertEquals("https://some-host/path?query", ctx.getRequestURL());
30+
}
31+
32+
@Test
33+
public void getRequestURL_withContextPath() {
34+
when(ctx.getContextPath()).thenReturn("/context");
35+
assertEquals("https://some-host/context/path?query", ctx.getRequestURL());
36+
}
37+
38+
@Test
39+
public void getRequestURL_withNonStandardPort() {
40+
when(ctx.getPort()).thenReturn(999);
41+
assertEquals("https://some-host:999/path?query", ctx.getRequestURL());
42+
}
43+
44+
@Test
45+
public void getRequestURL_withCustomPathWithoutQueryString() {
46+
assertEquals("https://some-host/my-path", ctx.getRequestURL("/my-path"));
47+
}
48+
}

0 commit comments

Comments
 (0)