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");
}
});
}
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");
}
});
}
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");
}
});
}
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")));
}
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")));
}
Aggregations