Search in sources :

Example 6 with HttpServer

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

the class HttpClientDownloaderTest method test_selectRequestMethod.

@Test
public void test_selectRequestMethod() throws Exception {
    final int port = 13423;
    HttpServer server = httpServer(port);
    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");
    final HttpUriRequestConverter httpUriRequestConverter = new HttpUriRequestConverter();
    final Site site = Site.me();
    Runner.running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            Request request = new Request();
            request.setUrl("http://127.0.0.1:" + port + "/search?q=webmagic");
            request.setMethod(HttpConstant.Method.GET);
            Map<String, Object> params = new HashedMap();
            params.put("q", "webmagic");
            HttpUriRequest httpUriRequest = httpUriRequestConverter.convert(request, site, null).getHttpUriRequest();
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(httpUriRequest).getEntity())).isEqualTo("get");
            request.setMethod(HttpConstant.Method.DELETE);
            httpUriRequest = httpUriRequestConverter.convert(request, site, null).getHttpUriRequest();
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(httpUriRequest).getEntity())).isEqualTo("delete");
            request.setMethod(HttpConstant.Method.HEAD);
            httpUriRequest = httpUriRequestConverter.convert(request, site, null).getHttpUriRequest();
            assertThat(HttpClients.custom().build().execute(httpUriRequest).getFirstHeader("method").getValue()).isEqualTo("head");
            request.setMethod(HttpConstant.Method.TRACE);
            httpUriRequest = httpUriRequestConverter.convert(request, site, null).getHttpUriRequest();
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(httpUriRequest).getEntity())).isEqualTo("trace");
            request.setUrl("http://127.0.0.1:" + port + "/search");
            request.setMethod(HttpConstant.Method.POST);
            request.setRequestBody(HttpRequestBody.form(params, "utf-8"));
            httpUriRequest = httpUriRequestConverter.convert(request, site, null).getHttpUriRequest();
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(httpUriRequest).getEntity())).isEqualTo("post");
            request.setMethod(HttpConstant.Method.PUT);
            httpUriRequest = httpUriRequestConverter.convert(request, site, null).getHttpUriRequest();
            assertThat(EntityUtils.toString(HttpClients.custom().build().execute(httpUriRequest).getEntity())).isEqualTo("put");
        }
    });
}
Also used : Site(us.codecraft.webmagic.Site) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Runnable(com.github.dreamhead.moco.Runnable) HttpServer(com.github.dreamhead.moco.HttpServer) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(us.codecraft.webmagic.Request) HashedMap(org.apache.commons.collections.map.HashedMap) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 7 with HttpServer

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

the class HttpClientDownloaderTest method test_download_set_charset.

@Test
public void test_download_set_charset() throws Exception {
    HttpServer server = httpServer(13423);
    server.response(header("Content-Type", "text/html; charset=utf-8")).response("hello world!");
    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:13423/");
            Page page = httpClientDownloader.download(request, Site.me().toTask());
            assertThat(page.getCharset()).isEqualTo("utf-8");
        }
    });
}
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 8 with HttpServer

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

the class HttpClientDownloaderTest method test_download_set_request_charset.

@Test
public void test_download_set_request_charset() throws Exception {
    HttpServer server = httpServer(13423);
    server.response("hello world!");
    Runner.running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            final HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
            Request request = new Request();
            request.setCharset("utf-8");
            request.setUrl("http://127.0.0.1:13423/");
            Page page = httpClientDownloader.download(request, Site.me().setCharset("gbk").toTask());
            assertThat(page.getCharset()).isEqualTo("utf-8");
        }
    });
}
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 9 with HttpServer

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

the class ActualHttpServerTest method should_merge_http_server_with_any_handler_other_side.

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

Example 10 with HttpServer

use of com.github.dreamhead.moco.HttpServer 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, () -> helper.get(remoteUrl("/bar/anything")));
}
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