/** * Get the {@code WWW-Authenticate} header of the request that cause the exception. * * @return {@code WWW-Authenticate} header value. */ public String getWwwAuthHeader() { return super.getResponse().getHeaderString(HttpHeaders.WWW_AUTHENTICATE); }
private Response _follow3Redirects(Response currentResponse) { Response.StatusType statusType = currentResponse.getStatusInfo(); if (statusType.getFamily() != Response.Status.Family.REDIRECTION) { return currentResponse; } AtomicInteger counter = new AtomicInteger(); Response response = currentResponse; while ((statusType.getFamily() == Response.Status.Family.REDIRECTION) && (counter.incrementAndGet() <= 3)) { String location = response.getHeaderString(HttpHeaders.LOCATION); if (StringUtils.isEmpty(location)) { return response; } if (_log.isDebugEnabled()) { _log.debug( "Redirect location {}#: {}", counter.get(), location); } response.close(); WebTarget webTarget = _client.target(location); Invocation.Builder builder = webTarget.request(APPLICATION_JSON_LD); response = builder.get(); } return response; }
@Test public void testConsole() { Response response = target("/index.html").request().accept(MediaType.TEXT_HTML).get(); Assert.assertEquals("public", response.getHeaderString("Cache-Control")); Assert.assertEquals("SAMEORIGIN", response.getHeaderString("X-Frame-Options")); String html = response.readEntity(String.class); String[] substrings = { "DOCTYPE", "Oryx Serving Layer", "Alternating Least Squares", "/recommend", "<form" }; for (String substring : substrings) { Assert.assertTrue("Doesn't contain " + substring, html.contains(substring)); } }
@Test public void testConsole() { Response response = target("/index.html").request().accept(MediaType.TEXT_HTML).get(); Assert.assertEquals("public", response.getHeaderString("Cache-Control")); Assert.assertEquals("SAMEORIGIN", response.getHeaderString("X-Frame-Options")); String html = response.readEntity(String.class); String[] substrings = { "DOCTYPE", "Oryx Serving Layer", "Random Decision Forests", "/predict", "<form" }; for (String substring : substrings) { Assert.assertTrue("Doesn't contain " + substring, html.contains(substring)); } }
@Test public void testConsole() { Response response = target("/index.html").request().accept(MediaType.TEXT_HTML).get(); Assert.assertEquals("public", response.getHeaderString("Cache-Control")); Assert.assertEquals("SAMEORIGIN", response.getHeaderString("X-Frame-Options")); String html = response.readEntity(String.class); String[] substrings = { "DOCTYPE", "Oryx Serving Layer", "K-means", "/assign", "<form" }; for (String substring : substrings) { Assert.assertTrue("Doesn't contain " + substring, html.contains(substring)); } }
public BookNotFoundFault fromResponse(Response r) { String status = r.getHeaderString("Status"); if ("notFound".equals(status)) { return new BookNotFoundFault(status); } return null; }
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)); }
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 testInterceptorInvokedOnFormAndFormParamMatchesFormValue() throws Exception { Client client = ClientBuilder.newClient(); String uri = "http://localhost:" + PORT + "/form"; Form f = new Form("value", "ORIGINAL"); Response r = client.target(uri) .request(MediaType.APPLICATION_FORM_URLENCODED) .post(Entity.form(f)); Assert.assertEquals("MODIFIED", r.getHeaderString("FromForm")); Assert.assertEquals("MODIFIED", r.getHeaderString("FromFormParam")); } }
@Test public void testUseMapperOnBus() { String address = "http://localhost:" + PORT + "/bookstore/mapperonbus"; WebClient wc = WebClient.create(address); Response r = wc.post(null); assertEquals(500, r.getStatus()); MediaType mt = r.getMediaType(); assertEquals("text/plain;charset=utf-8", mt.toString().toLowerCase()); assertEquals("the-mapper", r.getHeaderString("BusMapper")); assertEquals("BusMapperException", r.readEntity(String.class)); }
@Test public void testGetBookNameAsByteArray() { String address = "http://localhost:" + PORT + "/bookstore/booknames/123"; WebClient wc = WebClient.create(address); Response r = wc.accept("application/bar").get(); String name = r.readEntity(String.class); assertEquals("CXF in Action", name); String lengthStr = r.getHeaderString(HttpHeaders.CONTENT_LENGTH); assertNotNull(lengthStr); long length = Long.valueOf(lengthStr); assertEquals(name.length(), length); }
@Test public void testBookWithExceptionsNoMapper() throws Exception { BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); try { store.getBookWithExceptions(true); fail(); } catch (WebApplicationException ex) { assertEquals("notReturned", ex.getResponse().getHeaderString("Status")); } }
@Test public void testGetBookRetryAfter() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/query/default"; WebClient wc = WebClient.create(address); Response r = wc.get(); assertEquals(503, r.getStatus()); assertEquals("2", r.getHeaderString("Retry-After")); } @Test
@Test public void testEchoBook() throws Exception { URL url = new URL("http://localhost:" + PORT + "/test/5/bookstorestorage/thosebooks"); WebClient wc = WebClient.create(url.toString(), Collections.singletonList(new CustomJaxbElementProvider())); Response r = wc.type("application/xml").post(new Book("proxy", 333L)); Book book = r.readEntity(Book.class); assertEquals(333L, book.getId()); String ct = r.getHeaderString("Content-Type"); assertEquals("application/xml;a=b", ct); }
@Test public void testStatusAngHeadersFromStream() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/statusFromStream"; WebClient wc = WebClient.create(address); wc.accept("text/xml"); Response r = wc.get(); assertEquals(503, r.getStatus()); assertEquals("text/custom+plain", r.getMediaType().toString()); assertEquals("CustomValue", r.getHeaderString("CustomHeader")); assertEquals("Response is not available", r.readEntity(String.class)); }
protected void doTestTimeoutAndCancel(String baseAddress) throws Exception { WebClient wc = WebClient.create("http://localhost:" + getPort() + baseAddress + "/books/cancel"); Response r = wc.get(); assertEquals(503, r.getStatus()); String retryAfter = r.getHeaderString(HttpHeaders.RETRY_AFTER); assertNotNull(retryAfter); assertEquals("10", retryAfter); }
@Test public void testGetCustomBookText() { String address = "http://localhost:" + PORT + "/bookstore/customtext"; WebClient wc = WebClient.create(address); Response r = wc.accept("text/custom").get(); String name = r.readEntity(String.class); assertEquals("Good book", name); assertEquals("text/custom;charset=us-ascii", r.getMediaType().toString()); assertEquals("CustomValue", r.getHeaderString("CustomHeader")); }
@Test public void testGetCustomBookResponse() { String address = "http://localhost:" + PORT + "/bookstore/customresponse"; WebClient wc = WebClient.create(address); Response r = wc.accept("application/xml").get(Response.class); Book book = r.readEntity(Book.class); assertEquals(222L, book.getId()); assertEquals("OK", r.getHeaderString("customresponse")); }
@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
@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")); }