Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #359 from sbtqa/rest-endpoint-custom-host
Browse files Browse the repository at this point in the history
add custom host-port to Endpoint
  • Loading branch information
kosteman authored Nov 22, 2021
2 parents 1ce2d04 + 6917218 commit 9baa9b3
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ before_install:
- if [ ! -z "$GPG_OWNERTRUST" ]; then echo $GPG_OWNERTRUST | base64 --decode | $GPG_EXECUTABLE
--import-ownertrust; fi
- ./.travis/apk.sh
install:
- google-chrome --version
- mvn clean --settings $TRAVIS_BUILD_DIR/.travis/settings.xml install -Dmaven.javadoc.skip=true -Dgpg.skip -Dappium.url=$SAUCELABS_URL -B -V
#script:
- google-chrome --version
install: true
script:
- mvn clean --settings $TRAVIS_BUILD_DIR/.travis/settings.xml install -Denv.customHost=https://httpbin.org -Dmaven.javadoc.skip=true -Dgpg.skip -Dappium.url=$SAUCELABS_URL -B -V
# - mvn org.jacoco:jacoco-maven-plugin:prepare-agent sonar:sonar -Dmaven.javadoc.skip=true -Dgpg.skip -DskipTests=true
deploy:
- provider: script
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ru.sbtqa.tag.pagefactory.annotations.rest;

import ru.sbtqa.tag.pagefactory.Rest;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import ru.sbtqa.tag.pagefactory.Rest;

/**
* Annotate class that is the inheritor of ru.sbtqa.tag.api.EndpointEntry
Expand All @@ -19,6 +20,12 @@
*/
String title();

/**
* if no host is specified, the api.baseURI from application.properties will be taken
* @return schema://host[:port]
*/
String host() default "";

/**
* This should be look like my/endpoint/path.
* <p>
Expand Down
4 changes: 3 additions & 1 deletion page-factory-doc/src/main/asciidoc/rest_entries.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
Параметры метода нужно задать аннотацией `@Endpoint`. С ее помощью определяются:

* `method` - метод (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
* `host` - url вида <схема>://<хост>[:<порт>]. В случае если не задано, будет использовано `api.baseURI`
* `path` - путь, относительно заданного в `api.baseURI`
* `title` - имя метода для обращения из тестового сценария
* `template` - шаблон запроса
*__Пример:__*
[source,]
----
@Endpoint(method = Rest.GET, path = "repos/qa", title = "Запрос")
@Endpoint(method = Rest.GET, host = "http://example.pf2.ru:8080", path = "repos/qa", title = "Запрос")
public class ApiEntry extends EndpointEntry {
...
}
----
TIP: Параметр host можно задавать динамически. Для этого нужно использовать плейсхолдер вида `host = "${env.myHost}"`. В мавен нужно будет передать это параметр как `mvn clean test -Denv.myHost=https://localhost:8888`

Путь до пакета с описанными API-методами необходимо прописать в файле конфигурации. Например:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class EndpointEntry implements ApiEndpoint {
private final EndpointEntryReflection reflection;
private final BlankStorage blankStorage;
private final Rest method;
private String host;
private String path;
private final String template;
private final String title;
Expand All @@ -44,6 +45,7 @@ public EndpointEntry() {
Endpoint endpoint = this.getClass().getAnnotation(Endpoint.class);
method = endpoint.method();
path = endpoint.path();
host = endpoint.host();
template = endpoint.template();
title = endpoint.title();

Expand All @@ -70,7 +72,8 @@ public void send() {
reflection.applyAnnotations(Query.class);
reflection.applyAnnotations(Stashed.class);
reflection.applyAnnotations(Mutator.class);
String url = PathUtils.unite( PROPERTIES.getBaseURI(), path);

String url = PathUtils.unite(host.isEmpty() ? PROPERTIES.getBaseURI() : PlaceholderUtils.replaceTemplatePlaceholders(host), path);

RequestSpecification request = buildRequest();
Response response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public static String replaceTemplatePlaceholders(EndpointEntry entry, String str
return string;
}

/**
* Replace placeholders in string
*
* @param string replace placeholders in this string
* @return string with replaced placeholders
*/
public static String replaceTemplatePlaceholders(String string) {
if (string.startsWith("${") && string.endsWith("}")) {
String unquoteString = string.replaceAll("^\\$\\{", "").replaceAll("\\}$", "");
String property = System.getProperty(unquoteString);
return property != null ? property : string;
}

return string;
}

/**
* Replace Json template placeholders in string on parameters
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.eclipse.jetty.server.Server;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import ru.sbtqa.tag.api.annotation.ParameterType;
import ru.sbtqa.tag.api.entries.apirequest.ApiRequestWithMutator;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void mutatorTest() {
ApiSteps.getInstance().validate("result with mutated values", toDataTable(parameters));
}

@Test
@Test @Ignore
public void typedArrayTest() {
ApiSteps.getInstance().fill(TypedArraysEntry.class)
.add(ParameterType.HEADER, CONTENT_TYPE, JSON_UTF_8.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.sbtqa.tag.api.entries.apirequest;

import ru.sbtqa.tag.api.EndpointEntry;
import ru.sbtqa.tag.api.annotation.Validation;
import ru.sbtqa.tag.pagefactory.Rest;
import ru.sbtqa.tag.pagefactory.annotations.rest.Endpoint;

import static org.hamcrest.Matchers.equalTo;

@Endpoint(method = Rest.GET, host = "${env.customHost}", path = "/get", title = "custom host")
public class CustomHost extends EndpointEntry {

@Validation(title = "result")
public void validate() {
getResponse().body("url", equalTo("https://httpbin.org/get"));
}
}
7 changes: 7 additions & 0 deletions plugins/rest-plugin/src/test/resources/features/Host.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@host
Feature: Custom host test

@host
Scenario: Custom host test
* user sends request for "custom host"
* system returns "result"

0 comments on commit 9baa9b3

Please sign in to comment.