Skip to content

Commit

Permalink
Updated step so Rest client from rest assured is not initialized usin…
Browse files Browse the repository at this point in the history
…g a port if the user does not specify one
  • Loading branch information
Jose Fernandez Duque authored and Jose Fernandez Duque committed May 9, 2022
1 parent a8441ef commit 52817ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
48 changes: 24 additions & 24 deletions src/main/java/com/privalia/qa/specs/RestSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ public RestSpec(CommonG spec) {
* {@code
* Examples
*
* Scenario: Setting up the host. Defaults to port 80
* Scenario: Setting up the host.
* Given I send requests to 'jsonplaceholder.typicode.com'
*
* Scenario: Setting up host and specific port
* Given I send requests to 'jsonplaceholder.typicode.com:8080'
*
* Scenario: using the keyword 'securely' to use https, defaults to port 443
* Scenario: using the keyword 'securely' to use https.
* Given I securely send requests to 'jsonplaceholder.typicode.com'
* }
* </pre>
*
* @param isSecured Indicates if https:// should be used (if false, defaults to http://)
* @param restHost Port where the API is running. Defaults to 80 if null
* @param restHost Base url of the API (without the http/https prefix, like mybaseurl.com). You can also specify a port number with baseURL:port
*/
@Given("^I( securely)? send requests to '(.*)'$")
public void setupApp(String isSecured, String restHost) {
Expand All @@ -94,40 +94,33 @@ public void setupApp(String isSecured, String restHost) {

if (isSecured != null) {
restProtocol = "https://";
commonspec.getRestRequest().relaxedHTTPSValidation();
}

if (restHost == null) {
restHost = "localhost";
}

Assertions.assertThat(restHost).isNotNull();
Assertions.assertThat(restHost).as("Malformed url. No need to use http(s):// prefix").doesNotContain("http://").doesNotContain("https://");
Assertions.assertThat(commonspec.getRestRequest()).as("No rest client initialized. Did you forget to use @rest annotation in your feature?").isNotNull();

String[] restAddress = restHost.split(":");

if (restAddress.length == 2) {
restHost = restAddress[0];
restPort = restAddress[1];
restPort = restPort.replace(":", "");
}

if (restPort == null) {
if (isSecured == null) {
restPort = "80";
} else {
restPort = "443";
}
this.getCommonSpec().getLogger().debug("Setting base URL to {}", restProtocol + restHost);
commonspec.getRestRequest().baseUri(restProtocol + restHost);

if (restPort != null) {
this.getCommonSpec().getLogger().debug("Setting port to {}", Integer.parseInt(restPort));
commonspec.getRestRequest().port(Integer.parseInt(restPort));
}

restPort = restPort.replace(":", "");
Assertions.assertThat(commonspec.getRestRequest()).as("No rest client initialized. Did you forget to use @rest annotation in your feature?").isNotNull();
commonspec.setRestHost(restHost);
commonspec.setRestPort(restPort);
commonspec.setRestProtocol(restProtocol);

if (restProtocol.matches("https://")) {
commonspec.getRestRequest().relaxedHTTPSValidation();
}

this.getCommonSpec().getLogger().debug("Setting base URL to {} with port {}", restProtocol + restHost, Integer.parseInt(restPort));
commonspec.getRestRequest().baseUri(restProtocol + restHost).port(Integer.parseInt(restPort));
}

/**
Expand Down Expand Up @@ -985,13 +978,20 @@ private void initializeRestClient() {

commonspec.getLogger().debug("Re-initializing rest-client. Removing headers, cookies and url parameters");

RequestSpecification spec = new RequestSpecBuilder().setContentType(ContentType.JSON).build();
RequestSpecification spec = new RequestSpecBuilder().setContentType(ContentType.JSON).setRelaxedHTTPSValidation().build();
commonspec.setRestRequest(given().header("Content-Type", "application/json").spec(spec));

String baseUrl;
if (commonspec.getRestPort() != null) {
baseUrl = commonspec.getRestHost() + ":" + commonspec.getRestPort();
} else {
baseUrl = commonspec.getRestHost();
}

if (commonspec.getRestProtocol().matches("https://")) {
this.setupApp("https://", commonspec.getRestHost() + ":" + commonspec.getRestPort());
this.setupApp("https://", baseUrl);
} else {
this.setupApp(null, commonspec.getRestHost() + ":" + commonspec.getRestPort());
this.setupApp(null, baseUrl);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/features/restAssured.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Feature: Rest Assured Feature

Rule: Set up initial base URI for future requests

Scenario: Setting up base URI for future requests using http (if port not specified, defaults to 80)
Scenario: Setting up base URI for future requests using http
Given I send requests to '${REST_SERVER_HOST}:3000'

Scenario: Setting up base URI for future requests using https (if port not specified, defaults to 443)
Scenario: Setting up base URI for future requests using https
Given I securely send requests to '${REST_SERVER_HOST}:3000'


Expand Down

0 comments on commit 52817ab

Please sign in to comment.