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 #108 from sbtqa/rest-form-fix
Browse files Browse the repository at this point in the history
Cookie and form
  • Loading branch information
kosteman authored Nov 23, 2018
2 parents e31f820 + 4be4513 commit 02b791a
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ private RequestSpecification buildRequest() {

request.queryParams(getQueryParameters());
request.headers(getHeaders());
request.cookies(getCookies());

if (!Rest.isBodiless(method)) {
if (!Rest.isBodiless(method) && !template.isEmpty()) {
request.body(getBody());
} else {
request.formParams(getForm());
}

return request;
Expand All @@ -120,14 +123,31 @@ private RequestSpecification buildRequest() {
return headers;
}

private String getBody() {
private Map<String, ?> getCookies() {
Map<String, Object> headers = new HashMap<>();

headers.putAll(reflection.getParameters(COOKIE));
headers.putAll(blankStorage.get(title).getCookies());

return headers;
}

public String getBody() {
String body = TemplateUtils.loadFromResources(this.getClass(), template, PROPERTIES.getTemplateEncoding());
return PlaceholderUtils.replacePlaceholders(body, getParameters());
}

public Map<String, Object> getForm() {
return getParameters();
}

public Map<String, Object> getParameters() {
Map<String, Object> parameters = new HashMap<>();

parameters.putAll(reflection.getParameters(BODY));
parameters.putAll(blankStorage.get(title).getBodies());

return PlaceholderUtils.replacePlaceholders(body, parameters);
return parameters;
}

public void validate(String title, Object... params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;
import ru.sbtqa.tag.api.annotation.Body;
import ru.sbtqa.tag.api.annotation.Cookie;
import ru.sbtqa.tag.api.annotation.Endpoint;
import ru.sbtqa.tag.api.annotation.FromResponse;
import ru.sbtqa.tag.api.annotation.Header;
Expand Down Expand Up @@ -89,6 +90,7 @@ public void setParameterValueByTitle(String name, String value) {
if (annotation instanceof Body && ((Body) annotation).name().equals(name)
|| annotation instanceof Query && ((Query) annotation).name().equals(name)
|| annotation instanceof Header && ((Header) annotation).name().equals(name)
|| annotation instanceof Cookie && ((Cookie) annotation).name().equals(name)
&& value != null && !value.isEmpty()) {
set(endpoint, field, value);
return;
Expand Down Expand Up @@ -162,6 +164,11 @@ public Map<String, Object> getParameters(ParameterType type) {
parameters.put(((Body) annotation).name(), get(endpoint, field));
}
break;
case COOKIE:
if (annotation instanceof Cookie) {
parameters.put(((Cookie) annotation).name(), get(endpoint, field));
}
break;
default:
throw new RestPluginException(format("Parameter type \"%s\" is not supported", type));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.sbtqa.tag.api.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotate field that must be substitute to request as cookie
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Cookie {

/**
* Cookie name
*/
String name();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package ru.sbtqa.tag.api.annotation;

public enum ParameterType {
HEADER, QUERY, BODY
HEADER, QUERY, BODY, COOKIE
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class EndpointBlank {

private String title;
private Map<String, Object> headers = new HashMap<>();
private Map<String, Object> cookies = new HashMap<>();
private Map<String, Object> queries = new HashMap<>();
private Map<String, String> bodies = new HashMap<>();

Expand All @@ -41,6 +42,9 @@ public void addParameter(ParameterType type, String name, String value) {
case BODY:
addBodyParameter(name, value);
break;
case COOKIE:
addCookie(name, value);
break;
}
}

Expand All @@ -52,6 +56,14 @@ public void addHeader(String name, Object value) {
headers.put(name, value);
}

public Map<String, Object> getCookies() {
return cookies;
}

private void addCookie(String name, String value) {
headers.put(name, value);
}

public Map<String, Object> getQueries() {
return queries;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ru.sbtqa.tag.api.endpoints;

import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PATCH;
Expand Down Expand Up @@ -90,6 +92,32 @@ public Response delete(
.build();
}

@POST
@Path("form")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response form(
@FormParam("id") int id,
@FormParam("name") String name,
@FormParam("email") String email
) {
SimpleResult result = new SimpleResult();
result.setResult(id + name + email);
return Response.ok(result)
.build();
}

@GET
@Path("cookie")
@Produces(MediaType.APPLICATION_JSON)
public Response cookie(@CookieParam(Default.COOKIE_NAME) String cookie) {
SimpleResult result = new SimpleResult();
result.setResult(cookie);

return Response.ok(result)
.build();
}

@POST
@Path("request-from-feature")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.sbtqa.tag.api.entries.form;

import ru.sbtqa.tag.api.EndpointEntry;
import ru.sbtqa.tag.api.Rest;
import ru.sbtqa.tag.api.annotation.Body;
import ru.sbtqa.tag.api.annotation.Endpoint;
import ru.sbtqa.tag.api.annotation.Validation;
import ru.sbtqa.tag.api.utils.Default;

import static org.hamcrest.Matchers.equalTo;

@Endpoint(method = Rest.POST, path = "client/form", title = "form")
public class FormEndpointEntry extends EndpointEntry {

@Body(name = "id")
private int id = Default.ID;

@Body(name = "name")
private String name = Default.NAME;

@Body(name = "email")
private String email = Default.EMAIL;

@Validation(title = "result")
public void validate() {
getResponse().body("result", equalTo(id + name + email));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.sbtqa.tag.api.entries.parameters;

import ru.sbtqa.tag.api.EndpointEntry;
import ru.sbtqa.tag.api.Rest;
import ru.sbtqa.tag.api.annotation.Cookie;
import ru.sbtqa.tag.api.annotation.Endpoint;
import ru.sbtqa.tag.api.annotation.Validation;
import ru.sbtqa.tag.api.utils.Default;

import static org.hamcrest.Matchers.equalTo;

@Endpoint(method = Rest.GET, path = "client/cookie", title = "cookie")
public class CookieEntry extends EndpointEntry {

@Cookie(name = Default.COOKIE_NAME)
private String cookie = Default.COOKIE_VALUE;

@Validation(title = "result")
public void validate() {
getResponse().body("result", equalTo(cookie));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public interface Default {
String EMAIL = "default_person@google.com";
String MASK = "@(\\w*)\\.";

String COOKIE_NAME = "cookie-name";
String COOKIE_VALUE = "cookie-value";

String QUERY_PARAMETER_NAME_1 = "query-parameter-name-1";
String QUERY_PARAMETER_NAME_2 = "query-parameter-name-2";
String QUERY_PARAMETER_VALUE_1 = "query-parameter-value-1";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#language:en
@cookie
Feature: Send get with cookie

@cookie
Scenario: Send get with cookie
* user sends request for "cookie"
* system returns "result"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#language:en
@form
Feature: Send post with form parameters

@form
Scenario: Send post with form parameters
* user sends request for "form"
* system returns "result"

0 comments on commit 02b791a

Please sign in to comment.