Java Code Examples for com.gargoylesoftware.htmlunit.WebResponse#getResponseHeaders()

The following examples show how to use com.gargoylesoftware.htmlunit.WebResponse#getResponseHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockWebResponseBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void buildResponseHeaders() throws Exception {
	this.response.addHeader("Content-Type", "text/html");
	this.response.addHeader("X-Test", "value");
	Cookie cookie = new Cookie("cookieA", "valueA");
	cookie.setDomain("domain");
	cookie.setPath("/path");
	cookie.setMaxAge(1800);
	cookie.setSecure(true);
	cookie.setHttpOnly(true);
	this.response.addCookie(cookie);
	WebResponse webResponse = this.responseBuilder.build();

	List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
	assertThat(responseHeaders.size(), equalTo(3));
	NameValuePair header = responseHeaders.get(0);
	assertThat(header.getName(), equalTo("Content-Type"));
	assertThat(header.getValue(), equalTo("text/html"));
	header = responseHeaders.get(1);
	assertThat(header.getName(), equalTo("X-Test"));
	assertThat(header.getValue(), equalTo("value"));
	header = responseHeaders.get(2);
	assertThat(header.getName(), equalTo("Set-Cookie"));
	assertThat(header.getValue(), startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires="));
	assertThat(header.getValue(), endsWith("; Secure; HttpOnly"));
}
 
Example 2
Source File: MockWebResponseBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void buildResponseHeaders() throws Exception {
	this.response.addHeader("Content-Type", "text/html");
	this.response.addHeader("X-Test", "value");
	Cookie cookie = new Cookie("cookieA", "valueA");
	cookie.setDomain("domain");
	cookie.setPath("/path");
	cookie.setMaxAge(1800);
	cookie.setSecure(true);
	cookie.setHttpOnly(true);
	this.response.addCookie(cookie);
	WebResponse webResponse = this.responseBuilder.build();

	List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
	assertThat(responseHeaders.size(), equalTo(3));
	NameValuePair header = responseHeaders.get(0);
	assertThat(header.getName(), equalTo("Content-Type"));
	assertThat(header.getValue(), equalTo("text/html"));
	header = responseHeaders.get(1);
	assertThat(header.getName(), equalTo("X-Test"));
	assertThat(header.getValue(), equalTo("value"));
	header = responseHeaders.get(2);
	assertThat(header.getName(), equalTo("Set-Cookie"));
	assertThat(header.getValue(), startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires="));
	assertThat(header.getValue(), endsWith("; Secure; HttpOnly"));
}
 
Example 3
Source File: MockWebResponseBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildResponseHeadersNullDomainDefaulted() throws Exception {
	Cookie cookie = new Cookie("cookieA", "valueA");
	this.response.addCookie(cookie);
	WebResponse webResponse = this.responseBuilder.build();

	List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
	assertThat(responseHeaders.size(), equalTo(1));
	NameValuePair header = responseHeaders.get(0);
	assertThat(header.getName(), equalTo("Set-Cookie"));
	assertThat(header.getValue(), equalTo("cookieA=valueA"));
}
 
Example 4
Source File: MockWebResponseBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void buildResponseHeadersNullDomainDefaulted() throws Exception {
	Cookie cookie = new Cookie("cookieA", "valueA");
	this.response.addCookie(cookie);
	WebResponse webResponse = this.responseBuilder.build();

	List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
	assertThat(responseHeaders.size(), equalTo(1));
	NameValuePair header = responseHeaders.get(0);
	assertThat(header.getName(), equalTo("Set-Cookie"));
	assertThat(header.getValue(), equalTo("cookieA=valueA"));
}
 
Example 5
Source File: FalsifyingWebConnection.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a WebResponse with new content, preserving all other information.
 * @param wr the web response to adapt
 * @param newContent the new content to place in the response
 * @return a web response with the new content
 * @throws IOException if an encoding problem occurred
 */
protected WebResponse replaceContent(final WebResponse wr, final String newContent) throws IOException {
    final byte[] body = newContent.getBytes(wr.getContentCharset());
    final WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(),
        wr.getResponseHeaders());
    return new WebResponse(wrd, wr.getWebRequest().getUrl(), wr.getWebRequest().getHttpMethod(),
            wr.getLoadTime());
}
 
Example 6
Source File: FalsifyingWebConnection.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a WebResponse with new content, preserving all other information.
 * @param wr the web response to adapt
 * @param newContent the new content to place in the response
 * @return a web response with the new content
 * @throws IOException if an encoding problem occurred
 */
protected WebResponse replaceContent(final WebResponse wr, final String newContent) throws IOException {
    final byte[] body = newContent.getBytes(wr.getContentCharset());
    final WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(),
        wr.getResponseHeaders());
    return new WebResponse(wrd, wr.getWebRequest().getUrl(), wr.getWebRequest().getHttpMethod(),
            wr.getLoadTime());
}
 
Example 7
Source File: MockWebResponseBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void buildResponseHeaders() throws Exception {
	this.response.addHeader("Content-Type", "text/html");
	this.response.addHeader("X-Test", "value");
	WebResponse webResponse = this.responseBuilder.build();

	List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
	assertThat(responseHeaders.size(), equalTo(2));
	NameValuePair header = responseHeaders.get(0);
	assertThat(header.getName(), equalTo("Content-Type"));
	assertThat(header.getValue(), equalTo("text/html"));
	header = responseHeaders.get(1);
	assertThat(header.getName(), equalTo("X-Test"));
	assertThat(header.getValue(), equalTo("value"));
}
 
Example 8
Source File: GitClientSampleRepoRule.java    From git-client-plugin with MIT License 5 votes vote down vote up
public void notifyCommit(JenkinsRule r) throws Exception {
    synchronousPolling(r);
    WebResponse webResponse = r.createWebClient().goTo("git/notifyCommit?url=" + bareUrl(), "text/plain").getWebResponse();
    LOGGER.log(Level.FINE, webResponse.getContentAsString());
    for (NameValuePair pair : webResponse.getResponseHeaders()) {
        if (pair.getName().equals("Triggered")) {
            LOGGER.log(Level.FINE, "Triggered: " + pair.getValue());
        }
    }
    r.waitUntilNoActivity();
}