com.gargoylesoftware.htmlunit.WebConnection Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.WebConnection. 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: DelegatingWebConnectionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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()));
}
 
Example #2
Source File: DelegatingWebConnectionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyExampleInClassLevelJavadoc() throws Exception {
	Assume.group(TestGroup.PERFORMANCE);

	WebClient webClient = new WebClient();

	MockMvc mockMvc = MockMvcBuilders.standaloneSetup(TestController.class).build();
	MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc);
	mockConnection.setWebClient(webClient);

	WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");
	WebConnection httpConnection = new HttpWebConnection(webClient);
	WebConnection webConnection = new DelegatingWebConnection(mockConnection, new DelegateWebConnection(cdnMatcher, httpConnection));

	webClient.setWebConnection(webConnection);

	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()));
}
 
Example #3
Source File: MockMvcWebConnectionBuilderSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new {@link WebConnection} that will use a {@link MockMvc}
 * instance if one of the specified {@link WebRequestMatcher} instances
 * matches.
 * @param defaultConnection the default WebConnection to use if none of
 * the specified {@code WebRequestMatcher} instances matches; never {@code null}
 * @return a new {@code WebConnection} that will use a {@code MockMvc}
 * instance if one of the specified {@code WebRequestMatcher} matches
 * @see #alwaysUseMockMvc()
 * @see #useMockMvc(WebRequestMatcher...)
 * @see #useMockMvcForHosts(String...)
 */
protected final WebConnection createConnection(WebConnection defaultConnection) {
	Assert.notNull(defaultConnection, "Default WebConnection must not be null");
	MockMvcWebConnection mockMvcWebConnection = new MockMvcWebConnection(this.mockMvc, this.contextPath);

	if (this.alwaysUseMockMvc) {
		return mockMvcWebConnection;
	}

	List<DelegatingWebConnection.DelegateWebConnection> delegates = new ArrayList<DelegatingWebConnection.DelegateWebConnection>(
		this.mockMvcRequestMatchers.size());
	for (WebRequestMatcher matcher : this.mockMvcRequestMatchers) {
		delegates.add(new DelegatingWebConnection.DelegateWebConnection(matcher, mockMvcWebConnection));
	}

	return new DelegatingWebConnection(defaultConnection, delegates);
}
 
Example #4
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 #5
Source File: DebugFrameImplTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void withCallable() throws Exception {
    final String content = "<html><head><title>debug test</title>\n"
        + "<script>\n"
        + "  var counter = 0;\n"
        + "  window.__defineGetter__('foo', function(a) { return counter++ });\n"
        + "  alert(window.foo);\n"
        + "</script></head><body></body></html>";
    final WebConnection old = client_.getWebConnection();
    try {
        final MockWebConnection mock = new MockWebConnection();
        mock.setDefaultResponse(content);
        client_.setWebConnection(mock);
        client_.getPage(URL_FIRST);
    }
    finally {
        client_.setWebConnection(old);
    }
}
 
Example #6
Source File: DebuggingWebConnection.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Wraps a web connection to have a report generated of the received responses.
 * @param webConnection the webConnection that do the real work
 * @param dirName the name of the directory to create in the tmp folder to save received responses.
 * If this folder already exists, it will be deleted first.
 * @throws IOException in case of problems writing the files
 */
public DebuggingWebConnection(final WebConnection webConnection,
        final String dirName) throws IOException {

    super(webConnection);

    wrappedWebConnection_ = webConnection;
    final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    reportFolder_ = new File(tmpDir, dirName);
    if (reportFolder_.exists()) {
        FileUtils.forceDelete(reportFolder_);
    }
    FileUtils.forceMkdir(reportFolder_);
    javaScriptFile_ = new File(reportFolder_, "hu.js");
    createOverview();
}
 
Example #7
Source File: DelegatingWebConnectionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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("https://code.jquery.com/jquery-1.11.0.min.js");
	assertThat(page.getWebResponse().getStatusCode(), equalTo(200));
	assertThat(page.getWebResponse().getContentAsString(), not(isEmptyString()));
}
 
Example #8
Source File: MockMvcWebConnectionBuilderSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private WebConnection createConnection(WebClient webClient, WebConnection defaultConnection) {
	WebConnection connection = new MockMvcWebConnection(this.mockMvc, webClient, this.contextPath);
	if (this.alwaysUseMockMvc) {
		return connection;
	}
	List<DelegateWebConnection> delegates = new ArrayList<>(this.requestMatchers.size());
	for (WebRequestMatcher matcher : this.requestMatchers) {
		delegates.add(new DelegateWebConnection(matcher, connection));
	}
	return new DelegatingWebConnection(defaultConnection, delegates);
}
 
Example #9
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void context() throws Exception {
	WebConnection conn = this.builder.createConnection(this.client);

	assertMockMvcUsed(conn, "http://localhost/");
	assertMockMvcNotUsed(conn, "http://example.com/");
}
 
Example #10
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void mockMvcExampleDotCom() throws Exception {
	WebConnection conn = this.builder.useMockMvcForHosts("example.com").createConnection(this.client);

	assertMockMvcUsed(conn, "http://localhost/");
	assertMockMvcUsed(conn, "http://example.com/");
	assertMockMvcNotUsed(conn, "http://other.com/");
}
 
Example #11
Source File: MockMvcWebConnectionBuilderSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebConnection createConnection(WebClient webClient, WebConnection defaultConnection) {
	WebConnection connection = new MockMvcWebConnection(this.mockMvc, webClient, this.contextPath);
	if (this.alwaysUseMockMvc) {
		return connection;
	}
	List<DelegateWebConnection> delegates = new ArrayList<>(this.requestMatchers.size());
	for (WebRequestMatcher matcher : this.requestMatchers) {
		delegates.add(new DelegateWebConnection(matcher, connection));
	}
	return new DelegatingWebConnection(defaultConnection, delegates);
}
 
Example #12
Source File: WebConnectionWrapper.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a WebConnection object wrapping provided WebConnection.
 * @param webConnection the webConnection that does the real work
 * @throws IllegalArgumentException if the connection is {@code null}
 */
public WebConnectionWrapper(final WebConnection webConnection) throws IllegalArgumentException {
    if (webConnection == null) {
        throw new IllegalArgumentException("Wrapped connection can't be null");
    }
    wrappedWebConnection_ = webConnection;
}
 
Example #13
Source File: MockMvcConnectionBuilderSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void mockMvcExampleDotCom() throws Exception {
	WebConnection conn = this.builder.useMockMvcForHosts("example.com").createConnection(this.client);

	assertMockMvcUsed(conn, "http://localhost/");
	assertMockMvcUsed(conn, "https://example.com/");
	assertMockMvcNotUsed(conn, "http://other.com/");
}
 
Example #14
Source File: MockMvcConnectionBuilderSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void context() throws Exception {
	WebConnection conn = this.builder.createConnection(this.client);

	assertMockMvcUsed(conn, "http://localhost/");
	assertMockMvcNotUsed(conn, "https://example.com/");
}
 
Example #15
Source File: WebConnectionWrapper.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a WebConnection object wrapping provided WebConnection.
 * @param webConnection the webConnection that does the real work
 * @throws IllegalArgumentException if the connection is {@code null}
 */
public WebConnectionWrapper(final WebConnection webConnection) throws IllegalArgumentException {
    if (webConnection == null) {
        throw new IllegalArgumentException("Wrapped connection can't be null");
    }
    wrappedWebConnection_ = webConnection;
}
 
Example #16
Source File: DelegatingWebConnection.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public DelegatingWebConnection(WebConnection defaultConnection, List<DelegateWebConnection> connections) {
	Assert.notNull(defaultConnection, "Default WebConnection must not be null");
	Assert.notEmpty(connections, "Connections List must not be empty");
	this.connections = connections;
	this.defaultConnection = defaultConnection;
}
 
Example #17
Source File: DelegatingWebConnection.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public DelegatingWebConnection(WebConnection defaultConnection, DelegateWebConnection... connections) {
	this(defaultConnection, Arrays.asList(connections));
}
 
Example #18
Source File: DelegatingWebConnection.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public DelegateWebConnection(WebRequestMatcher matcher, WebConnection delegate) {
	this.matcher = matcher;
	this.delegate = delegate;
}
 
Example #19
Source File: DelegatingWebConnection.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public DelegatingWebConnection(WebConnection defaultConnection, DelegateWebConnection... connections) {
	this(defaultConnection, Arrays.asList(connections));
}
 
Example #20
Source File: DelegatingWebConnection.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private WebConnection getDelegate() {
	return this.delegate;
}
 
Example #21
Source File: DelegatingWebConnection.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public DelegateWebConnection(WebRequestMatcher matcher, WebConnection delegate) {
	this.matcher = matcher;
	this.delegate = delegate;
}
 
Example #22
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private WebResponse getResponse(WebConnection connection, String url) throws IOException {
	return connection.getResponse(new WebRequest(new URL(url)));
}
 
Example #23
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertMockMvcNotUsed(WebConnection connection, String url) throws Exception {
	assertThat(getResponse(connection, url), nullValue());
}
 
Example #24
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertMockMvcUsed(WebConnection connection, String url) throws Exception {
	assertThat(getResponse(connection, url), notNullValue());
}
 
Example #25
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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"));
}
 
Example #26
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void defaultContextPathEmpty() throws Exception {
	WebConnection conn = this.builder.createConnection(this.client);
	assertThat(getResponse(conn, "http://localhost/abc").getContentAsString(), equalTo(""));
}
 
Example #27
Source File: MockMvcConnectionBuilderSupportTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void mockMvcAlwaysUseMockMvc() throws Exception {
	WebConnection conn = this.builder.alwaysUseMockMvc().createConnection(this.client);
	assertMockMvcUsed(conn, "http://other.com/");
}
 
Example #28
Source File: WebConnectionHtmlUnitDriver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Set the {@link WebConnection} to be used with the {@link WebClient}.
 * @param webConnection the {@code WebConnection} to use; never {@code null}
 */
public void setWebConnection(WebConnection webConnection) {
	Assert.notNull(webConnection, "WebConnection must not be null");
	this.webClient.setWebConnection(webConnection);
}
 
Example #29
Source File: WebConnectionHtmlUnitDriver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Set the {@link WebConnection} to be used with the {@link WebClient}.
 * @param webConnection the {@code WebConnection} to use
 */
public void setWebConnection(WebConnection webConnection) {
	Assert.notNull(webConnection, "WebConnection must not be null");
	getWebClient().setWebConnection(webConnection);
}
 
Example #30
Source File: MockMvcConnectionBuilderSupportTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Before
public void setup() {
	given(this.client.getWebConnection()).willReturn(mock(WebConnection.class));
	this.builder = new MockMvcWebConnectionBuilderSupport(this.wac) {};
}