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

org.mockserver.model.HttpResponse Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.model;

import com.google.common.base.Charsets;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.mockserver.client.serialization.Base64Converter;
import org.mockserver.client.serialization.ObjectMapperFactory;

import java.util.*;

/**
 * @author jamesdbloom
 */
public class HttpResponse extends Action {
    private Integer statusCode = 200;
    private byte[] body = new byte[0];
    private Map headers = new LinkedHashMap();
    private Map cookies = new LinkedHashMap();
    private Delay delay;

    public HttpResponse() {
    }

    public static HttpResponse response() {
        return new HttpResponse();
    }

    /**
     * The status code to return, such as 200, 404, the status code specified
     * here will result in the default status message for this status code for
     * example for 200 the status message "OK" is used
     *
     * @param statusCode an integer such as 200 or 404
     */
    public HttpResponse withStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
        return this;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    /**
     * The response body
     *
     * @param body a UTF-8 string
     */
    public HttpResponse withBody(String body) {
        if (body != null) {
            withBody(body.getBytes());
        } else {
            withBody(new byte[0]);
        }
        return this;
    }

    public HttpResponse withBody(byte[] body) {
        this.body = body != null ? Base64Converter.stringToBase64Bytes(body).getBytes() : null;
        return this;
    }

    public byte[] getBody() {
        if (body != null) {
            return Base64Converter.base64StringToBytes(new String(body));
        } else {
            return null;
        }
    }

    @JsonIgnore
    public String getBodyAsString() {
        if (body == null) {
            return "";
        }
        return new String(body, Charsets.UTF_8);
    }

    /**
     * The headers to return as a list of Header objects
     *
     * @param headers a list of Header objects
     */
    public HttpResponse withHeaders(List
headers) { this.headers.clear(); for (Header header : headers) { withHeader(header); } return this; } /** * The headers to return as a varargs of Header objects * * @param headers a varargs of Header objects */ public HttpResponse withHeaders(Header... headers) { if (headers != null) { withHeaders(Arrays.asList(headers)); } return this; } /** * A header to return as a Header objects * * @param header a Header objects */ public HttpResponse withHeader(Header header) { if (this.headers.containsKey(header.getName())) { this.headers.get(header.getName()).addValues(header.getValues()); } else { this.headers.put(header.getName(), header); } return this; } public List
getHeaders() { return new ArrayList
(headers.values()); } /** * The cookies to return as Set-Cookie headers as a list of Cookie objects * * @param cookies a list of Cookie objects */ public HttpResponse withCookies(List cookies) { this.cookies.clear(); for (Cookie cookie : cookies) { withCookie(cookie); } return this; } /** * The cookies to return as Set-Cookie headers as a varargs of Cookie objects * * @param cookies a varargs of Cookie objects */ public HttpResponse withCookies(Cookie... cookies) { if (cookies != null) { withCookies(Arrays.asList(cookies)); } return this; } /** * Add cookie to return as Set-Cookie header * * @param cookie a Cookie object */ public HttpResponse withCookie(Cookie cookie) { if (this.cookies.containsKey(cookie.getName())) { this.cookies.get(cookie.getName()).addValues(cookie.getValues()); } else { this.cookies.put(cookie.getName(), cookie); } return this; } public List getCookies() { return new ArrayList(cookies.values()); } /** * The delay before responding with this request as a Delay object, for example new Delay(TimeUnit.SECONDS, 3) * * @param delay a Delay object, for example new Delay(TimeUnit.SECONDS, 3) */ public HttpResponse withDelay(Delay delay) { this.delay = delay; return this; } public Delay getDelay() { return delay; } @JsonIgnore public HttpResponse applyDelay() { if (delay != null) { delay.applyDelay(); } return this; } @Override public String toString() { try { return ObjectMapperFactory .createObjectMapper() .writerWithDefaultPrettyPrinter() .writeValueAsString(this); } catch (Exception e) { return super.toString(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy