@Test public void testRoom() throws Exception { // create room WebRequest request = new PostMethodWebRequest( getCreateUrl(), new ByteArrayInputStream(objectMapper.writeValueAsString(baseRoom).getBytes()), MediaType.APPLICATION_JSON_VALUE); WebResponse response = conversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); assertEquals(MediaType.APPLICATION_JSON_VALUE, response.getContentType()); Room room = objectMapper.readValue(response.getText(), Room.class); assertNotNull(room); assertTrue(room.getId() != 0); assertTrue(compare(baseRoom, room, "id")); // update room baseRoom = room; baseRoom.setName("New name"); request = new PutMethodWebRequest( getUpdateUrl(), new ByteArrayInputStream(objectMapper.writeValueAsString(baseRoom).getBytes()), MediaType.APPLICATION_JSON_VALUE); response = conversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); assertEquals(MediaType.APPLICATION_JSON_VALUE, response.getContentType()); room = objectMapper.readValue(response.getText(), Room.class); assertNotNull(room); assertEquals(room, baseRoom); // get room request = new GetMethodWebRequest(getViewUrl(baseRoom.getId())); response = conversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); assertEquals(MediaType.APPLICATION_JSON_VALUE, response.getContentType()); room = objectMapper.readValue(response.getText(), Room.class); assertNotNull(room); assertEquals(room, baseRoom); // remove room request = new WebRequest(getDeleteUrl(baseRoom.getId())) { @Override public String getMethod() { return RequestMethod.DELETE.toString(); } }; response = conversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // check removed URL url = new URL(getViewUrl(baseRoom.getId())); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); assertEquals(HttpURLConnection.HTTP_NO_CONTENT, connection.getResponseCode()); }
@Test public void testCreateTopLevelFile() throws CoreException, IOException, SAXException, JSONException { String directoryPath = "sample" + System.currentTimeMillis(); createDirectory(directoryPath); String fileName = "testfile.txt"; WebRequest request = getPostFilesRequest(directoryPath, getNewFileJSON(fileName).toString(), fileName); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); assertTrue( "Create file response was OK, but the file does not exist", checkFileExists(directoryPath + "/" + fileName)); assertEquals( "Response should contain file metadata in JSON, but was " + response.getText(), "application/json", response.getContentType()); JSONObject responseObject = new JSONObject(response.getText()); assertNotNull("No file information in responce", responseObject); checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null); // should be able to perform GET on location header to obtain metadata String location = response.getHeaderField("Location"); request = getGetFilesRequest(location + "?parts=meta"); response = webConversation.getResource(request); assertNotNull(location); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); responseObject = new JSONObject(response.getText()); assertNotNull("No direcory information in responce", responseObject); checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null); }
@Test public void testCreateDirectory() throws CoreException, IOException, SAXException, JSONException { String directoryPath = "sample/directory/path" + System.currentTimeMillis(); createDirectory(directoryPath); String dirName = "testdir"; webConversation.setExceptionsThrownOnErrorStatus(false); WebRequest request = getPostFilesRequest(directoryPath, getNewDirJSON(dirName).toString(), dirName); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); assertTrue( "Create directory response was OK, but the directory does not exist", checkDirectoryExists(directoryPath + "/" + dirName)); assertEquals( "Response should contain directory metadata in JSON, but was " + response.getText(), "application/json", response.getContentType()); JSONObject responseObject = new JSONObject(response.getText()); assertNotNull("No directory information in response", responseObject); checkDirectoryMetadata(responseObject, dirName, null, null, null, null, null); // should be able to perform GET on location header to obtain metadata String location = response.getHeaderField(ProtocolConstants.HEADER_LOCATION); request = getGetFilesRequest(location); response = webConversation.getResource(request); assertNotNull(location); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); responseObject = new JSONObject(response.getText()); assertNotNull("No direcory information in responce", responseObject); checkDirectoryMetadata(responseObject, dirName, null, null, null, null, null); }
/** * @param query to search for. * @param attrs attributes to return from search * @param ldifFile to compare with * @throws Exception On test failure. */ @Parameters({"searchServletQuery", "searchServletAttrs", "searchServletLdif"}) @Test(groups = {"servlettest"}) public void dsmlSearchServlet(final String query, final String attrs, final String ldifFile) throws Exception { final String ldif = TestUtil.readFileIntoString(ldifFile); final DsmlResult entry = new DsmlResult(TestUtil.convertLdifToEntry(ldif)); final ServletUnitClient sc = this.dsmlServletRunner.newClient(); // test basic dsml query WebRequest request = new PostMethodWebRequest("http://servlets.search.ldap.middleware.vt.edu/DsmlPeopleSearch"); request.setParameter("query", query); request.setParameter("attrs", attrs.split("\\|")); WebResponse response = sc.getResponse(request); AssertJUnit.assertNotNull(response); AssertJUnit.assertEquals("text/xml", response.getContentType()); AssertJUnit.assertEquals(entry.toDsmlv1(), response.getText()); // test plain text request = new PostMethodWebRequest("http://servlets.search.ldap.middleware.vt.edu/DsmlPeopleSearch"); request.setParameter("content-type", "text"); request.setParameter("query", query); request.setParameter("attrs", attrs.split("\\|")); response = sc.getResponse(request); AssertJUnit.assertNotNull(response); AssertJUnit.assertEquals("text/plain", response.getContentType()); AssertJUnit.assertEquals(entry.toDsmlv1(), response.getText()); // test dsmlv2 request = new PostMethodWebRequest("http://servlets.search.ldap.middleware.vt.edu/DsmlPeopleSearch"); request.setParameter("dsml-version", "2"); request.setParameter("query", query); request.setParameter("attrs", attrs.split("\\|")); response = sc.getResponse(request); AssertJUnit.assertNotNull(response); AssertJUnit.assertEquals("text/xml", response.getContentType()); AssertJUnit.assertEquals(entry.toDsmlv2(), response.getText()); }
/** Verifies that the HTTP header of a diagram incites the browser to cache it. */ public void testInvalidUrl() throws Exception { WebConversation conversation = new WebConversation(); // Try to proxify an invalid address WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/invalidURL"); WebResponse response = conversation.getResource(request); // Analyze response, it must be the empty form // Verifies the Content-Type header assertEquals("Response content type is not HTML", "text/html", response.getContentType()); WebForm[] forms = response.getForms(); assertEquals(2, forms.length); }
public void testNoInitialState() throws Exception { final String resourceName = "something/interesting"; ServletRunner sr = new ServletRunner(); sr.registerServlet(resourceName, StatefulServlet.class.getName()); WebRequest request = new GetMethodWebRequest("http://localhost/" + resourceName); WebResponse response = sr.getResponse(request); assertNotNull("No response received", response); assertEquals("content type", "text/plain", response.getContentType()); assertEquals("requested resource", "No session found", response.getText()); assertEquals("Returned cookie count", 0, response.getNewCookieNames().length); }
public void testStatePreservation() throws Exception { final String resourceName1 = "something/interesting/start"; final String resourceName2 = "something/continue"; ServletRunner sr = new ServletRunner(); sr.registerServlet(resourceName1, StatefulServlet.class.getName()); sr.registerServlet(resourceName2, StatefulServlet.class.getName()); WebClient wc = sr.newClient(); WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName1); request.setParameter("color", "red"); WebResponse response = wc.getResponse(request); assertNotNull("No response received", response); assertEquals("content type", "text/plain", response.getContentType()); assertEquals("requested resource", "You selected red", response.getText()); request = new GetMethodWebRequest("http://localhost/" + resourceName2); response = wc.getResponse(request); assertNotNull("No response received", response); assertEquals("content type", "text/plain", response.getContentType()); assertEquals("requested resource", "You posted red", response.getText()); assertEquals("Returned cookie count", 0, response.getNewCookieNames().length); }
/** * Test for content disposition and filename. * * @param jspName jsp name, with full path * @throws Exception any axception thrown during test. */ @Test public void doTest() throws Exception { ParamEncoder encoder = new ParamEncoder("table"); String mediaParameter = encoder.encodeParameterName(TableTagParameters.PARAMETER_EXPORTTYPE); WebRequest request = new GetMethodWebRequest(getJspUrl(getJspName())); request.setParameter(mediaParameter, Integer.toString(MediaTypeEnum.CSV.getCode())); WebResponse response = runner.getResponse(request); // we are really testing an xml output? Assert.assertEquals( "Expected a different content type.", "text/csv", response.getContentType()); Assert.assertEquals("Wrong content.", "1\n2\n3\n", response.getText()); }
@Test public void testTransferException() throws Exception { WebRequest req = new PostMethodWebRequest( CONTEXT_URL + "/services/hello", new ByteArrayInputStream("".getBytes()), "text/plain"); ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(false); WebResponse response = client.getResponse(req); assertEquals(500, response.getResponseCode()); assertEquals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, response.getContentType()); Object object = HttpHelper.deserializeJavaObjectFromStream(response.getInputStream()); assertNotNull(object); IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, object); assertEquals("Damn", cause.getMessage()); }
public void testText() throws Exception { MockStatusAccessor statusAccessor = MockStatusAccessor.generateStatusAccessor(colArray1, rowArray1); statusAccessor.setTitle("testtbl", null); statSvc.registerStatusAccessor("testtbl", statusAccessor); WebResponse resp = getTable("testtbl", true); assertResponseOk(resp); assertEquals("Content type", "text/plain", resp.getContentType()); log.debug(resp.getText()); List lines = getLines(resp); assertEquals(rowArray1.length + 3, lines.size()); Map row0 = getRow((String) lines.get(0)); assertEquals("2.4.6.8", row0.get("host")); Map row2 = getRow((String) lines.get(2)); assertEquals("testtbl", row2.get("table")); assertEqualTables(table1, lines); }
public void testInvocationContextUpdate() throws Exception { final String resourceName = "something/interesting"; ServletRunner sr = new ServletRunner(); sr.registerServlet(resourceName, StatefulServlet.class.getName()); ServletUnitClient suc = sr.newClient(); WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName); request.setParameter("color", "red"); InvocationContext ic = suc.newInvocation(request); StatefulServlet ss = (StatefulServlet) ic.getServlet(); ss.setColor(ic.getRequest(), "blue"); suc.getResponse(ic); WebResponse response = suc.getResponse("http://localhost/" + resourceName); assertNotNull("No response received", response); assertEquals("content type", "text/plain", response.getContentType()); assertEquals("requested resource", "You posted blue", response.getText()); assertEquals("Returned cookie count", 0, response.getNewCookieNames().length); }
/** * @param query to search for. * @param attrs attributes to return from search * @param ldifFile to compare with * @throws Exception On test failure. */ @Parameters({"searchServletQuery", "searchServletAttrs", "searchServletLdif"}) @Test(groups = {"servlettest"}) public void searchServlet(final String query, final String attrs, final String ldifFile) throws Exception { final String ldif = TestUtil.readFileIntoString(ldifFile); final LdapEntry entry = TestUtil.convertLdifToEntry(ldif); final ServletUnitClient sc = this.ldifServletRunner.newClient(); final WebRequest request = new PostMethodWebRequest("http://servlets.search.ldap.middleware.vt.edu/LdifPeopleSearch"); request.setParameter("query", query); request.setParameter("attrs", attrs.split("\\|")); final WebResponse response = sc.getResponse(request); AssertJUnit.assertNotNull(response); AssertJUnit.assertEquals("text/plain", response.getContentType()); final LdapEntry result = TestUtil.convertLdifToEntry(response.getText()); AssertJUnit.assertEquals(entry, result); }