Search in sources :

Example 1 with Runnable

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

the class HttpClientDownloaderTest method test_download_when_task_is_null.

@Test
public void test_download_when_task_is_null() throws Exception {
    HttpServer server = httpserver(12306);
    server.response("foo");
    Runner.running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            final HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
            Request request = new Request();
            request.setUrl("http://127.0.0.1:12306/");
            Page page = httpClientDownloader.download(request, null);
            assertThat(page.getRawText()).isEqualTo("foo");
        }
    });
}
Also used : Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) Request(us.codecraft.webmagic.Request) Page(us.codecraft.webmagic.Page) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 2 with Runnable

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

the class HttpClientDownloaderTest method test_selectRequestMethod.

@Test
public void test_selectRequestMethod() throws Exception {
    HttpServer server = httpserver(12306);
    server.get(eq(query("q"), "webmagic")).response("get");
    server.post(eq(form("q"), "webmagic")).response("post");
    server.put(eq(form("q"), "webmagic")).response("put");
    server.delete(eq(query("q"), "webmagic")).response("delete");
    server.request(and(by(method("HEAD")), eq(query("q"), "webmagic"))).response(header("method", "head"));
    server.request(and(by(method("TRACE")), eq(query("q"), "webmagic"))).response("trace");
    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:12306/search");
            request.putParams("q", "webmagic");
            request.setMethod(HttpConstant.Method.GET);
            RequestBuilder requestBuilder = httpClientDownloader.selectRequestMethod(request).setUri(request.getUrl());
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(requestBuilder.build()).getEntity())).isEqualTo("get");
            request.setMethod(HttpConstant.Method.POST);
            requestBuilder = httpClientDownloader.selectRequestMethod(request).setUri(request.getUrl());
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(requestBuilder.build()).getEntity())).isEqualTo("post");
            request.setMethod(HttpConstant.Method.PUT);
            requestBuilder = httpClientDownloader.selectRequestMethod(request).setUri(request.getUrl());
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(requestBuilder.build()).getEntity())).isEqualTo("put");
            request.setMethod(HttpConstant.Method.DELETE);
            requestBuilder = httpClientDownloader.selectRequestMethod(request).setUri(request.getUrl());
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(requestBuilder.build()).getEntity())).isEqualTo("delete");
            request.setMethod(HttpConstant.Method.HEAD);
            requestBuilder = httpClientDownloader.selectRequestMethod(request).setUri(request.getUrl());
            assertThat(HttpClients.custom().build().execute(requestBuilder.build()).getFirstHeader("method").getValue()).isEqualTo("head");
            request.setMethod(HttpConstant.Method.TRACE);
            requestBuilder = httpClientDownloader.selectRequestMethod(request).setUri(request.getUrl());
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(requestBuilder.build()).getEntity())).isEqualTo("trace");
        }
    });
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder) Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) Request(us.codecraft.webmagic.Request) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 3 with Runnable

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

the class HttpClientDownloaderTest method testGetHtmlCharset.

@Test
public void testGetHtmlCharset() throws Exception {
    HttpServer server = httpserver(12306);
    server.get(by(uri("/header"))).response(header("Content-Type", "text/html; charset=gbk"));
    server.get(by(uri("/meta4"))).response(with(text("<html>\n" + "  <head>\n" + "    <meta charset='gbk'/>\n" + "  </head>\n" + "  <body></body>\n" + "</html>")), header("Content-Type", ""));
    server.get(by(uri("/meta5"))).response(with(text("<html>\n" + "  <head>\n" + "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\" />\n" + "  </head>\n" + "  <body></body>\n" + "</html>")), header("Content-Type", ""));
    Runner.running(server, new Runnable() {

        @Override
        public void run() {
            String charset = getCharsetByUrl("http://127.0.0.1:12306/header");
            assertEquals(charset, "gbk");
            charset = getCharsetByUrl("http://127.0.0.1:12306/meta4");
            assertEquals(charset, "gbk");
            charset = getCharsetByUrl("http://127.0.0.1:12306/meta5");
            assertEquals(charset, "gbk");
        }

        private String getCharsetByUrl(String url) {
            HttpClientDownloader downloader = new HttpClientDownloader();
            Site site = Site.me();
            CloseableHttpClient httpClient = new HttpClientGenerator().getClient(site, null);
            // encoding in http header Content-Type
            Request requestGBK = new Request(url);
            CloseableHttpResponse httpResponse = null;
            try {
                httpResponse = httpClient.execute(downloader.getHttpUriRequest(requestGBK, site, null, null));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String charset = null;
            try {
                byte[] contentBytes = IOUtils.toByteArray(httpResponse.getEntity().getContent());
                charset = downloader.getHtmlCharset(httpResponse, contentBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return charset;
        }
    });
}
Also used : Site(us.codecraft.webmagic.Site) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) Request(us.codecraft.webmagic.Request) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Runnable

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

the class MocoConnectionTest method should_keep_alive_for_1_0_keep_alive_request.

@Test
public void should_keep_alive_for_1_0_keep_alive_request() throws Exception {
    server.response("foo");
    running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            Response response = Request.Get(root()).version(HttpVersion.HTTP_1_0).addHeader("Connection", "keep-alive").execute();
            String connection = response.returnResponse().getFirstHeader("Connection").getValue();
            assertThat(connection, is("keep-alive"));
        }
    });
}
Also used : Response(org.apache.http.client.fluent.Response) Runnable(com.github.dreamhead.moco.Runnable) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Example 5 with Runnable

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

the class ActualHttpServerTest method should_throw_for_merging_http_server_with_any_handler_other_side.

@Test(expected = HttpResponseException.class)
public void should_throw_for_merging_http_server_with_any_handler_other_side() throws Exception {
    HttpServer mergedServer = ((ActualHttpServer) httpServer).mergeServer((ActualHttpServer) anotherServer);
    running(mergedServer, new Runnable() {

        @Override
        public void run() throws Exception {
            helper.get(remoteUrl("/bar/anything"));
        }
    });
}
Also used : Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Aggregations

Runnable (com.github.dreamhead.moco.Runnable)21 Test (org.junit.Test)21 HttpServer (com.github.dreamhead.moco.HttpServer)16 IOException (java.io.IOException)16 AbstractMocoHttpTest (com.github.dreamhead.moco.AbstractMocoHttpTest)15 HttpResponseException (org.apache.http.client.HttpResponseException)11 SocketServer (com.github.dreamhead.moco.SocketServer)3 Request (us.codecraft.webmagic.Request)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Response (org.apache.http.client.fluent.Response)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 Page (us.codecraft.webmagic.Page)1 Site (us.codecraft.webmagic.Site)1