Skip to content
Merged
8 changes: 4 additions & 4 deletions src/main/java/org/juv25d/http/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class HttpResponse {

private int statusCode;
private String statusText;
private Map<String, String> headers;
private final Map<String, String> headers;
private byte[] body;

public HttpResponse(){
public HttpResponse() {
this.statusCode = 200;
this.statusText = "OK";
this.headers = new LinkedHashMap<>();
Expand All @@ -23,8 +23,8 @@ public HttpResponse(){

public HttpResponse(int statusCode, String statusText, Map<String, String> headers, byte[] body) {
this.statusCode = statusCode;
this.statusText = statusText;
this.headers = headers != null ? new LinkedHashMap<>(headers) : new LinkedHashMap<>();
this.statusText = Objects.requireNonNull(statusText, "statusText must not be null");
this.headers = new LinkedHashMap<>(headers != null ? headers : Map.of());
this.body = body != null ? body.clone() : new byte[0];
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/juv25d/http/HttpResponseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.juv25d.http;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

class HttpResponseTest {

@Test
void defaultConstructor_hasSafeDefaults_andSetHeaderDoesNotThrow() {
HttpResponse response = new HttpResponse();

assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.statusText()).isEqualTo("OK");
assertThat(response.headers()).isNotNull();
assertThat(response.body()).isNotNull();
assertThat(response.body()).isEmpty();

assertThatCode(() -> response.setHeader("Content-Type", "text/plain"))
.doesNotThrowAnyException();

assertThat(response.headers()).containsEntry("Content-Type", "text/plain");
}
}