Skip to content

Commit

Permalink
Merge pull request #93 from christophd/issue/92/wait-for-steps
Browse files Browse the repository at this point in the history
fix(#92): Add waitFor Http URL condition step
  • Loading branch information
christophd authored Apr 27, 2020
2 parents ada111b + 091655b commit 049255b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.consol.citrus.TestCaseRunner;
import com.consol.citrus.annotations.CitrusFramework;
import com.consol.citrus.annotations.CitrusResource;
import com.consol.citrus.container.Wait;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.http.actions.HttpClientActionBuilder;
import com.consol.citrus.http.actions.HttpClientRequestActionBuilder;
Expand Down Expand Up @@ -85,6 +86,8 @@ public class HttpClientSteps implements HttpSteps {
private DataDictionary outboundDictionary;
private DataDictionary inboundDictionary;

private long timeout;

@Before
public void before(Scenario scenario) {
if (httpClient == null && citrus.getCitrusContext().getReferenceResolver().resolveAll(HttpClient.class).size() == 1L) {
Expand All @@ -93,6 +96,8 @@ public void before(Scenario scenario) {
httpClient = new HttpClientBuilder().build();
}

timeout = httpClient.getEndpointConfiguration().getTimeout();

requestHeaders = new HashMap<>();
responseHeaders = new HashMap<>();
requestParams = new HashMap<>();
Expand Down Expand Up @@ -123,6 +128,45 @@ public void setUrl(String url) {
this.requestUrl = url;
}

@Given("^HTTP request timeout is (\\d+)(?: ms| milliseconds)$")
public void configureTimeout(long timeout) {
this.timeout = timeout;
}

@Given("^(?:URL|url) is healthy$")
public void healthCheck() {
waitForHttpUrl(requestUrl);
}

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

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

@Given("^wait for (?:URL|url) ([^\\s]+) to return (\\d+)(?: [^\\s]+)?$")
public void waitForHttpStatus(String url, Integer statusCode) {
runner.given(Wait.Builder.waitFor().http()
.milliseconds(timeout)
.interval(timeout / 10)
.status(statusCode)
.url(url));
}

@Given("^wait for (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) on (?:URL|url) ([^\\s]+) to return (\\d+)(?: [^\\s]+)?$")
public void waitForHttpStatusUsingMethod(String method, String url, Integer statusCode) {
runner.given(Wait.Builder.waitFor().http()
.milliseconds(timeout)
.method(method)
.interval(timeout / 10)
.status(statusCode)
.url(url));
}

@Then("^(?:expect|verify) HTTP response header ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addResponseHeader(String name, String value) {
if (name.equals(HttpHeaders.CONTENT_TYPE)) {
Expand Down Expand Up @@ -274,6 +318,7 @@ private void receiveClientResponse(HttpMessage response) {
}
bodyValidationExpressions.clear();

responseBuilder.timeout(timeout);
responseBuilder.messageType(responseMessageType);

if (inboundDictionary != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public EndpointAdapter staticResponseAdapter(TestContextFactory contextFactory)
Map<String, EndpointAdapter> mappings = new HashMap<>();

mappings.put(HttpMethod.GET.name(), handleGetRequestAdapter(contextFactory));
mappings.put(HttpMethod.HEAD.name(), handleHeadRequestAdapter(contextFactory));
mappings.put(HttpMethod.POST.name(), handlePostRequestAdapter());
mappings.put(HttpMethod.PUT.name(), handlePutRequestAdapter());
mappings.put(HttpMethod.DELETE.name(), handleDeleteRequestAdapter());
Expand Down Expand Up @@ -105,6 +106,15 @@ public EndpointAdapter handleGetRequestAdapter(TestContextFactory contextFactory
return responseEndpointAdapter;
}

@Bean
public EndpointAdapter handleHeadRequestAdapter(TestContextFactory contextFactory) {
StaticResponseEndpointAdapter responseEndpointAdapter = new StaticResponseEndpointAdapter();
responseEndpointAdapter.getMessageHeader().put(HttpMessageHeaders.HTTP_CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
responseEndpointAdapter.getMessageHeader().put("X-TodoId", "citrus:randomNumber(5)");
responseEndpointAdapter.setTestContextFactory(contextFactory);
return responseEndpointAdapter;
}

@Bean
public EndpointAdapter handleDeleteRequestAdapter() {
return new StaticEndpointAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Feature: Http client
Given variable port is "8080"
Given URL: http://localhost:${port}

Scenario: Health check
And URL is healthy

Scenario: Wait for Http URL condition
Given HTTP request timeout is 5000 milliseconds
Then wait for URL http://localhost:${port}/todo to return 200 OK
And wait for GET on URL http://localhost:${port}/todo

Scenario: GET
When send GET /todo
Then verify HTTP response body: {"id": "@ignore@", "task": "Sample task", "completed": 0}
Expand Down

0 comments on commit 049255b

Please sign in to comment.