fix(http): keep snapshot names in one path segment and report odd server responses clearly (#54)

* fix(http): keep snapshot names in one path segment and report odd server responses clearly

Snapshot names are URL-encoded and names that would change the path are refused; every trailing slash is stripped from the endpoint; a non-JSON body or a redirect gets a message that says so instead of a parser error; requests use HTTP/1.1. snapshot delete reports a missing snapshot API like save and load, save warns that --message is not sent yet, and export/import no longer cite a server version that does not exist.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(http): tell a missing snapshot API from its status, not from the error text

The error message carries the request path, so a snapshot named nightly404 turned a real server error into 'not available'. FlociException now carries the HTTP status and the snapshot commands ask that.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-10 21:39:43 -05:00
1 parent 1a7e84cd23
commit 24b4280f69
10 files changed
+289 -22

No files matched your search

@@ -0,0 +1,99 @@
package io.floci.cli.unit;
import com.sun.net.httpserver.HttpServer;
import io.floci.cli.http.FlociException;
import io.floci.cli.http.FlociHttpClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.*;
/** BL-047: what FlociHttpClient sends, and how it reports responses that are not what it expects. */
class FlociHttpClientRobustnessTest {
private HttpServer server;
private final AtomicReference<String> lastPath = new AtomicReference<>();
private final AtomicInteger status = new AtomicInteger(200);
private final AtomicReference<String> body = new AtomicReference<>("{\"version\":\"1.0\",\"services\":[]}");
private final AtomicReference<String> location = new AtomicReference<>();
@BeforeEach
void start() throws Exception {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> {
lastPath.set(exchange.getRequestURI().getRawPath());
if (location.get() != null) exchange.getResponseHeaders().add("Location", location.get());
byte[] bytes = body.get().getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status.get(), bytes.length == 0 ? -1 : bytes.length);
if (bytes.length > 0) {
try (OutputStream out = exchange.getResponseBody()) {
out.write(bytes);
}
}
exchange.close();
});
server.start();
}
@AfterEach
void stop() {
server.stop(0);
}
private String endpoint() {
return "http://127.0.0.1:" + server.getAddress().getPort();
}
@Test
void everyTrailingSlashIsStripped() throws Exception {
new FlociHttpClient(endpoint() + "///", "/_floci").health();
assertEquals("/_floci/health", lastPath.get());
}
@Test
void aBodyThatIsNotJsonIsReportedAsSuch() {
body.set("OK");
FlociException e = assertThrows(FlociException.class, () -> new FlociHttpClient(endpoint(), "/_floci").health());
assertTrue(e.getMessage().contains("is not JSON"), e.getMessage());
}
@Test
void aRedirectIsReportedNotFollowed() {
status.set(302);
body.set("");
location.set("http://elsewhere.example/_floci/health");
FlociException e = assertThrows(FlociException.class, () -> new FlociHttpClient(endpoint(), "/_floci").health());
assertTrue(e.getMessage().contains("redirected"), e.getMessage());
assertTrue(e.getMessage().contains("elsewhere.example"), e.getMessage());
}
@Test
void snapshotNamesAreOnePathSegment() throws Exception {
status.set(204);
body.set("");
new FlociHttpClient(endpoint(), "/_floci").deleteSnapshot("my snap?x");
assertEquals("/_floci/snapshots/my%20snap%3Fx", lastPath.get());
}
@Test
void snapshotNamesThatWouldChangeThePathAreRefused() {
FlociHttpClient client = new FlociHttpClient(endpoint(), "/_floci");
for (String bad : new String[]{"..", ".", "a/b", "../escape"}) {
FlociException e = assertThrows(FlociException.class, () -> client.deleteSnapshot(bad), bad);
assertTrue(e.getMessage().contains("Invalid snapshot name"), e.getMessage());
}
assertNull(lastPath.get(), "nothing reached the server");
}
}
@@ -0,0 +1,106 @@
package io.floci.cli.unit;
import com.sun.net.httpserver.HttpServer;
import io.floci.cli.FlociCli;
import io.floci.cli.config.ProfileStore;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
/** BL-048: the snapshot commands against a server that has no snapshot API (404). */
class SnapshotCommandsTest {
@TempDir
Path tempDir;
private HttpServer server;
private volatile int status = 404;
@BeforeEach
void start() throws Exception {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> {
exchange.sendResponseHeaders(status, -1);
exchange.close();
});
server.start();
}
@AfterEach
void stop() {
server.stop(0);
}
private String[] run(String... args) {
String[] all = new String[args.length + 4];
System.arraycopy(args, 0, all, 0, args.length);
all[args.length] = "--endpoint";
all[args.length + 1] = "http://127.0.0.1:" + server.getAddress().getPort();
all[args.length + 2] = "--container";
all[args.length + 3] = "floci-snapshot-test-none";
PrintStream out = System.out;
PrintStream err = System.err;
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
System.setOut(new PrintStream(outBuf));
System.setErr(new PrintStream(errBuf));
try {
int exit = FlociCli.buildCommandLine(new ProfileStore(tempDir)).execute(all);
return new String[]{String.valueOf(exit), outBuf.toString(), errBuf.toString()};
} finally {
System.setOut(out);
System.setErr(err);
}
}
@Test
void deleteSaysTheApiIsMissingLikeSaveAndLoad() {
String[] r = run("snapshot", "delete", "nightly");
assertEquals("1", r[0]);
assertTrue(r[2].contains("Snapshot API not available"), r[2]);
}
@Test
void aMessageIsNotDroppedSilently() {
String[] r = run("snapshot", "save", "nightly", "-m", "before upgrade");
assertTrue(r[2].contains("--message is not sent"), r[2]);
}
/**
* Only the status says the API is missing. A name that happens to contain "404" or "501" must
* not turn a real server error into "not available".
*/
@ParameterizedTest
@CsvSource({"delete, nightly404", "save, build-501", "load, 404"})
void aStatusLookingNameDoesNotHideARealError(String command, String name) {
status = 500;
String[] r = run("snapshot", command, name);
assertEquals("1", r[0]);
assertFalse(r[2].contains("Snapshot API not available"), r[2]);
assertTrue(r[2].contains("HTTP 500"), r[2]);
}
@ParameterizedTest
@CsvSource({"404", "501"})
void bothMissingEndpointStatusesReadAsNotAvailable(int missing) {
status = missing;
String[] r = run("snapshot", "delete", "nightly");
assertTrue(r[2].contains("Snapshot API not available"), r[2]);
}
}