Search in sources :

Example 16 with HttpServer

use of com.github.dreamhead.moco.HttpServer in project webmagic by code4craft.

the class HttpClientDownloaderTest method test_set_site_cookie.

@Test
public void test_set_site_cookie() throws Exception {
    HttpServer server = httpServer(13423);
    server.get(eq(cookie("cookie"), "cookie-webmagic")).response("ok");
    Runner.running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
            Request request = new Request();
            request.setUrl("http://127.0.0.1:13423");
            Site site = Site.me().addCookie("cookie", "cookie-webmagic").setDomain("127.0.0.1");
            Page page = httpClientDownloader.download(request, site.toTask());
            assertThat(page.getRawText()).isEqualTo("ok");
        }
    });
}
Also used : Site(us.codecraft.webmagic.Site) Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(us.codecraft.webmagic.Request) Page(us.codecraft.webmagic.Page) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 17 with HttpServer

use of com.github.dreamhead.moco.HttpServer in project webmagic by code4craft.

the class HttpClientDownloaderTest method test_disableCookieManagement.

@Test
public void test_disableCookieManagement() throws Exception {
    HttpServer server = httpServer(13423);
    server.get(not(eq(cookie("cookie"), "cookie-webmagic"))).response("ok");
    Runner.running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
            Request request = new Request();
            request.setUrl("http://127.0.0.1:13423");
            request.addCookie("cookie", "cookie-webmagic");
            Page page = httpClientDownloader.download(request, Site.me().setDisableCookieManagement(true).toTask());
            assertThat(page.getRawText()).isEqualTo("ok");
        }
    });
}
Also used : Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(us.codecraft.webmagic.Request) Page(us.codecraft.webmagic.Page) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 18 with HttpServer

use of com.github.dreamhead.moco.HttpServer in project webmagic by code4craft.

the class HttpClientDownloaderTest method test_download_binary_content.

@Test
public void test_download_binary_content() throws Exception {
    HttpServer server = httpServer(13423);
    server.response("binary");
    Runner.running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            final HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
            Request request = new Request();
            request.setBinaryContent(true);
            request.setUrl("http://127.0.0.1:13423/");
            Page page = httpClientDownloader.download(request, Site.me().toTask());
            assertThat(page.getRawText()).isNull();
            assertThat(page.getBytes()).isEqualTo("binary".getBytes());
        }
    });
}
Also used : Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(us.codecraft.webmagic.Request) Page(us.codecraft.webmagic.Page) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 19 with HttpServer

use of com.github.dreamhead.moco.HttpServer in project moco by dreamhead.

the class ActualHttpServerTest method should_merge_https_server_into_http_server.

@Test
public void should_merge_https_server_into_http_server() throws Exception {
    httpServer = httpsServer(12306, DEFAULT_CERTIFICATE, context("/foo"));
    httpServer.response("foo");
    HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer);
    running(mergedServer, () -> assertThat(helper.get(remoteHttpsUrl("/foo/anything")), is("foo")));
}
Also used : HttpServer(com.github.dreamhead.moco.HttpServer) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Example 20 with HttpServer

use of com.github.dreamhead.moco.HttpServer in project moco by dreamhead.

the class ActualHttpServerTest method should_merge_https_server.

@Test
public void should_merge_https_server() throws Exception {
    anotherServer = httpsServer(12306, DEFAULT_CERTIFICATE, context("/bar"));
    HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer);
    running(mergedServer, () -> assertThat(helper.get(remoteHttpsUrl("/foo/anything")), is("foo")));
}
Also used : HttpServer(com.github.dreamhead.moco.HttpServer) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Aggregations

HttpServer (com.github.dreamhead.moco.HttpServer)27 Test (org.junit.Test)25 AbstractMocoHttpTest (com.github.dreamhead.moco.AbstractMocoHttpTest)13 Runnable (com.github.dreamhead.moco.Runnable)12 IOException (java.io.IOException)12 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)12 Request (us.codecraft.webmagic.Request)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 Page (us.codecraft.webmagic.Page)10 Site (us.codecraft.webmagic.Site)3 ActualHttpServer (com.github.dreamhead.moco.internal.ActualHttpServer)2 RunnerSetting.aRunnerSetting (com.github.dreamhead.moco.runner.RunnerSetting.aRunnerSetting)1 Map (java.util.Map)1 HashedMap (org.apache.commons.collections.map.HashedMap)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 Proxy (us.codecraft.webmagic.proxy.Proxy)1