diff --git a/src/main/java/de/malkusch/km200/KM200.java b/src/main/java/de/malkusch/km200/KM200.java index f91574b..1bba3f4 100644 --- a/src/main/java/de/malkusch/km200/KM200.java +++ b/src/main/java/de/malkusch/km200/KM200.java @@ -246,14 +246,20 @@ private HttpResponse send(HttpRequest request, BodyHandler bodyHandler throws IOException, InterruptedException, KM200Exception { var response = http.send(request, bodyHandler); - return switch (response.statusCode()) { - case 200 -> response; - case 403 -> throw new KM200Exception.Forbidden(request.uri() + " is forbidden"); - case 404 -> throw new KM200Exception.NotFound(request.uri() + " was not found"); - case 423 -> throw new KM200Exception.Locked(request.uri() + " was locked"); - case 500 -> throw new KM200Exception.ServerError(request.uri() + " resulted in a server error"); - default -> throw new KM200Exception(request.uri() + " failed with response code " + response.statusCode()); - }; + var status = response.statusCode(); + + if (status >= 200 && status <= 299) { + return response; + + } else { + throw switch (status) { + case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden"); + case 404 -> new KM200Exception.NotFound(request.uri() + " was not found"); + case 423 -> new KM200Exception.Locked(request.uri() + " was locked"); + case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error"); + default -> new KM200Exception(request.uri() + " failed with response code " + status); + }; + } } public double queryDouble(String path) throws KM200Exception, IOException, InterruptedException { diff --git a/src/test/java/de/malkusch/km200/KM200Test.java b/src/test/java/de/malkusch/km200/KM200Test.java index bf4437b..ce461d5 100644 --- a/src/test/java/de/malkusch/km200/KM200Test.java +++ b/src/test/java/de/malkusch/km200/KM200Test.java @@ -53,6 +53,26 @@ public void stubSystem() throws Exception { stubFor(get("/system").willReturn(ok(loadBody("system")))); } + @ParameterizedTest + @ValueSource(ints = { 200, 201, 204, 299 }) + public void updateShouldSucceedWith2xx(int status) throws Exception { + stubFor(post("/update").willReturn(status(status))); + var km200 = new KM200(URI, TIMEOUT, GATEWAY_PASSWORD, PRIVATE_PASSWORD, SALT); + + km200.update("/update", 42); + } + + @ParameterizedTest + @ValueSource(ints = { 200, 201, 299 }) + public void queryShouldSucceedWith2xx(int status) throws Exception { + stubFor(get("/query").willReturn(status(status).withBody(loadBody("gateway.DateTime")))); + var km200 = new KM200(URI, TIMEOUT, GATEWAY_PASSWORD, PRIVATE_PASSWORD, SALT); + + var dateTime = km200.queryString("/query"); + + assertEquals("2021-09-21T10:49:25", dateTime); + } + @ParameterizedTest @ValueSource(strings = { "http://localhost:" + PORT, "http://localhost:" + PORT + "/" }) public void queryShouldReplaceSlashesInURI(String uri) throws Exception {