public String getHTTPHeader() { return super.getHTTPHeader() + "; path=/"; } }
NanoHTTPD.Response process() { if (isPassed()) { return NanoHTTPD.newFixedLengthResponse(Status.OK, "text/plain; charset=utf-8", "OK!"); } else { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, "text/plain; charset=utf-8", "Bad!"); } }
Optional<HttpAction> getAction(NanoHTTPD.IHTTPSession session) { String path = session.getUri().substring(1).toLowerCase(Locale.ENGLISH); return Optional.ofNullable(actionsByPath.get(path)); } }
@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")); }
@Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { if (session.getMethod() != NanoHTTPD.Method.POST) { return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); } String levelStr = session.getParms().get(PARAM_LEVEL); if (levelStr == null || levelStr.isEmpty()) { return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Parameter '%s' is missing", PARAM_LEVEL)); } try { LoggerLevel level = LoggerLevel.valueOf(levelStr); logging.changeLevel(level); return newFixedLengthResponse(OK, MIME_PLAINTEXT, null); } catch (IllegalArgumentException e) { Loggers.get(ChangeLogLevelHttpAction.class).debug("Value '{}' for parameter '" + PARAM_LEVEL + "' is invalid: {}", levelStr, e); return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Value '%s' for parameter '%s' is invalid", levelStr, PARAM_LEVEL)); } } }
@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(OK, PROTOBUF_MIME_TYPE, new ByteArrayInputStream(bytes), bytes.length); } }
public static NanoHTTPD.IHTTPSession createHttpSession(NanoHTTPD.Method method, @Nullable Map<String, String> params) { NanoHTTPD.IHTTPSession res = mock(NanoHTTPD.IHTTPSession.class); when(res.getMethod()).thenReturn(method); if (params != null) { when(res.getParms()).thenReturn(params); } return res; } }
@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 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 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.")); }
@Override public Response serve(IHTTPSession session) { return actionRegistry.getAction(session) .map(action -> serveFromAction(session, action)) .orElseGet(() -> newFixedLengthResponse(NOT_FOUND, MIME_PLAINTEXT, format("Error 404, '%s' not found.", session.getUri()))); }
@Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, "ok"); } }
@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 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 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 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 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 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); }