Skip to content

Commit

Permalink
Merge pull request #177 from axonivy/query-params-in-path
Browse files Browse the repository at this point in the history
Fix query string in path
  • Loading branch information
ivy-lli authored Nov 16, 2023
2 parents 41e32f2 + 36f526d commit b5d9dd1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [11.2.1] - 2023-11-16

**Breaking Change:** If you use the EngineUrl#path method with a query parameter, an IllegalArgumentException will be thrown.
Please use the EngineUrl#queryParam method instead.

## [11.2.0] - 2023-11-15

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import javax.ws.rs.core.UriBuilder;

import org.apache.commons.lang3.StringUtils;

/**
* This is a Util to build URLs against the designer (localhost:8081) or a test
* engine.
Expand Down Expand Up @@ -75,23 +77,28 @@ public static EngineUrl create() {
}

public static String createProcessUrl(String path) {
return create().process(path).toUrl();
var parts = splitQueryFromPath(path);
return create().process(parts.path).toUrl() + parts.query;
}

public static String createRestUrl(String path) {
return create().rest(path).toUrl();
var parts = splitQueryFromPath(path);
return create().rest(parts.path).toUrl() + parts.query;
}

public static String createWebServiceUrl(String path) {
return create().webService(path).toUrl();
var parts = splitQueryFromPath(path);
return create().webService(parts.path).toUrl() + parts.query;
}

public static String createStaticViewUrl(String path) {
return create().staticView(path).toUrl();
var parts = splitQueryFromPath(path);
return create().staticView(parts.path).toUrl() + parts.query;
}

public static String createCaseMapUrl(String path) {
return create().caseMap(path).toUrl();
var parts = splitQueryFromPath(path);
return create().caseMap(parts.path).toUrl() + parts.query;
}

public EngineUrl base(String base) {
Expand Down Expand Up @@ -130,6 +137,9 @@ public EngineUrl servlet(SERVLET servlet) {
}

public EngineUrl path(String path) {
if (StringUtils.contains(path, "?")) {
throw new IllegalArgumentException("Adding query parameters via the path method will not work, please use the queryParam method or encode the '?' with '%3F'.");
}
this.path = path;
return this;
}
Expand Down Expand Up @@ -161,6 +171,18 @@ private String getServletPath() {
return "";
}

private static PathAndQuery splitQueryFromPath(String pathWithQuery) {
if (pathWithQuery == null) {
return new PathAndQuery("", "");
}
var path = StringUtils.substringBefore(pathWithQuery, "?");
var query = StringUtils.substringAfter(pathWithQuery, "?");
if (query.length() > 0) {
query = "?" + query;
}
return new PathAndQuery(path, query);
}

/**
* Gets base URL of a running engine. Returns URL of started
* project-build-plugin test engine ({@value #TEST_ENGINE_URL}) or
Expand Down Expand Up @@ -239,4 +261,5 @@ public static Boolean isDesigner() {
}

private static record QueryParam(String key, String value) {}
private static record PathAndQuery(String path, String query) {}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.axonivy.ivy.webtest.engine;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

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

import com.axonivy.ivy.webtest.engine.EngineUrl.SERVLET;
import com.codeborne.selenide.Configuration;

class TestEngineUrl {
private static String BASE_URL = "http://www.axonivy.com:8080/ivy/";
private static String APP = "test";

@BeforeEach
void setup() {
System.setProperty(EngineUrl.TEST_ENGINE_URL, BASE_URL);
System.setProperty(EngineUrl.TEST_ENGINE_APP, APP);
}

@AfterEach
void cleanup() {
Expand All @@ -20,6 +30,8 @@ void cleanup() {
void designerUrls() {
var remote = Configuration.remote;
try {
System.clearProperty(EngineUrl.TEST_ENGINE_URL);
System.clearProperty(EngineUrl.TEST_ENGINE_APP);
Configuration.remote = null;
String baseUrl = "http://localhost:8081/";
assertThat(EngineUrl.createRestUrl("")).isEqualTo(baseUrl + EngineUrl.DESIGNER + "/api");
Expand All @@ -35,15 +47,11 @@ void designerUrls() {

@Test
void engineUrls() {
String baseUrl = "http://www.axonivy.com:8080/ivy/";
String app = "test";
System.setProperty(EngineUrl.TEST_ENGINE_URL, baseUrl);
System.setProperty(EngineUrl.TEST_ENGINE_APP, app);
assertThat(EngineUrl.createRestUrl("")).isEqualTo(baseUrl + app + "/api");
assertThat(EngineUrl.createWebServiceUrl("")).isEqualTo(baseUrl + app + "/ws");
assertThat(EngineUrl.createProcessUrl("")).isEqualTo(baseUrl + app + "/pro");
assertThat(EngineUrl.createStaticViewUrl("")).isEqualTo(baseUrl + app + "/faces/view");
assertThat(EngineUrl.createCaseMapUrl("")).isEqualTo(baseUrl + app + "/casemap");
assertThat(EngineUrl.createRestUrl("")).isEqualTo(BASE_URL + APP + "/api");
assertThat(EngineUrl.createWebServiceUrl("")).isEqualTo(BASE_URL + APP + "/ws");
assertThat(EngineUrl.createProcessUrl("")).isEqualTo(BASE_URL + APP + "/pro");
assertThat(EngineUrl.createStaticViewUrl("")).isEqualTo(BASE_URL + APP + "/faces/view");
assertThat(EngineUrl.createCaseMapUrl("")).isEqualTo(BASE_URL + APP + "/casemap");
assertThat(EngineUrl.isDesigner()).isEqualTo(false);
}

Expand All @@ -67,11 +75,15 @@ void urlBuidler() {

@Test
void queryParam() {
String baseUrl = "http://www.axonivy.com:8080/ivy/";
String app = "test";
System.setProperty(EngineUrl.TEST_ENGINE_URL, baseUrl);
System.setProperty(EngineUrl.TEST_ENGINE_APP, app);
var url = EngineUrl.create().staticView("abc.xhtml").queryParam("userName", "crazy user").toUrl();
assertThat(url).isEqualTo(baseUrl + app + "/faces/view/abc.xhtml?userName=crazy+user");
assertThat(url).isEqualTo(BASE_URL + APP + "/faces/view/abc.xhtml?userName=crazy+user");
}

@Test
void queryParamInPath() {
assertThatThrownBy(() -> EngineUrl.create().path("bla?embedInFrame")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Adding query parameters via the path method will not work");
assertThat(EngineUrl.createProcessUrl("start.ivp?locale=en&format=DE")).isEqualTo("http://www.axonivy.com:8080/ivy/test/pro/start.ivp?locale=en&format=DE");
assertThat(EngineUrl.createRestUrl("variable/myVar?value=new")).isEqualTo("http://www.axonivy.com:8080/ivy/test/api/variable/myVar?value=new");
}
}

0 comments on commit b5d9dd1

Please sign in to comment.