com.gargoylesoftware.htmlunit.WebResponseData Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.WebResponseData. 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: WebConnectionWrapperTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void wrapper() throws Exception {
    final List<NameValuePair> emptyList = Collections.emptyList();
    final WebResponseData data = new WebResponseData(new byte[]{}, HttpStatus.SC_OK, "", emptyList);
    final WebResponse response = new WebResponse(data, URL_FIRST, HttpMethod.GET, 0);
    final WebRequest wrs = new WebRequest(URL_FIRST);

    final WebConnection realConnection = new WebConnection() {
        @Override
        public WebResponse getResponse(final WebRequest request) {
            assertSame(wrs, request);
            return response;
        }
        @Override
        public void close() {
            // nothing
        }
    };

    try (WebConnectionWrapper wrapper = new WebConnectionWrapper(realConnection)) {
        assertSame(response, wrapper.getResponse(wrs));
    }
}
 
Example #2
Source File: MockWebResponseBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private WebResponseData webResponseData() throws IOException {
	List<NameValuePair> responseHeaders = responseHeaders();
	int statusCode = (this.response.getRedirectedUrl() != null ?
			HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus());
	String statusMessage = statusMessage(statusCode);
	return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders);
}
 
Example #3
Source File: DelegatingWebConnectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	request = new WebRequest(new URL("http://localhost/"));
	WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.emptyList());
	expectedResponse = new WebResponse(data, request, 100L);
	webConnection = new DelegatingWebConnection(defaultConnection,
			new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2));
}
 
Example #4
Source File: MockWebResponseBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebResponseData webResponseData() throws IOException {
	List<NameValuePair> responseHeaders = responseHeaders();
	int statusCode = (this.response.getRedirectedUrl() != null ?
			HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus());
	String statusMessage = statusMessage(statusCode);
	return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders);
}
 
Example #5
Source File: DelegatingWebConnectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	request = new WebRequest(new URL("http://localhost/"));
	WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.emptyList());
	expectedResponse = new WebResponse(data, request, 100L);
	webConnection = new DelegatingWebConnection(defaultConnection,
			new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2));
}
 
Example #6
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 #7
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 #8
Source File: MockWebResponseBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private WebResponseData webResponseData() throws IOException {
	List<NameValuePair> responseHeaders = responseHeaders();
	int statusCode = (this.response.getRedirectedUrl() != null ?
			HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus());
	String statusMessage = statusMessage(statusCode);
	return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders);
}
 
Example #9
Source File: DelegatingWebConnectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	request = new WebRequest(new URL("http://localhost/"));
	WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.<NameValuePair> emptyList());
	expectedResponse = new WebResponse(data, request, 100L);
	webConnection = new DelegatingWebConnection(defaultConnection,
		new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2));
}
 
Example #10
Source File: MockWebResponseBuilder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public WebResponse build() throws IOException {
	WebResponseData webResponseData = webResponseData();
	long endTime = System.currentTimeMillis();
	return new WebResponse(webResponseData, this.webRequest, endTime - this.startTime);
}
 
Example #11
Source File: MockWebResponseBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
public WebResponse build() throws IOException {
	WebResponseData webResponseData = webResponseData();
	long endTime = System.currentTimeMillis();
	return new WebResponse(webResponseData, this.webRequest, endTime - this.startTime);
}
 
Example #12
Source File: MockWebResponseBuilder.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public WebResponse build() throws IOException {
	WebResponseData webResponseData = webResponseData();
	long endTime = System.currentTimeMillis();
	return new WebResponse(webResponseData, webRequest, endTime - startTime);
}
 
Example #13
Source File: FalsifyingWebConnection.java    From htmlunit with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a faked WebResponse for the request with the provided content.
 * @param wr the web request for which a response should be created
 * @param content the content to place in the response
 * @param contentType the content type of the response
 * @param responseCode the HTTP code for the response
 * @param responseMessage the HTTP message for the response
 * @return a web response with the provided content
 * @throws IOException if an encoding problem occurred
 */
protected WebResponse createWebResponse(final WebRequest wr, final String content,
        final String contentType, final int responseCode, final String responseMessage) throws IOException {
    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair(HttpHeader.CONTENT_TYPE_LC, contentType + "; charset=" + UTF_8));
    final byte[] body = content.getBytes(UTF_8);
    final WebResponseData wrd = new WebResponseData(body, responseCode, responseMessage, headers);
    return new WebResponse(wrd, wr.getUrl(), wr.getHttpMethod(), 0);
}
 
Example #14
Source File: FalsifyingWebConnection.java    From HtmlUnit-Android with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a faked WebResponse for the request with the provided content.
 * @param wr the web request for which a response should be created
 * @param content the content to place in the response
 * @param contentType the content type of the response
 * @param responseCode the HTTP code for the response
 * @param responseMessage the HTTP message for the response
 * @return a web response with the provided content
 * @throws IOException if an encoding problem occurred
 */
protected WebResponse createWebResponse(final WebRequest wr, final String content,
        final String contentType, final int responseCode, final String responseMessage) throws IOException {
    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair(HttpHeader.CONTENT_TYPE_LC, contentType + "; charset=" + UTF_8));
    final byte[] body = content.getBytes(UTF_8);
    final WebResponseData wrd = new WebResponseData(body, responseCode, responseMessage, headers);
    return new WebResponse(wrd, wr.getUrl(), wr.getHttpMethod(), 0);
}