From eff8f60c082c64a9d29eb0bc2dc5d0e84c912624 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 19 Aug 2016 18:11:19 +0200 Subject: [PATCH] SONAR-7825 make SystemInfoHttpAction support only GET --- .../ce/systeminfo/SystemInfoHttpAction.java | 8 ++++++- .../systeminfo/SystemInfoHttpActionTest.java | 22 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/systeminfo/SystemInfoHttpAction.java b/server/sonar-ce/src/main/java/org/sonar/ce/systeminfo/SystemInfoHttpAction.java index 59bba1c6e96..980344546f7 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/systeminfo/SystemInfoHttpAction.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/systeminfo/SystemInfoHttpAction.java @@ -26,6 +26,8 @@ import org.sonar.ce.httpd.HttpAction; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import static fi.iki.elonen.NanoHTTPD.MIME_PLAINTEXT; +import static fi.iki.elonen.NanoHTTPD.Response.Status.*; import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse; public class SystemInfoHttpAction implements HttpAction { @@ -46,12 +48,16 @@ public class SystemInfoHttpAction implements HttpAction { @Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { + if (session.getMethod() != NanoHTTPD.Method.GET) { + return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); + } + ProtobufSystemInfo.SystemInfo.Builder infoBuilder = ProtobufSystemInfo.SystemInfo.newBuilder(); for (SystemInfoSection sectionProvider : sectionProviders) { ProtobufSystemInfo.Section section = sectionProvider.toProtobuf(); infoBuilder.addSections(section); } byte[] bytes = infoBuilder.build().toByteArray(); - return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, PROTOBUF_MIME_TYPE, new ByteArrayInputStream(bytes), bytes.length); + return newFixedLengthResponse(OK, PROTOBUF_MIME_TYPE, new ByteArrayInputStream(bytes), bytes.length); } } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/systeminfo/SystemInfoHttpActionTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/systeminfo/SystemInfoHttpActionTest.java index cf0d5b31974..2561872e9ea 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/systeminfo/SystemInfoHttpActionTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/systeminfo/SystemInfoHttpActionTest.java @@ -30,9 +30,13 @@ import org.sonar.process.systeminfo.ProcessStateSystemInfo; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import static fi.iki.elonen.NanoHTTPD.Method.GET; +import static fi.iki.elonen.NanoHTTPD.Method.POST; +import static fi.iki.elonen.NanoHTTPD.Response.Status.*; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class SystemInfoHttpActionTest { @@ -58,13 +62,25 @@ public class SystemInfoHttpActionTest { } @Test - public void start_starts_http_server_and_publishes_URL_in_IPC() throws Exception { - NanoHTTPD.Response response = underTest.serve(mock(NanoHTTPD.IHTTPSession.class)); - assertThat(response.getStatus()).isEqualTo(NanoHTTPD.Response.Status.OK); + public void serves_METHOD_NOT_ALLOWED_error_when_method_is_not_GET() { + NanoHTTPD.Response response = underTest.serve(createHttpSession(POST)); + assertThat(response.getStatus()).isEqualTo(METHOD_NOT_ALLOWED); + } + + @Test + public void serves_data_from_SystemInfoSections() throws Exception { + NanoHTTPD.Response response = underTest.serve(createHttpSession(GET)); + assertThat(response.getStatus()).isEqualTo(OK); ProtobufSystemInfo.SystemInfo systemInfo = ProtobufSystemInfo.SystemInfo.parseFrom(response.getData()); assertThat(systemInfo.getSectionsCount()).isEqualTo(2); assertThat(systemInfo.getSections(0).getName()).isEqualTo("state1"); assertThat(systemInfo.getSections(1).getName()).isEqualTo("state2"); } + private NanoHTTPD.IHTTPSession createHttpSession(NanoHTTPD.Method method) { + NanoHTTPD.IHTTPSession res = mock(NanoHTTPD.IHTTPSession.class); + when(res.getMethod()).thenReturn(method); + return res; + } + } -- 2.39.5