Search in sources :

Example 11 with HttpServer

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

the class ActualHttpServerTest method should_merge_http_server_without_port_for_second_server.

@Test
public void should_merge_http_server_without_port_for_second_server() throws Exception {
    httpServer = httpServer(context("/foo"));
    anotherServer = httpServer(12307, context("/bar"));
    final HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer);
    running(mergedServer, () -> assertThat(mergedServer.port(), is(12307)));
}
Also used : HttpServer(com.github.dreamhead.moco.HttpServer) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Example 12 with HttpServer

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

the class ActualHttpServerTest method should_merge_http_server_with_different_port.

@Test
public void should_merge_http_server_with_different_port() throws Exception {
    httpServer = httpServer(12306, context("/foo"));
    anotherServer = httpServer(12307, context("/bar"));
    final HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer);
    running(mergedServer, () -> assertThat(mergedServer.port(), is(12307)));
}
Also used : HttpServer(com.github.dreamhead.moco.HttpServer) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Example 13 with HttpServer

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

the class ActualHttpServerTest method should_throw_exception_for_merging_http_server_with_any_handler_one_side.

@Test(expected = HttpResponseException.class)
public void should_throw_exception_for_merging_http_server_with_any_handler_one_side() throws Exception {
    HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer);
    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)

Example 14 with HttpServer

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

the class JsonRunner method createBaseHttpServer.

private HttpServer createBaseHttpServer(final Iterable<? extends RunnerSetting> settings, final StartArgs startArgs) {
    HttpServer targetServer = createHttpServer(startArgs);
    for (RunnerSetting setting : settings) {
        HttpServer parsedServer = httpParser.parseServer(setting.getStreams(), startArgs.getPort().orElse(0), toConfigs(setting));
        targetServer = mergeServer(targetServer, parsedServer);
    }
    return targetServer;
}
Also used : ActualHttpServer(com.github.dreamhead.moco.internal.ActualHttpServer) HttpServer(com.github.dreamhead.moco.HttpServer) RunnerSetting.aRunnerSetting(com.github.dreamhead.moco.runner.RunnerSetting.aRunnerSetting)

Example 15 with HttpServer

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

the class HttpClientDownloaderTest method test_set_request_cookie.

@Test
public void test_set_request_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");
            request.addCookie("cookie", "cookie-webmagic");
            Page page = httpClientDownloader.download(request, Site.me().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)

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