@Test public void serves_BAD_REQUEST_error_when_value_of_parameter_level_is_not_LEVEL_in_uppercase() throws IOException { NanoHTTPD.Response response = underTest.serve(createHttpSession(POST, ImmutableMap.of("level", "info"))); assertThat(response.getStatus()).isEqualTo(BAD_REQUEST); assertThat(IOUtils.toString(response.getData())).isEqualTo("Value 'info' for parameter 'level' is invalid"); }
@Test public void shouldRouteToIsConnectedToServerV1Handler() throws Exception { when(session.getMethod()).thenReturn(NanoHTTPD.Method.GET); when(session.getUri()).thenReturn("/health/v1/isConnectedToServer"); when(agentHealthHolder.hasLostContact()).thenReturn(false); NanoHTTPD.Response response = this.agentStatusHttpd.serve(session); assertThat(response.getStatus(), is(NanoHTTPD.Response.Status.OK)); assertThat(response.getMimeType(), is("text/plain; charset=utf-8")); assertThat(IOUtils.toString(response.getData(), StandardCharsets.UTF_8), is("OK!")); }
@Test public void shouldRouteToIsConnectedToServerHandler() throws Exception { when(session.getMethod()).thenReturn(NanoHTTPD.Method.GET); when(session.getUri()).thenReturn("/health/latest/isConnectedToServer"); when(agentHealthHolder.hasLostContact()).thenReturn(false); NanoHTTPD.Response response = this.agentStatusHttpd.serve(session); assertThat(response.getStatus(), is(NanoHTTPD.Response.Status.OK)); assertThat(response.getMimeType(), is("text/plain; charset=utf-8")); assertThat(IOUtils.toString(response.getData(), StandardCharsets.UTF_8), is("OK!")); }
@Test public void changes_server_logging_if_level_is_TRACE() { NanoHTTPD.Response response = underTest.serve(createHttpSession(POST, ImmutableMap.of("level", "TRACE"))); assertThat(response.getStatus()).isEqualTo(OK); verify(serverLogging).changeLevel(LoggerLevel.TRACE); } }
@Test public void serves_BAD_REQUEST_error_when_parameter_level_is_missing() throws IOException { NanoHTTPD.Response response = underTest.serve(createHttpSession(POST)); assertThat(response.getStatus()).isEqualTo(BAD_REQUEST); assertThat(IOUtils.toString(response.getData())).isEqualTo("Parameter 'level' is missing"); }
@Test public void changes_server_logging_if_level_is_INFO() { NanoHTTPD.Response response = underTest.serve(createHttpSession(POST, ImmutableMap.of("level", "INFO"))); assertThat(response.getStatus()).isEqualTo(OK); verify(serverLogging).changeLevel(LoggerLevel.INFO); }
@Test public void changes_server_logging_if_level_is_ERROR() { NanoHTTPD.Response response = underTest.serve(createHttpSession(POST, ImmutableMap.of("level", "ERROR"))); assertThat(response.getStatus()).isEqualTo(OK); verify(serverLogging).changeLevel(LoggerLevel.ERROR); }
@Test 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 changes_server_logging_if_level_is_DEBUG() { NanoHTTPD.Response response = underTest.serve(createHttpSession(POST, ImmutableMap.of("level", "DEBUG"))); assertThat(response.getStatus()).isEqualTo(OK); verify(serverLogging).changeLevel(LoggerLevel.DEBUG); }
@Test public void serves_METHOD_NOT_ALLOWED_error_when_method_is_not_POST() { NanoHTTPD.Response response = underTest.serve(createHttpSession(GET)); assertThat(response.getStatus()).isEqualTo(METHOD_NOT_ALLOWED); }
@Test public void shouldReturnNotFoundForBadUrl() throws Exception { when(session.getMethod()).thenReturn(NanoHTTPD.Method.GET); when(session.getUri()).thenReturn("/foo"); NanoHTTPD.Response response = this.agentStatusHttpd.serve(session); assertThat(response.getStatus(), is(NanoHTTPD.Response.Status.NOT_FOUND)); assertThat(response.getMimeType(), is("text/plain; charset=utf-8")); assertThat(IOUtils.toString(response.getData(), StandardCharsets.UTF_8), is("The page you requested was not found")); }
@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"); }
@Test public void shouldReturnMethodNotAllowedOnNonGetNonHeadRequests() throws Exception { when(session.getMethod()).thenReturn(NanoHTTPD.Method.POST); NanoHTTPD.Response response = this.agentStatusHttpd.serve(session); assertThat(response.getStatus(), is(NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED)); assertThat(response.getMimeType(), is("text/plain; charset=utf-8")); assertThat(IOUtils.toString(response.getData(), StandardCharsets.UTF_8), is("This method is not allowed. Please use GET or HEAD.")); }