@Test public void testGetStaticResource() throws Exception { String address = "http://localhost:" + PORT + "/singleton/staticmodel.xml"; WebClient wc = WebClient.create(address); String response = wc.get(String.class); assertTrue(response.startsWith("<model")); assertEquals("application/xml+model", wc.getResponse().getMetadata().getFirst("Content-Type")); } @Test
@Test public void testGetBookResponseAndETag() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books/response/123"; WebClient wc = WebClient.create(endpointAddress); Book book = wc.get(Book.class); assertEquals(200, wc.getResponse().getStatus()); assertEquals(123L, book.getId()); MultivaluedMap<String, Object> headers = wc.getResponse().getMetadata(); assertTrue(!headers.isEmpty()); Object etag = headers.getFirst("ETag"); assertNotNull(etag); assertTrue(etag.toString().startsWith("\"")); assertTrue(etag.toString().endsWith("\"")); }
private void doTestGetGenericBook(String address, long bookId, boolean checkAnnotations) throws Exception { WebClient wc = WebClient.create(address); wc.accept("application/xml"); Book book = wc.get(Book.class); assertEquals(bookId, book.getId()); MediaType mt = wc.getResponse().getMediaType(); assertEquals("application/xml;charset=ISO-8859-1", mt.toString()); if (checkAnnotations) { assertEquals("OK", wc.getResponse().getHeaderString("Annotations")); } else { assertNull(wc.getResponse().getHeaderString("Annotations")); } }
private void doTestGetBookHTML(String endpointAddress) throws Exception { WebClient client = WebClient.create(endpointAddress); client.accept("text/html"); WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(100000000); XMLSource source = client.accept("text/html").get(XMLSource.class); Map<String, String> namespaces = new HashMap<>(); namespaces.put("xhtml", "http://www.w3.org/1999/xhtml"); namespaces.put("books", "http://www.w3.org/books"); String value = source.getValue("xhtml:html/xhtml:body/xhtml:ul/books:bookTag", namespaces); assertEquals("CXF Rocks", value); String ct = client.getResponse().getMetadata().getFirst("Content-Type").toString(); assertEquals("text/html", ct); }
@Test public void testNoBookWebClient() throws Exception { String baseAddress = "http://localhost:" + PORT + "/test/services/rest"; WebClient client = WebClient.create(baseAddress); client.path("/bookstore/books/0/subresource").accept(MediaType.APPLICATION_XML_TYPE); Book b = client.get(Book.class); assertNull(b); assertEquals(204, client.getResponse().getStatus()); }
@Test public void testPostCollectionGetBooksWebClient() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3"; WebClient wc = WebClient.create(endpointAddress); wc.accept("application/xml").type("application/xml"); Book b1 = new Book("CXF in Action", 123L); Book b2 = new Book("CXF Rocks", 124L); List<Book> books = new ArrayList<>(); books.add(b1); books.add(b2); Book book = wc.postCollection(books, Book.class, Book.class); assertEquals(200, wc.getResponse().getStatus()); assertNotSame(b1, book); assertEquals(b1.getName(), book.getName()); }
@Test public void testGetBookFromWebClientWithTextJMSMessage() throws Exception { // setup the the client String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?replyToName=dynamicQueues/test.jmstransport.response" + "&jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&jndiURL=tcp://localhost:" + JMS_PORT + "&messageType=text"; WebClient client = WebClient.create(endpointAddressUrlEncoded); WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor()); WebClient.getConfig(client).getRequestContext() .put(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books/123"); Book book = client.get(Book.class); assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus()); assertEquals("Get a wrong book id.", 123, book.getId()); }
@Test public void testGetBookFromWebClient() throws Exception { // setup the the client String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?replyToName=dynamicQueues/test.jmstransport.response" + "&jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&jndiURL=tcp://localhost:" + JMS_PORT; WebClient client = WebClient.create(endpointAddressUrlEncoded); WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor()); WebClient.getConfig(client).getRequestContext() .put(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books/123"); Book book = client.get(Book.class); assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus()); assertEquals("Get a wrong book id.", 123, book.getId()); }
@Test public void testAddBookCustomFailureStatus() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books/customstatus"; WebClient client = WebClient.create(endpointAddress); Book book = client.type("text/xml").accept("application/xml").post(new Book(), Book.class); assertEquals(888L, book.getId()); Response r = client.getResponse(); assertEquals("CustomValue", r.getMetadata().getFirst("CustomHeader")); assertEquals(233, r.getStatus()); assertEquals("application/xml", r.getMetadata().getFirst("Content-Type")); }
@Test public void testPostCollectionGenericEntityWebClient() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3"; WebClient wc = WebClient.create(endpointAddress); wc.accept("application/xml").type("application/xml"); Book b1 = new Book("CXF in Action", 123L); Book b2 = new Book("CXF Rocks", 124L); List<Book> books = new ArrayList<>(); books.add(b1); books.add(b2); GenericEntity<List<Book>> genericCollectionEntity = new GenericEntity<List<Book>>(books) { }; Book book = wc.post(genericCollectionEntity, Book.class); assertEquals(200, wc.getResponse().getStatus()); assertNotSame(b1, book); assertEquals(b1.getName(), book.getName()); }
@Test public void testGetBookWithRequestScope() { // the BookStore method which will handle this request depends on the injected HttpHeaders WebClient wc = WebClient.create("http://localhost:" + PORT + "/test/request/bookstore/booksecho2"); wc.type("text/plain").accept("text/plain"); wc.header("CustomHeader", "custom-header"); String value = wc.post("CXF", String.class); assertEquals("CXF", value); assertEquals("custom-header", wc.getResponse().getMetadata().getFirst("CustomHeader")); }
@Test public void testPostCollectionGenericEntityAsEntity() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3"; WebClient wc = WebClient.create(endpointAddress); wc.accept("application/xml"); GenericEntity<List<Book>> collectionEntity = createGenericEntity(); final Holder<Book> holder = new Holder<>(); InvocationCallback<Book> callback = createCallback(holder); Future<Book> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"), callback); Book book = future.get(); assertEquals(200, wc.getResponse().getStatus()); assertSame(book, holder.value); assertNotSame(collectionEntity.getEntity().get(0), book); assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName()); }
@Test public void testGetBookFromWebClientWithPath() throws Exception { // setup the the client String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&replyToName=dynamicQueues/test.jmstransport.response" + "&jndiURL=tcp://localhost:" + JMS_PORT + "&jndiConnectionFactoryName=ConnectionFactory"; WebClient client = WebClient.create(endpointAddressUrlEncoded); client.path("bookstore").path("books").path("123"); Book book = client.get(Book.class); assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus()); assertEquals("Get a wrong book id.", 123, book.getId()); }
@Test public void testPostCollectionGenericEntityGenericCallback() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3"; WebClient wc = WebClient.create(endpointAddress); wc.accept("application/xml").type("application/xml"); GenericEntity<List<Book>> collectionEntity = createGenericEntity(); final Holder<Book> holder = new Holder<>(); InvocationCallback<Book> callback = new GenericInvocationCallback<Book>(holder) { }; Future<Book> future = wc.post(collectionEntity, callback); Book book = future.get(); assertEquals(200, wc.getResponse().getStatus()); assertSame(book, holder.value); assertNotSame(collectionEntity.getEntity().get(0), book); assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName()); }
@Test public void testGetBookFromWebClientWithPathWithTextJMSMessage() throws Exception { // setup the the client String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&replyToName=dynamicQueues/test.jmstransport.response" + "&jndiURL=tcp://localhost:" + JMS_PORT + "&jndiConnectionFactoryName=ConnectionFactory" + "&messageType=text"; WebClient client = WebClient.create(endpointAddressUrlEncoded); client.path("bookstore").path("books").path("123"); Book book = client.get(Book.class); assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus()); assertEquals("Get a wrong book id.", 123, book.getId()); }
@Test public void testPostCollectionGenericEntity() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/collections3"; WebClient wc = WebClient.create(endpointAddress); wc.accept("application/xml").type("application/xml"); GenericEntity<List<Book>> collectionEntity = createGenericEntity(); final Holder<Book> holder = new Holder<>(); InvocationCallback<Book> callback = createCallback(holder); Future<Book> future = wc.post(collectionEntity, callback); Book book = future.get(); assertEquals(200, wc.getResponse().getStatus()); assertSame(book, holder.value); assertNotSame(collectionEntity.getEntity().get(0), book); assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName()); } @Test
@Test public void testGetBookLowCaseHeader() throws Exception { WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/booksecho3"); wc.type("text/plain").accept("text/plain").header("CustomHeader", "custom"); String name = wc.post("book", String.class); assertEquals("book", name); assertEquals("custom", wc.getResponse().getHeaderString("CustomHeader")); } @Test
private void validatePostResponse(WebClient wc, boolean async, boolean bodyEmpty) { validateResponse(wc); Response response = wc.getResponse(); assertEquals(!async ? "serverRead" : "serverReadAsync", response.getHeaderString("ServerReaderInterceptor")); if (!bodyEmpty) { assertEquals("clientWrite", response.getHeaderString("ClientWriterInterceptor")); } else { assertEquals("true", response.getHeaderString("EmptyRequestStreamDetected")); } assertEquals("clientRead", response.getHeaderString("ClientReaderInterceptor")); }
@Test public void testPostBookNewMediaType() { String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple"; WebClient wc = createWebClientPost(address); wc.header("newmediatype", "application/v1+xml"); Book book = wc.post(new Book("Book", 126L), Book.class); assertEquals(124L, book.getId()); validatePostResponse(wc, false, false); assertEquals("application/v1+xml", wc.getResponse().getHeaderString("newmediatypeused")); }
private void validateResponse(WebClient wc) { Response response = wc.getResponse(); assertEquals("OK", response.getHeaderString("Response")); assertEquals("OK2", response.getHeaderString("Response2")); assertEquals("Dynamic", response.getHeaderString("DynamicResponse")); assertEquals("Dynamic2", response.getHeaderString("DynamicResponse2")); assertEquals("custom", response.getHeaderString("Custom")); assertEquals("simple", response.getHeaderString("Simple")); assertEquals("serverWrite", response.getHeaderString("ServerWriterInterceptor")); assertEquals("application/xml;charset=us-ascii", response.getMediaType().toString()); assertEquals("http://localhost/redirect", response.getHeaderString(HttpHeaders.LOCATION)); }