org.zalando.logbook.MockHttpResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logbook-test Show documentation
Show all versions of logbook-test Show documentation
HTTP request and response logging
package org.zalando.logbook;
/*
* #%L
* Logbook: Test
* %%
* Copyright (C) 2015 Zalando SE
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static java.util.Collections.emptyMap;
@Immutable
public final class MockHttpResponse implements MockHttpMessage, HttpResponse {
private final String protocolVersion;
private final Origin origin;
private final int status;
private final Map> headers;
private final String contentType;
private final Charset charset;
private final String body;
@lombok.Builder(builderMethodName = "response", builderClassName = "Builder")
public MockHttpResponse(
@Nullable final String protocolVersion,
@Nullable final Origin origin,
final int status,
@Nullable final Map> headers,
@Nullable final String contentType,
@Nullable final Charset charset,
@Nullable final String body) {
this.protocolVersion = Optional.ofNullable(protocolVersion).orElse("HTTP/1.1");
this.origin = Optional.ofNullable(origin).orElse(Origin.LOCAL);
this.status = status == 0 ? 200 : status;
this.headers = firstNonNullNorEmpty(headers, emptyMap());
this.contentType = Optional.ofNullable(contentType).orElse("");
this.charset = Optional.ofNullable(charset).orElse(StandardCharsets.UTF_8);
this.body = Optional.ofNullable(body).orElse("");
}
@Override
public String getProtocolVersion() {
return protocolVersion;
}
@Override
public Origin getOrigin() {
return origin;
}
@Override
public int getStatus() {
return status;
}
@Override
public Map> getHeaders() {
return headers;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public Charset getCharset() {
return charset;
}
@Override
public byte[] getBody() {
return getBodyAsString().getBytes(getCharset());
}
@Override
public String getBodyAsString() {
return body;
}
static HttpResponse create() {
return response().build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy