Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Response code handling #37

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,31 @@ public void setResponseHeaders(Map<String, String> responseHeaders) {
}

public boolean isSuccessful() {
return statusCode / 100 == 2;
return statusCodeFamily(2);
}

public boolean isFaliure() {
return is3xx() || is4xx() || is5xx();
}

public boolean is1xx() {
return statusCodeFamily(1);
}

public boolean is3xx() {
return statusCodeFamily(3);
}

public boolean is4xx() {
return statusCodeFamily(4);
}

public boolean is5xx() {
return statusCodeFamily(5);
}

private boolean statusCodeFamily(int family) {
return statusCode / 100 == family;
}

public void addResponseHeader(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,54 @@ void addResponseHeader_AppendsTheUniqueHeaders() {
response.addResponseHeader("key2", "value3");

assertThat(response.getResponseHeaders()).containsEntry("key1", "value1").containsEntry("key2", "value3");
}

@ParameterizedTest(name = "given{0}StatusCode_isFailureReturnsTrue")
@ValueSource(ints = {500, 404, 302})
void isFailure_GivenFailureStatusCode_ReturnsTrue(int statusCode) {
Response response = new Response();
response.setStatusCode(statusCode);

assertThat(response.isFaliure()).isTrue();
}

@Test
void isFailure_Given201StatusCode_ReturnsFalse() {
Response response = new Response();
response.setStatusCode(201);

assertThat(response.isFaliure()).isFalse();
}

@Test
void is1xx_given101StatusCode_ReturnsTrue() {
Response response = new Response();
response.setStatusCode(101);

assertThat(response.is1xx()).isTrue();
}

@Test
void is3xx_given303StatusCode_ReturnsTrue() {
Response response = new Response();
response.setStatusCode(303);

assertThat(response.is3xx()).isTrue();
}

@Test
void is4xx_given404StatusCode_ReturnsTrue() {
Response response = new Response();
response.setStatusCode(404);

assertThat(response.is4xx()).isTrue();
}

@Test
void is5xx_given505StatusCode_ReturnsTrue() {
Response response = new Response();
response.setStatusCode(505);

assertThat(response.is5xx()).isTrue();
}
}
Loading