Skip to content

Commit

Permalink
chore: Skip ResponseWithBodiesLinter for 204 and 304
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed Jun 2, 2024
1 parent ca77233 commit 1249fbf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@LinterFuzzer
public class ResponsesWithBodiesLinterFuzzer extends BaseLinterFuzzer {
private final PrettyLogger log = PrettyLoggerFactory.getLogger(this.getClass());
private static final List<String> SKIPPED_CODES = List.of("204", "304");

/**
* Creates a new ResponsesWithBodiesLinterFuzzer instance.
Expand All @@ -36,6 +37,7 @@ public void process(FuzzingData data) {
List<String> httpResponseCodesMissingBody = data.getResponses()
.entrySet()
.stream()
.filter(entry -> !SKIPPED_CODES.contains(entry.getKey()))
.filter(entry -> entry.getValue().isEmpty())
.map(Map.Entry::getKey)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.endava.cats.args.IgnoreArguments;
import com.endava.cats.args.ReportingArguments;
import com.endava.cats.context.CatsGlobalContext;
import com.endava.cats.http.HttpMethod;
import com.endava.cats.model.FuzzingData;
import com.endava.cats.report.ExecutionStatisticsListener;
import com.endava.cats.report.TestCaseExporter;
Expand All @@ -13,6 +14,8 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mockito;

import java.util.List;
Expand Down Expand Up @@ -53,6 +56,15 @@ void shouldReturnError() {
Mockito.eq("Missing response body for some HTTP response codes"), Mockito.eq("The following HTTP response codes are missing a response body: {}"), Mockito.eq(List.of("200")));
}

@ParameterizedTest
@CsvSource({"204", "304"})
void shouldSkipCodes(String code) {
FuzzingData data = FuzzingData.builder().responses(Map.of(code, List.of())).build();
responsesWithBodiesLinterFuzzer.fuzz(data);

Mockito.verify(testCaseListener, Mockito.times(1)).reportResultInfo(Mockito.any(), Mockito.any(), Mockito.eq("All HTTP response codes have a response body"));
}

@Test
void shouldReturnSimpleClassNameForToString() {
Assertions.assertThat(responsesWithBodiesLinterFuzzer).hasToString(responsesWithBodiesLinterFuzzer.getClass().getSimpleName());
Expand All @@ -62,4 +74,9 @@ void shouldReturnSimpleClassNameForToString() {
void shouldReturnMeaningfulDescription() {
Assertions.assertThat(responsesWithBodiesLinterFuzzer.description()).isEqualTo("verifies that HTTP response codes (except for 204 and 304) have a response body");
}

@Test
void shouldSkipForHttpHead() {
Assertions.assertThat(responsesWithBodiesLinterFuzzer.skipForHttpMethods()).containsOnly(HttpMethod.HEAD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ void shouldGenerate() {
Assertions.assertThat(phoneNumberGenerator.generate(new Schema<>()).toString()).hasSizeGreaterThan(1);
}

@Test
void shouldGenerateWithPlusWhenPlusPattern() {
@ParameterizedTest
@CsvSource(value = {"^\\+?[1-9]\\d{6,14}$", "\\+?[1-9]\\d{6,14}$"}, delimiter = ';')
void shouldGenerateWithPlusWhenPlusPattern(String pattern) {
PhoneNumberGenerator phoneNumberGenerator = new PhoneNumberGenerator();
StringSchema schema = new StringSchema();
schema.setPattern("^\\+?[1-9]\\d{6,14}$");
schema.setPattern(pattern);
Assertions.assertThat(phoneNumberGenerator.generate(schema).toString()).startsWith("+4");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,10 @@ void shouldGenerateWhenNegativeLength() {
String generated = StringGenerator.generate("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", -1, -1);
Assertions.assertThat(generated).hasSize(17).matches("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
}

@Test
void shouldGenerateWhenOrEmpty() {
String generated = StringGenerator.generate("^$|^(?i)(http:\\/\\/|https:\\/\\/)([a-z0-9./\\-_.~+=:;%&?]+)$", 100, 100);
Assertions.assertThat(generated).hasSize(100).matches("^(?i)(http://|https://)([a-z0-9./\\-_.~+=:;%&?]+)");
}
}

0 comments on commit 1249fbf

Please sign in to comment.