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

org.webbitserver.stub.StubHttpRequest Maven / Gradle / Ivy

package org.webbitserver.stub;

import org.webbitserver.HttpRequest;
import org.webbitserver.helpers.InboundCookieParser;
import org.webbitserver.helpers.QueryParameters;

import java.net.HttpCookie;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Implementation of HttpRequest that is easy to construct manually and populate.
 * Useful for testing.
 */
public class StubHttpRequest extends StubDataHolder implements HttpRequest {

    private String uri = "/";
    private String method = "GET";
    private List> headers = new ArrayList>();
    private SocketAddress remoteAddress = new InetSocketAddress("localhost", 0);
    private Object id = "StubID";
    private long timestamp = 0;
    private String body;

    public StubHttpRequest() {
    }

    public StubHttpRequest(String uri) {
        this.uri = uri;
    }

    @Override
    public String uri() {
        return uri;
    }

    @Override
    public StubHttpRequest uri(String uri) {
        this.uri = uri;
        return this;
    }

    @Override
    public String header(String name) {
        for (Map.Entry header : headers) {
            if (header.getKey().equals(name)) {
                return header.getValue();
            }
        }
        return null;
    }

    @Override
    public boolean hasHeader(String name) {
        for (Map.Entry header : headers) {
            if (header.getKey().equals(name)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List cookies() {
        return InboundCookieParser.parse(headers(COOKIE_HEADER));
    }

    @Override
    public HttpCookie cookie(String name) {
        for (HttpCookie cookie : cookies()) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }
        }
        return null;
    }

    @Override
    public String queryParam(String key) {
        return new QueryParameters(URI.create(uri()).getQuery()).first(key);
    }

    @Override
    public List queryParams(String key) {
        return new QueryParameters(URI.create(uri()).getQuery()).all(key);
    }

    @Override
    public Set queryParamKeys() {
        return new QueryParameters(URI.create(uri()).getQuery()).keys();
    }

    @Override
    public String postParam(String key) {
        return new QueryParameters(body()).first(key);
    }

    @Override
    public List postParams(String key) {
        return new QueryParameters(body()).all(key);
    }

    @Override
    public Set postParamKeys() {
        return new QueryParameters(body()).keys();
    }

    @Override
    public String cookieValue(String name) {
        HttpCookie cookie = cookie(name);
        return cookie == null ? null : cookie.getValue();
    }

    @Override
    public List headers(String name) {
        List result = new ArrayList();
        for (Map.Entry header : headers) {
            if (header.getKey().equals(name)) {
                result.add(header.getValue());
            }
        }
        return result;
    }

    @Override
    public List> allHeaders() {
        return headers;
    }

    @Override
    public String method() {
        return method;
    }

    @Override
    public String body() {
        return body;
    }

    @Override
    public byte[] bodyAsBytes() {
        return body.getBytes();
    }

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

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

    public StubHttpRequest header(String name, String value) {
        headers.add(new AbstractMap.SimpleEntry(name, value));
        return this;
    }

    @Override
    public StubHttpRequest data(String key, Object value) {
        super.data(key, value);
        return this;
    }

    @Override
    public SocketAddress remoteAddress() {
        return remoteAddress;
    }

    @Override
    public Object id() {
        return id;
    }

    public StubHttpRequest id(Object id) {
        this.id = id;
        return this;
    }

    @Override
    public long timestamp() {
        return timestamp;
    }

    public StubHttpRequest timestamp(long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public StubHttpRequest remoteAddress(SocketAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy