Skip to content

Commit

Permalink
chore: Add support for test variables in Http URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed May 14, 2024
1 parent 79590dc commit 1a1ae8d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ public void setClient(String id) {

@Given("^(?:URL|url): ([^\\s]+)$")
public void setUrl(String url) {
if (url.startsWith("https")) {
String resolvedUrl = context.replaceDynamicContentInString(url);

if (resolvedUrl.startsWith("https")) {
httpClient.getEndpointConfiguration().setRequestFactory(sslRequestFactory());
}

this.requestUrl = url;
this.requestUrl = resolvedUrl;
}

@Given("^HTTP client (enable|disable) basic auth$")
Expand Down Expand Up @@ -200,17 +202,17 @@ public void healthCheck() {

@Given("^(?:URL|url|path) ([^\\s]+) is healthy$")
public void healthCheck(String urlOrPath) {
waitForHttpUrl(getRequestUrl(urlOrPath));
waitForHttpUrl(getRequestUrl(urlOrPath, context));
}

@Given("^wait for (?:URL|url|path) ([^\\s]+)$")
public void waitForHttpUrl(String urlOrPath) {
waitForHttpStatus(getRequestUrl(urlOrPath), 200);
waitForHttpStatus(getRequestUrl(urlOrPath, context), 200);
}

@Given("^wait for (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) on (?:URL|url|path) ([^\\s]+)$")
public void waitForHttpUrlUsingMethod(String method, String urlOrPath) {
waitForHttpStatusUsingMethod(method, getRequestUrl(urlOrPath), 200);
waitForHttpStatusUsingMethod(method, getRequestUrl(urlOrPath, context), 200);
}

@Given("^wait for (?:URL|url|path) ([^\\s]+) to return (\\d+)(?: [^\\s]+)?$")
Expand All @@ -219,7 +221,7 @@ public void waitForHttpStatus(String urlOrPath, Integer statusCode) {
.milliseconds(timeout)
.interval(timeout / 10)
.status(statusCode)
.url(getRequestUrl(urlOrPath)));
.url(getRequestUrl(urlOrPath, context)));
}

@Given("^wait for (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) on (?:URL|url|path) ([^\\s]+) to return (\\d+)(?: [^\\s]+)?$")
Expand All @@ -229,7 +231,7 @@ public void waitForHttpStatusUsingMethod(String method, String urlOrPath, Intege
.method(method)
.interval(timeout / 10)
.status(statusCode)
.url(getRequestUrl(urlOrPath)));
.url(getRequestUrl(urlOrPath, context)));
}

@Then("^(?:expect|verify) HTTP response header ([^\\s]+)(?:=| is )\"(.+)\"$")
Expand Down Expand Up @@ -333,15 +335,15 @@ public void sendClientRequestMultilineBody(String method) {

@When("^send (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) ([^\"\\s]+)$")
public void sendClientRequest(String method, String path) {
sendClientRequest(createRequest(requestBody, requestHeaders, requestParams, method, path));
sendClientRequest(createRequest(requestBody, requestHeaders, requestParams, method, path, context));
requestBody = null;
requestHeaders.clear();
requestParams.clear();
}

@Then("^receive HTTP (\\d+)(?: [^\\s]+)?$")
public void receiveClientResponse(Integer status) {
receiveClientResponse(createResponse(responseBody, responseHeaders, status));
receiveClientResponse(createResponse(responseBody, responseHeaders, status, context));
responseBody = null;
responseHeaders.clear();
}
Expand Down Expand Up @@ -462,11 +464,14 @@ private org.apache.hc.client5.http.classic.HttpClient sslClient() {
* separators in concatenated URLs.
*
* @param urlOrPath
* @param context
* @return
*/
private String getRequestUrl(String urlOrPath) {
if (StringUtils.hasText(urlOrPath) && urlOrPath.startsWith("http")) {
return urlOrPath;
private String getRequestUrl(String urlOrPath, TestContext context) {
String resolvedUrlOrPath = context.replaceDynamicContentInString(urlOrPath);

if (StringUtils.hasText(resolvedUrlOrPath) && resolvedUrlOrPath.startsWith("http")) {
return resolvedUrlOrPath;
}

String url;
Expand All @@ -478,11 +483,11 @@ private String getRequestUrl(String urlOrPath) {
throw new CitrusRuntimeException("Must provide a base request URL first when using relative resource path: " + urlOrPath);
}

if (!StringUtils.hasText(urlOrPath) || urlOrPath.equals("/")) {
if (!StringUtils.hasText(resolvedUrlOrPath) || resolvedUrlOrPath.equals("/")) {
return url;
}

return (url.endsWith("/") ? url : url + "/") + (urlOrPath.startsWith("/") ? urlOrPath.substring(1) : urlOrPath);
return (url.endsWith("/") ? url : url + "/") + (resolvedUrlOrPath.startsWith("/") ? resolvedUrlOrPath.substring(1) : resolvedUrlOrPath);
}

private HttpComponentsClientHttpRequestFactory basicAuthRequestFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,15 @@ public void receiveServerRequestMultilineBody(String method) {

@When("^receive (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) ([^\"\\s]+)$")
public void receiveServerRequest(String method, String path) {
receiveServerRequest(createRequest(requestBody, requestHeaders, requestParams, method, path));
receiveServerRequest(createRequest(requestBody, requestHeaders, requestParams, method, path, context));
requestBody = null;
requestHeaders.clear();
requestParams.clear();
}

@Then("^send HTTP (\\d+)(?: [^\\s]+)?$")
public void sendServerResponse(Integer status) {
sendServerResponse(createResponse(responseBody, responseHeaders, status));
sendServerResponse(createResponse(responseBody, responseHeaders, status, context));
responseBody = null;
responseHeaders.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;

import org.citrusframework.CitrusSettings;
import org.citrusframework.context.TestContext;
import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.message.MessageType;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -71,9 +72,10 @@ default String getMessageType(String contentType) {
* @param headers
* @param method
* @param path
* @param context
* @return
*/
default HttpMessage createRequest(String body, Map<String, String> headers, Map<String, String> params, String method, String path) {
default HttpMessage createRequest(String body, Map<String, String> headers, Map<String, String> params, String method, String path, TestContext context) {
HttpMessage request = new HttpMessage();
request.method(HttpMethod.valueOf(method));

Expand All @@ -82,7 +84,7 @@ default HttpMessage createRequest(String body, Map<String, String> headers, Map<
}

if (StringUtils.hasText(body)) {
request.setPayload(body);
request.setPayload(context.replaceDynamicContentInString(body));
}

for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
Expand All @@ -101,18 +103,19 @@ default HttpMessage createRequest(String body, Map<String, String> headers, Map<
* @param body
* @param headers
* @param status
* @param context
* @return
*/
default HttpMessage createResponse(String body, Map<String, String> headers, Integer status) {
default HttpMessage createResponse(String body, Map<String, String> headers, Integer status, TestContext context) {
HttpMessage response = new HttpMessage();
response.status(HttpStatus.valueOf(status));

if (StringUtils.hasText(body)) {
response.setPayload(body);
response.setPayload(context.replaceDynamicContentInString(body));
}

for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
response.setHeader(headerEntry.getKey(), headerEntry.getValue());
response.setHeader(headerEntry.getKey(), context.replaceDynamicContentInString(headerEntry.getValue()));
}

return response;
Expand Down

0 comments on commit 1a1ae8d

Please sign in to comment.