From 52817ab13a9ca91cf0582426f8c1e1ec6e9704e6 Mon Sep 17 00:00:00 2001 From: Jose Fernandez Duque Date: Mon, 9 May 2022 12:59:18 +0200 Subject: [PATCH] Updated step so Rest client from rest assured is not initialized using a port if the user does not specify one --- .../java/com/privalia/qa/specs/RestSpec.java | 48 +++++++++---------- .../resources/features/restAssured.feature | 4 +- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/privalia/qa/specs/RestSpec.java b/src/main/java/com/privalia/qa/specs/RestSpec.java index 9c4f1b8a..3d4eeae1 100644 --- a/src/main/java/com/privalia/qa/specs/RestSpec.java +++ b/src/main/java/com/privalia/qa/specs/RestSpec.java @@ -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' * } * * * @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) { @@ -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)); } /** @@ -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); } } diff --git a/src/test/resources/features/restAssured.feature b/src/test/resources/features/restAssured.feature index 11f18380..f8a1013a 100644 --- a/src/test/resources/features/restAssured.feature +++ b/src/test/resources/features/restAssured.feature @@ -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'