@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!")); }
order.verify(v).draw(c); byte[] data = IOUtils.toByteArray(response.getData()); assertNotNull(data); assertTrue(data.length > 0);
@Test public void testNonEmptyResultForNullView() throws IOException { WindowManager wm = mock(WindowManager.class); View inflate = View.inflate(RuntimeEnvironment.application, android.R.layout.two_line_list_item, null); doReturn(inflate).when(wm).getCurrentRootView(); ViewHierarchyPlugin viewHierarchyPlugin = new ViewHierarchyPlugin(wm); NanoHTTPD.Response response = viewHierarchyPlugin.handleRequest(null, null, mock(NanoHTTPD.IHTTPSession.class), null, null); assertEquals(HttpTools.MimeType.APP_JSON, response.getMimeType()); String data = IOUtils.toString(response.getData()); ViewNode vn = new Gson().fromJson(data, ViewNode.class); assertNotNull(vn); assertTrue(vn.getData().size() > 0); assertEquals(2, vn.getChildCount()); assertEquals(0, vn.getLevel()); assertEquals(0, vn.getPosition()); assertEquals(android.R.id.text1, vn.getChildAt(0).getId()); assertEquals(android.R.id.text2, vn.getChildAt(1).getId()); //very simple check for (int i = 0, n = vn.getChildCount(); i < n; i++) { ViewNode vnc = vn.getChildAt(i); assertTrue(vnc.getData().size() > 0); assertEquals(0, vnc.getChildCount()); assertEquals(1, vnc.getLevel()); assertEquals(i + 1, vnc.getPosition()); } } }
@Test public void testViewWithTopLeftPosition() throws IOException { View v = createView(0, 0, 100, 100); Bitmap bitmap = spy(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); doReturn(bitmap).when(v).getDrawingCache(); WindowManager wm = mock(WindowManager.class); doReturn(v).when(wm).getCurrentRootView(); ScreenViewPlugin svp = new ScreenViewPlugin(wm); NanoHTTPD.Response response = svp.handleRequest(null, null, mock(NanoHTTPD.IHTTPSession.class), null, null); verify(v).destroyDrawingCache(); verify(v).buildDrawingCache(anyBoolean()); verify(bitmap).recycle(); byte[] data = IOUtils.toByteArray(response.getData()); assertNotNull(data); assertTrue(data.length > 0); Bitmap resultBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); assertNotNull(resultBitmap); }
@Test public void testServeJSONWithNoActivity() throws IOException { WindowManager wm = mock(WindowManager.class); doReturn(null).when(wm).getCurrentActivity(); ActivityPlugin ap = spy(new TestActivityPlugin(wm)); NanoHTTPD.Response response = ap.serveFile("someuri", null, mock(NanoHTTPD.IHTTPSession.class), mock(File.class), HttpTools.MimeType.APP_JSON); String data = IOUtils.toString(response.getData()); assertEquals("{}", data); assertEquals(HttpTools.MimeType.APP_JSON, response.getMimeType()); }
@Test public void testEmptyResultForNullView() throws IOException { WindowManager wm = mock(WindowManager.class); doReturn(null).when(wm).getCurrentRootView(); ViewHierarchyPlugin viewHierarchyPlugin = new ViewHierarchyPlugin(wm); NanoHTTPD.Response response = viewHierarchyPlugin.handleRequest(null, null, mock(NanoHTTPD.IHTTPSession.class), null, null); assertEquals(HttpTools.MimeType.APP_JSON, response.getMimeType()); String data = IOUtils.toString(response.getData()); assertEquals("{}", data); }
private void serveContent(IRascalResult result, Map<String, InputStream> output, Map<String, String> metadata) throws IOException { IConstructor provider = (IConstructor) result.getValue(); String id = ((IString) provider.get("id")).getValue(); Function<IValue, IValue> target = liftProviderFunction(provider.get("callback")); // this installs the provider such that subsequent requests are handled. contentServer.registerContentProvider(id, target); // now we need some HTML to show Response response = contentServer.serve("/" + id, Method.GET, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); metadata.put("url", "http://localhost:" + contentServer.getListeningPort() + "/" + id); output.put(response.getMimeType(), response.getData()); }
@Test public void testServeTextWithNoActivity() throws IOException { WindowManager wm = mock(WindowManager.class); doReturn(null).when(wm).getCurrentActivity(); ActivityPlugin ap = spy(new TestActivityPlugin(wm)); NanoHTTPD.Response response = ap.serveFile("someuri", null, mock(NanoHTTPD.IHTTPSession.class), mock(File.class), HttpTools.MimeType.TEXT_PLAIN); String data = IOUtils.toString(response.getData()); assertEquals(ActivityPlugin.APPLICATION_IS_NOT_ACTIVE, data); assertEquals(HttpTools.MimeType.TEXT_PLAIN, response.getMimeType()); }
@Test public void testNullView() throws IOException { WindowManager wm = mock(WindowManager.class); doReturn(null).when(wm).getCurrentRootView(); ScreenViewPlugin svp = new ScreenViewPlugin(wm); NanoHTTPD.IHTTPSession session = mock(NanoHTTPD.IHTTPSession.class); NanoHTTPD.Response response = svp.handleRequest(null, null, session, null, null); byte[] data = IOUtils.toByteArray(response.getData()); }
@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 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.")); }