All Downloads are FREE. Search and download functionalities are using the official Maven repository.

restx.specs.WhenHttpRequest Maven / Gradle / Ivy

There is a newer version: 1.2.0-rc2
Show newest version
package restx.specs;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

import static com.google.common.collect.Maps.newLinkedHashMap;
import static restx.common.MoreStrings.indent;

/**
* @author xavierhanin
*/
public class WhenHttpRequest extends When {
    public static final String CONTEXT_NAME = "WhenHttpRequest.CONTEXT_NAME";
    public static final String BASE_URL = "WhenHttpRequest.BASE_URL";

    private final String method;
    private final String path;
    private final String body;
    private final ImmutableMap cookies;

    public WhenHttpRequest(String method, String path, Map cookies, String body, ThenHttpResponse then) {
        super(then);
        this.method = method;
        this.path = path;
        this.body = body;
        this.cookies = ImmutableMap.copyOf(cookies);
    }

    @Override
    public void toString(StringBuilder sb) {
        if (Strings.isNullOrEmpty(body) && cookies.isEmpty()) {
            sb.append("  - when: ").append(method).append(" ").append(path).append("\n");
        } else {
            sb.append("  - when: |\n")
                    .append("       ").append(method).append(" ").append(path).append("\n");
            if (!cookies.isEmpty()) {
                sb.append("       Cookie: ");
                for (Map.Entry entry : cookies.entrySet()) {
                    sb.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                sb.setLength(sb.length() - 2);
                sb.append("\n");
            }
            if (!Strings.isNullOrEmpty(body)) {
                sb.append("\n").append(indent(body.trim(), 8)).append("\n");
            }
        }
        getThen().toString(sb);
    }

    public String getMethod() {
        return method;
    }

    public String getPath() {
        return path;
    }

    public String getBody() {
        return body;
    }

    public ImmutableMap getCookies() {
        return cookies;
    }

    @Override
    public When withThen(ThenHttpResponse then) {
        return new WhenHttpRequest(method, path, cookies, body, then);
    }

    public static Builder builder(){
        return new Builder();
    }

    public static class Builder {
        private String method;
        private String path;
        private String body;
        private Map cookies = newLinkedHashMap();
        private ThenHttpResponse response;

        public Builder withMethod(String method) {
            this.method = method;
            return this;
        }

        public Builder withPath(String path) {
            this.path = path;
            return this;
        }

        public Builder withBody(String body) {
            this.body = body;
            return this;
        }

        public Builder addCookie(String key, String value) {
            this.cookies.put(key, value);
            return this;
        }

        public boolean containsCookie(String key) {
            return this.cookies.containsKey(key);
        }

        public Builder withThen(ThenHttpResponse response) {
            this.response = response;
            return this;
        }

        public WhenHttpRequest build() {
            return new WhenHttpRequest(method, path, cookies, body, response);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy