?????? 2013 ??Duke's Choice Awards???Moco ?????????????????? Java ????????? Twitter ?????? Martin Fowler ????????????????????????????????? InfoQ ????
????Moco ?μ? Hello World
????????????????
???????????? Java ????
???????? Java ?????????ú??????????? ??PATH?? JAVA_HOME??
????????????? Gradle
???????????ο?http://www.gradle.org/
???????????????????????譎? Moco
??????? Moco ????
??????? git ????????μ????
????git clone https://github.com/dreamhead/moco.git
????????????????????? Jar ????????? 0.9.1
????http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.9.1/moco-runner-0.9.1-standalone.jar
????????????????? Jar ??? (????????? Jar ????????????????????) ??????????????μ?????
????cd <moco directory>
????./gradlew build
??????д?????????????? Hello World ???
????[
????{
????"response" :
????{
????"text" : "Hello?? Moco"
????}
????}
????]
??????????? json ?????洢?????? foo.json
??????? Moco ????
????????????????
????java -jar moco-runner-<version>-standalone.jar start -p 12306 -c foo.json
???????-p ??? Moco ?????? ??????? Web ????
???????? Web ????
????????????????? http://localhost:12306
??????????????????????Υ???"Hello World"
????Moco ????????
?????????? ???????? HTTP ????
??????Щ???????????????????????????????????????????????????"queries"?????
???????????
????{
????"request" :
????{
????"uri" : "/foo"??
????"queries" :
????{
????"param" : "blah"
????}
????}??
????"response" :
????{
????"text" : "bar"
????}
????}
????????????????????
????http://localhost:12306/foo?parm=blash
?????????????????????? URL ???
????????? URL ?????????У??????????????????? URL???????????????
???????????
????{
????"request":
????{
????"uri":
????{
????"match": "/\w*/foo"
????}
????}??
????"response":
????{
????"text": "bar"
????}
????}
????????????
?????????????????????/foo ?????????????? ???? http://localhost:12306/xxx/foo??
??????????????
?????????????????????? Web ??????????????????
???????????
????{
????"request" :
????{
????"uri" : "/redirect"
????}??
????"redirectTo" : "http://www.github.com"
????}
???????????????? http://localhost:12306/redirect
??????潫????????? http://www.github.com
?????????? ???? JSON ????
?????? Web ?????У????? JSON ??????????????
???????????
????{
????"request": {
????"uri": "/json"
????}??
????"response": {
????"json": {
????"foo" : "bar"
????}
????}
????}
???????????????? http://localhost:12306/json
?????????????????? JSON ????
????Moco ????÷?
?????? Moco ??????????????Щ???????????????÷??????? Asynchronous??Template???????÷?????ο? Moco ?????????????? Asynchronous ?????
??????д???????
{
"request": {
"uri" : "/event"
}??
"response": {
"text": "event"
}??
"on": {
"complete": {
"async" : "true"??
"post" : {
"url" : "http://another_site"??
"content": "content"
}
}
}
}
???????????/event ????????????????
???????????????????????????????????? http://another_siter ?????????????????? Response ??
????Moco ?? API ?÷?
??????????Щ?÷??? Moco ?????"Standalone"?? ????????? Moco ???????????????? Moco ?? API ???????????????????????????????????????????? Moco??
?????????????????? Moco ????????????
import org.junit.Test;
import java.io.IOException;
import com.github.dreamhead.moco.HttpServer;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import com.github.dreamhead.moco.Runnable;
import static com.github.dreamhead.moco.Moco.*;
import static com.github.dreamhead.moco.Runner.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@Test
public void should_response_as_expected() throws Exception {
HttpServer server = httpserver(12306);
server.response("foo");
running(server?? new Runnable() {
@Override
public void run() throws IOException {
Content content = Request.Get("http://localhost:12306").execute().returnContent();
assertThat(content.asString()?? is("foo"));
}
});
}
?????????????????????????????? Moco?????????????????????????д?????
????????????????????????????????? Server ??????????????????@Before?? @After ??Щ Junit ????????
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static com.github.dreamhead.moco.Moco.httpserver;
import static com.github.dreamhead.moco.Runner.runner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class MocoRunnerTest {
private Runner runner;
@Before
public void setup() {
HttpServer server = httpserver(12306);
server.response("foo");
runner = runner(server);
runner.start();
helper = new MocoTestHelper();
}
@After
public void tearDown() {
runner.stop();
}
@Test
public void should_response_as_expected() throws IOException {
Content content = Request.Get("http://localhost:12306").execute().returnContent();
assertThat(content.asString()?? is("foo"));
}
}
????Moco + Web ??????????
????????? Moco ???????÷???????????????????????? Moco ???????????????? Moco ???? Web ???????????????????
??????????
?????? Web ????? Ajax ????????????汾??version????????????????????????????????
??????????
???????????????????????????????????????????????????"??????????????"???????????????? version ???????????????????????????????????????
?????????????????????????
????????????ú? Web Server (Tomcat or Apache)
????????????? Web ????????????????????????????? struts2
?????????????? Java ?????????????????????? jar ??????????????????????????????????? 4
???????????????????????????????? Lib ???? so ????????????????е?????£?????????????????????????
???????? Web ??ò???????????? API ?б????????????????????????в??? 4??
??????д?????? Ajax ?????
?????????????????? Moco ???????????????????
??????д????????? ?????????????????????
[
{
"env" : "remote"??
"include": "foo.json"??
}??
{
"env" : "local"??
"include": "bar.json"??
}
]
??????? Moco ????
????java -jar moco-runner-<version>-standalone.jar start -p 12306 -g env.json -e remote
???????????????????????????????? 3??4 ?У????????б????? Lib ???? so???????????????????????Web ??????????????????????????????????滹???????????????????
??????д Web ???????? ajax ????
????Moco ???????????????????????????????????????? Lib ???? so ????????????????и???? Web ????????????????????????????
???????????? API ?????? Web ???????д?????????????????????????Ч???????????????????????????????????? Web ?????????????????Ч??????????????????