/** * Gets content from the path that's relative to the base URL on which the Arquillian test * archive is deployed. * * @param path the path relative to the URL on which the Arquillian test is deployed * @return the raw content as a string as returned by the server */ protected String getFromServerPath(final String path) { response = null; for (int i=0; i<=3; i++) { try { response = webClient.getPage(base + path).getWebResponse().getContentAsString(); if (!response.contains("The response wrapper must wrap the response obtained from getResponse()")) { return response; } } catch (FailingHttpStatusCodeException | IOException e) { throw new IllegalStateException(e); } } return response; }
private void assertMockMvcNotUsed(WebClient client, String url) throws Exception { assertThat(getResponse(client, url).getContentAsString(), not(equalTo("mvc"))); }
private void assertMockMvcUsed(WebClient client, String url) throws Exception { assertThat(getResponse(client, url).getContentAsString(), equalTo("mvc")); }
@Test // SPR-14265 public void cookiesAreManaged() throws Exception { this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build(); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("NA")); assertThat(postResponse(client, "http://localhost/?cookie=foo").getContentAsString(), equalTo("Set")); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("foo")); assertThat(deleteResponse(client, "http://localhost/").getContentAsString(), equalTo("Delete")); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("NA")); }
@Test public void buildContent() throws Exception { this.response.getWriter().write("expected content"); WebResponse webResponse = this.responseBuilder.build(); assertThat(webResponse.getContentAsString(), equalTo("expected content")); }
@Test public void defaultContextPathEmpty() throws Exception { WebConnection conn = this.builder.createConnection(this.client); assertThat(getResponse(conn, "http://localhost/abc").getContentAsString(), equalTo("")); }
@Test public void defaultContextPathCustom() throws Exception { WebConnection conn = this.builder.contextPath("/abc").createConnection(this.client); assertThat(getResponse(conn, "http://localhost/abc/def").getContentAsString(), equalTo("/abc")); }
@Test public void forward() throws IOException { this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient, "")); Page page = this.webClient.getPage("http://localhost/forward"); assertThat(page.getWebResponse().getContentAsString(), equalTo("hello")); }
@Test // SPR-14066 public void cookieManagerShared() throws Exception { this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build(); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("NA")); client.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared")); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("cookieManagerShared")); }
@Test public void verifyExampleInClassLevelJavadoc() throws Exception { Assume.group(TestGroup.PERFORMANCE); WebClient webClient = new WebClient(); MockMvc mockMvc = MockMvcBuilders.standaloneSetup().build(); MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient); WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*"); WebConnection httpConnection = new HttpWebConnection(webClient); webClient.setWebConnection( new DelegatingWebConnection(mockConnection, new DelegateWebConnection(cdnMatcher, httpConnection))); Page page = webClient.getPage("http://code.jquery.com/jquery-1.11.0.min.js"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); assertThat(page.getWebResponse().getContentAsString(), not(isEmptyString())); }
WebClient client = new WebClient(); Page page = client.getPage("http://stackoverflow.com/users/flair/97901.json"); WebResponse response = page.getWebResponse(); if (response.getContentType().equals("application/json")) { String json = response.getContentAsString(); Map<String, String> map = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {}.getType()); System.out.println(map.get("displayName")); // Benju }
/** * Returns the response content as a string, using the specified charset, * rather than the charset/encoding specified in the server response. * If there is a bom header the charset parameter will be overwritten by the bom. * @param encoding the charset/encoding to use to convert the response content into a string * @return the response content as a string or null if the content retrieval was failing */ public String getContentAsString(final Charset encoding) { return getContentAsString(encoding, false); }
/** * {@inheritDoc} * The default behavior of this method is to return getContentAsString(Charset) on the wrapped webResponse object. */ @Override public String getContentAsString(final Charset encoding) { return wrappedWebResponse_.getContentAsString(encoding); }
/** * {@inheritDoc} * The default behavior of this method is to return getContentAsString(Charset, boolean) * on the wrapped webResponse object. */ @Override public String getContentAsString(final Charset encoding, final boolean ignoreUtf8Bom) { return wrappedWebResponse_.getContentAsString(encoding, ignoreUtf8Bom); }
/** * {@inheritDoc} * The default behavior of this method is to return getContentAsString() on the wrapped webResponse object. */ @Override public String getContentAsString() { return wrappedWebResponse_.getContentAsString(getContentCharset()); }
/** * Returns the content of this page. * * @return the content of this page */ public String getContent() { return getWebResponse().getContentAsString(); } }
/** * Returns the response content as a string, using the charset/encoding specified in the server response. * @return the response content as a string, using the charset/encoding specified in the server response * or null if the content retrieval was failing */ public String getContentAsString() { return getContentAsString(getContentCharset()); }
@Test public void testOnCompleteCalledSuccesfully() throws Exception { WebClient webClient = new WebClient(); webClient.getPage(getPath(AsyncServlet.TEST_COMPLETE)); Page results = webClient.getPage(contextPath + "Status"); assertTrue(results.getWebResponse().getContentAsString().contains("onComplete: true")); }
@Test public void testCrossContextForward(@ArquillianResource @OperateOnDeployment(FIRST) URL firstContext) throws IOException { Page page = new WebClient().getPage(firstContext + "forwarding"); assertEquals(200, page.getWebResponse().getStatusCode()); assertEquals("<h1>Hello World</h1>", page.getWebResponse().getContentAsString()); } }
private JSONObject callStepsToJson(String jenkinsFileContent) throws IOException { JenkinsRule.WebClient wc = j.createWebClient(); WebRequest req = new WebRequest(new URL(wc.getContextPath() + ModelConverterAction.PIPELINE_CONVERTER_URL + "/stepsToJson"), HttpMethod.POST); assertNotNull(jenkinsFileContent); NameValuePair pair = new NameValuePair("jenkinsfile", jenkinsFileContent); req.setRequestParameters(Collections.singletonList(pair)); String rawResult = wc.getPage(req).getWebResponse().getContentAsString(); assertNotNull(rawResult); return JSONObject.fromObject(rawResult); } }