@Test public void run_on_valid_response() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL); RequestContext context = RequestContext.getCurrentContext(); context.setRequest(request); MockHttpServletResponse response = new MockHttpServletResponse(); context.setResponse(response); InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}"); context.setResponseDataStream(in); filter.run(); assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody()); }
@Test public void testGetAppList() throws UnsupportedEncodingException { // 测试准备。 MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); CredentialResolver credentialResolver = Mockito.mock(CredentialResolver.class); logoutAction.setCredentialResolver(credentialResolver); Ki4soService ki4soService = Mockito.mock(Ki4soService.class); logoutAction.setKi4soService(ki4soService); Credential credential = Mockito.mock(Credential.class); // 设置预期结果。 Mockito.when(credentialResolver.resolveCredential(request)).thenReturn(credential); List<App> list = new ArrayList<App>(); App app = new App(); app.setAppId("1000"); app.setAppName("测试应用程序1"); app.setHost("app.com"); app.setLogoutUrl("http://app.com/logout.do"); list.add(app); Mockito.when(ki4soService.getAppList(credential)).thenReturn(list); // 执行查询。 logoutAction.getAppList(request, response); // 检查结果。 Assert.assertEquals("application/x-javascript", response.getContentType()); Assert.assertEquals("UTF-8", response.getCharacterEncoding()); // 检查输出的jsonp串是否正确。 String content = response.getContentAsString(); content = content.trim(); Assert.assertTrue(content.startsWith("fetchAppList(")); Assert.assertTrue(content.endsWith(");")); String json = content.replaceFirst("fetchAppList\\(", ""); json = json.replaceFirst("\\);", ""); JSONArray data = (JSONArray) JSON.parse(json); Assert.assertEquals(1, data.size()); @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) data.get(0); Assert.assertEquals("1000", map.get("appId")); }
public void testGetServerStatus() throws Exception { MockHttpServletRequest request = createMockRequest("GET", "/server/status"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals(MockHttpServletResponse.SC_OK, response.getStatus()); assertEquals("text/plain", response.getContentType()); assertEquals("UTF-8", response.getCharacterEncoding()); BufferedReader content = new BufferedReader(new StringReader(response.getContentAsString())); String line = content.readLine(); boolean found = false; while (line != null) { if (line.startsWith("jvm.memory.max=")) { found = true; break; } line = content.readLine(); } assertTrue("did not find jvm.memory.max in status output", found); }