import static fi.iki.elonen.NanoHTTPD.Response.Status.INTERNAL_ERROR;
import static fi.iki.elonen.NanoHTTPD.Response.Status.NOT_FOUND;
import static java.lang.Integer.parseInt;
+import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
return "http://" + nanoHttpd.getHostname() + ":" + nanoHttpd.getListeningPort();
}
- private class CeNanoHttpd extends NanoHTTPD {
- private final NanoHTTPD.Response response404 = newFixedLengthResponse(NOT_FOUND, MIME_PLAINTEXT, "Error 404, not found.");
-
+ private static class CeNanoHttpd extends NanoHTTPD {
private final ActionRegistryImpl actionRegistry;
CeNanoHttpd(String hostname, int port, ActionRegistryImpl actionRegistry) {
public Response serve(IHTTPSession session) {
return actionRegistry.getAction(session)
.map(action -> serveFromAction(session, action))
- .orElse(response404);
+ .orElseGet(() -> newFixedLengthResponse(NOT_FOUND, MIME_PLAINTEXT, format("Error 404, '%s' not found.", session.getUri())));
}
- private Response serveFromAction(IHTTPSession session, HttpAction action) {
+ private static Response serveFromAction(IHTTPSession session, HttpAction action) {
try {
return action.serve(session);
} catch (Exception e) {
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
+import java.util.Arrays;
import java.util.Properties;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.DefaultProcessCommands;
-import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ private static final RuntimeException FAILING_ACTION = new IllegalStateException("Simulating the action failed");
private CeHttpServer underTest;
private File sharedDir;
Properties properties = new Properties();
properties.setProperty(PROPERTY_PROCESS_INDEX, "1");
properties.setProperty(PROPERTY_SHARED_PATH, sharedDir.getAbsolutePath());
- underTest = new CeHttpServer(properties, singletonList(new PomPomAction()));
+ underTest = new CeHttpServer(properties, Arrays.asList(new PomPomAction(), new FailingAction()));
underTest.start();
}
}
@Test
- public void unkownUrl_return_a_404() throws IOException {
- assertThat(call(underTest.getUrl() + "/dfdsfdsfsdsd").code()).isEqualTo(404);
+ public void return_http_response_with_code_404_and_exception_message_as_body_when_url_has_no_matching_action() throws IOException {
+ String action = "/dfdsfdsfsdsd";
+ Response response = call(underTest.getUrl() + action);
+
+ assertThat(response.code()).isEqualTo(404);
+ assertThat(response.body().string()).isEqualTo("Error 404, '" + action + "' not found.");
}
@Test
assertIsPomPomResponse(response);
}
- private void assertIsPomPomResponse(Response response) throws IOException {
- assertThat(response.code()).isEqualTo(200);
- assertThat(IOUtils.toString(response.body().byteStream())).isEqualTo("ok");
- }
-
@Test
public void stop_stops_http_server() throws Exception {
underTest.stop();
call(underTest.getUrl());
}
+ @Test
+ public void return_http_response_with_code_500_and_exception_message_as_body_when_action_throws_exception() throws IOException {
+ Response response = call(underTest.getUrl() + "/failing");
+
+ assertThat(response.code()).isEqualTo(500);
+ assertThat(response.body().string()).isEqualTo(FAILING_ACTION.getMessage());
+ }
+
+ private void assertIsPomPomResponse(Response response) throws IOException {
+ assertThat(response.code()).isEqualTo(200);
+ assertThat(IOUtils.toString(response.body().byteStream())).isEqualTo("ok");
+ }
+
private static Response call(String url) throws IOException {
Request request = new Request.Builder().get().url(url).build();
return new OkHttpClient().newCall(request).execute();
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, "ok");
}
}
+
+ private static class FailingAction implements HttpAction {
+
+ @Override
+ public void register(ActionRegistry registry) {
+ registry.register("failing", this);
+ }
+
+ @Override
+ public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) {
+ throw FAILING_ACTION;
+ }
+ }
}