From 1a1ae8dd3bf99b955d62a71836d4f0f83faadf23 Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Tue, 14 May 2024 21:39:17 +0200 Subject: [PATCH] chore: Add support for test variables in Http URLs --- .../yaks/http/HttpClientSteps.java | 33 +++++++++++-------- .../yaks/http/HttpServerSteps.java | 4 +-- .../citrusframework/yaks/http/HttpSteps.java | 13 +++++--- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java index 3c2c3ec0..9561d06c 100644 --- a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java +++ b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java @@ -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$") @@ -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]+)?$") @@ -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]+)?$") @@ -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 )\"(.+)\"$") @@ -333,7 +335,7 @@ 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(); @@ -341,7 +343,7 @@ public void sendClientRequest(String method, String path) { @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(); } @@ -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; @@ -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() { diff --git a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpServerSteps.java b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpServerSteps.java index 273e8aca..80a1330b 100644 --- a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpServerSteps.java +++ b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpServerSteps.java @@ -366,7 +366,7 @@ 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(); @@ -374,7 +374,7 @@ public void receiveServerRequest(String method, String path) { @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(); } diff --git a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpSteps.java b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpSteps.java index aef0ee30..e41b3ac1 100644 --- a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpSteps.java +++ b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpSteps.java @@ -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; @@ -71,9 +72,10 @@ default String getMessageType(String contentType) { * @param headers * @param method * @param path + * @param context * @return */ - default HttpMessage createRequest(String body, Map headers, Map params, String method, String path) { + default HttpMessage createRequest(String body, Map headers, Map params, String method, String path, TestContext context) { HttpMessage request = new HttpMessage(); request.method(HttpMethod.valueOf(method)); @@ -82,7 +84,7 @@ default HttpMessage createRequest(String body, Map headers, Map< } if (StringUtils.hasText(body)) { - request.setPayload(body); + request.setPayload(context.replaceDynamicContentInString(body)); } for (Map.Entry headerEntry : headers.entrySet()) { @@ -101,18 +103,19 @@ default HttpMessage createRequest(String body, Map headers, Map< * @param body * @param headers * @param status + * @param context * @return */ - default HttpMessage createResponse(String body, Map headers, Integer status) { + default HttpMessage createResponse(String body, Map 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 headerEntry : headers.entrySet()) { - response.setHeader(headerEntry.getKey(), headerEntry.getValue()); + response.setHeader(headerEntry.getKey(), context.replaceDynamicContentInString(headerEntry.getValue())); } return response;