Skip to content
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
10 changes: 8 additions & 2 deletions restest-gen/src/main/java/io/restest/gen/Mutations.java
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,16 @@ private static String items(int howMany) {
return howMany + (howMany == 1 ? " item" : " items");
}

/**
* The value as JSON, cut after so many characters. A character outside the most common ones,
* an emoji say, is two halves in a Java string, and cutting between them would leave half a
* character that no file can hold: the cut counts whole characters instead.
*/
private static String quoted(JsonValue value) {
String written = JsonText.write(value);
return written.length() <= QUOTED_AT_MOST
? written : written.substring(0, QUOTED_AT_MOST) + "...";
return written.codePointCount(0, written.length()) <= QUOTED_AT_MOST
? written
: written.substring(0, written.offsetByCodePoints(0, QUOTED_AT_MOST)) + "...";
}

/**
Expand Down
26 changes: 26 additions & 0 deletions restest-gen/src/test/java/io/restest/gen/MutationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,32 @@ void against_the_pattern() {
.contains("which its pattern ^[a-z]+$ refuses");
});
}

@Test
@DisplayName("a long word is quoted cut between two characters, never inside an emoji")
void quoted_whole_characters() {
// Written as JSON, the quote and thirty-eight letters come first, so the fortieth
// character, where the quote stops, is the emoji - two halves in a Java string.
String word = "x".repeat(38) + "😀";
Operation find = Operation.of(HttpMethod.GET, "/moods", List.of(
Parameter.of("mood", ParameterLocation.QUERY, true,
new StringSchema(SchemaMetadata.none().withEnumeration(
List.of(JsonValue.of(word))), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty()))))
.withId(OperationId.of("findMoods"));

List<TestCase> changed = changesIn(find, accepted(find,
value("mood", ParameterLocation.QUERY, JsonValue.of(word))),
"breakAnEnumeration");

assertThat(changed).extracting(each -> each.mutation().orElseThrow().description())
.isNotEmpty()
.allSatisfy(description -> assertThat(description.codePoints())
.describedAs("half a character, which no file can hold")
.noneMatch(point -> Character.getType(point) == Character.SURROGATE))
.anySatisfy(description -> assertThat(description)
.contains("\"" + "X".repeat(38) + "😀..."));
}
}

@Nested
Expand Down
14 changes: 13 additions & 1 deletion restest-report/src/main/java/io/restest/report/WholeFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
*/
package io.restest.report;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -61,7 +66,14 @@ static Path whileBeingWritten(Path file) {
static void write(Path file, String what, Content content) {
Path partial = whileBeingWritten(file);
try {
try (Writer out = Files.newBufferedWriter(partial)) {
// Half a character - the first or last half of an emoji, say, from text cut in the
// wrong place - cannot be written in UTF-8. The writer Files hands out refuses it and
// takes the whole report with it; this one writes a question mark in its place.
CharsetEncoder utf8 = StandardCharsets.UTF_8.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
try (Writer out = new BufferedWriter(
new OutputStreamWriter(Files.newOutputStream(partial), utf8))) {
content.writeTo(out);
}
try {
Expand Down
21 changes: 21 additions & 0 deletions restest-report/src/test/java/io/restest/report/HtmlReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.restest.core.json.JsonText;
import io.restest.core.json.JsonValue;
import io.restest.core.model.SpecificationIssue;
import io.restest.core.oracle.Finding;
import io.restest.core.oracle.WfcFault;
import io.restest.core.settings.Settings;
import io.restest.core.settings.SettingsInEffect;
import java.io.IOException;
Expand Down Expand Up @@ -103,6 +105,25 @@ void a_hostile_reply_cannot_break_out(@TempDir Path directory) throws IOExceptio
.contains("alert(1)");
}

@Test
@DisplayName("half a character in the run, which UTF-8 cannot hold, is written as a question "
+ "mark, and both files are still written")
void half_a_character_is_written_as_a_question_mark(@TempDir Path directory)
throws IOException {
Path json = directory.resolve("report.json");
Path page = directory.resolve("report.html");
HtmlReport report = HtmlReport.around(JsonReport.to(json), page);
report.on(Runs.started(WHEN));
// The first half of an emoji, and nothing after it: what cutting a quote in the wrong
// place leaves.
report.on(new RunEvent.FaultFound(WHEN, Finding.of(WfcFault.HTTP_STATUS_500,
Runs.attempt("GET /pets", "/pets", 500), "sent x\uD83D... for q")));
report.on(new RunEvent.RunFinished(WHEN, Duration.ofSeconds(1), Runs.engine()));

assertThat(Files.readString(json)).contains("sent x?... for q");
assertThat(theRunIn(Files.readString(page))).contains("sent x?... for q");
}

@Test
@DisplayName("what changes in the run's text is only what has to, written so that JSON reads "
+ "it back the same")
Expand Down
Loading