Skip to content
Browse files

[#1104] Now rendering using @@{'/publice/..'} in jobs works with and …

…without trailing '/' in application.baseUrl
  • Loading branch information...
1 parent 0976149 commit 7865d717e172b97145ce701b057400bc7fd88a4c @mbknor mbknor committed Sep 13, 2011
Showing with 68 additions and 3 deletions.
  1. +19 −3 framework/src/play/mvc/Router.java
  2. +49 −0 framework/test-src/play/mvc/RouterTest.java
View
22 framework/src/play/mvc/Router.java
@@ -318,13 +318,29 @@ public static ActionDefinition reverse(String action) {
public static String getFullUrl(String action, Map<String, Object> args) {
ActionDefinition actionDefinition = reverse(action, args);
- String base = Http.Request.current() == null ? Play.configuration.getProperty("application.baseUrl", "application.baseUrl") : Http.Request.current().getBase();
+ String base = getBaseUrl();
if (actionDefinition.method.equals("WS")) {
return base.replaceFirst("https?", "ws") + actionDefinition;
}
return base + actionDefinition;
}
+ // Gets baseUrl from current request or application.baseUrl in application.conf
+ protected static String getBaseUrl() {
+ if (Http.Request.current() == null) {
+ // No current request is present - must get baseUrl from config
+ String appBaseUrl = Play.configuration.getProperty("application.baseUrl", "application.baseUrl");
+ if (appBaseUrl.endsWith("/")) {
+ // remove the trailing slash
+ appBaseUrl = appBaseUrl.substring(0, appBaseUrl.length()-1);
+ }
+ return appBaseUrl;
+
+ } else {
+ return Http.Request.current().getBase();
+ }
+ }
+
public static String getFullUrl(String action) {
// Note the map is not <code>Collections.EMPTY_MAP</code> because it will be copied and changed.
return getFullUrl(action, new HashMap<String, Object>(16));
@@ -356,7 +372,7 @@ public static String reverse(VirtualFile file, boolean absolute) {
}
if (absolute) {
boolean isSecure = Http.Request.current() == null ? false : Http.Request.current().secure;
- String base = Http.Request.current() == null ? Play.configuration.getProperty("application.baseUrl", "application.baseUrl") : Http.Request.current().getBase();
+ String base = getBaseUrl();
if (!StringUtils.isEmpty(route.host)) {
// Compute the host
int port = Http.Request.current() == null ? 80 : Http.Request.current().get().port;
@@ -578,7 +594,7 @@ public String toString() {
public void absolute() {
boolean isSecure = Http.Request.current() == null ? false : Http.Request.current().secure;
- String base = Http.Request.current() == null ? Play.configuration.getProperty("application.baseUrl", "application.baseUrl") : Http.Request.current().getBase();
+ String base = getBaseUrl();
String hostPart = host;
String domain = Http.Request.current() == null ? "" : Http.Request.current().get().domain;
int port = Http.Request.current() == null ? 80 : Http.Request.current().get().port;
View
49 framework/test-src/play/mvc/RouterTest.java
@@ -0,0 +1,49 @@
+package play.mvc;
+
+import com.ning.http.client.RequestBuilder;
+import org.junit.Test;
+import play.Play;
+
+import java.util.Properties;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RouterTest {
+
+ @Test
+ public void test_getBaseUrl() {
+
+ Play.configuration = new Properties();
+
+ // test with currentRequest
+ Http.Request request = Http.Request.createRequest(
+ null,
+ "GET",
+ "/",
+ "",
+ null,
+ null,
+ null,
+ null,
+ false,
+ 80,
+ "localhost",
+ false,
+ null,
+ null
+ );
+
+ Http.Request.current.set( request );
+ assertThat(Router.getBaseUrl()).isEqualTo("http://localhost");
+
+ // test without current request
+ Http.Request.current.remove();
+ // application.baseUrl without trailing /
+ Play.configuration.setProperty("application.baseUrl", "http://a");
+ assertThat(Router.getBaseUrl()).isEqualTo("http://a");
+
+ // application.baseUrl with trailing /
+ Play.configuration.setProperty("application.baseUrl", "http://b/");
+ assertThat(Router.getBaseUrl()).isEqualTo("http://b");
+ }
+}

0 comments on commit 7865d71

Please sign in to comment.
Something went wrong with that request. Please try again.