????????????????
???????????? ???????[ 2013/5/20 11:29:10 ] ????????
????????Response?????????????????HTTP?????????????????
public class Response {
private final int statusCode;
private final String responseBody;
?????????????EndPoint??????????????????????????????????????????????????????????????????????????????·????????????Σ????????????????????????????????????????????????????????Σ????????????EndPoint??????????????????????????????
??????????????
?????????????EndPoint????????????????????????????????????????????????????????????????????????——“???????”?????????????????????????HTTP?????????????????????????????????????????????????????????????????????????/???Э?飬?????????????????HTTP?????ü???????/???????????в????
????Moco?????????????????????????????????????Moco??“???????????????stub??????????????????”????JUnit?????У????????д?????????????HTTP?????????÷?????????12306??????????????????????“foo”??????
MocoHttpServer server = httpserver(12306);
server.reponse("foo");
?????????????????????????????????????Apache Commons HTTP Client????????????????????????????????????????????????running???У???????????????????????
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??????????????????????????????в???????????????????????????Moco?????????????е????????????????????????????????????????????????????????OpenPTK??OpenPTK?????????XML???Э?飬???????????????????????????/openptk-server/login??????????????????????????ó??????????????????????????Moco server??????????
server = httpserver(12306);
server.post(and(
by(uri("/openptk-server/login"))??
by("clientid=test_app&clientcred=fake_password"))).response(status(200));
???????????????????????????????÷???λ??localhost:12306????????????????????????
configuration = new IdentityServiceConfiguration();
configuration.setHost("http://localhost:12306");
configuration.setClientId("test_app");
configuration.setClientCredential("fake_password");
xmlEndPoint = new XmlEndPoint(configuration);
??????????????????????????????????XmlEndPoint??????GET????????????????URL?????????????
@Test
public void shouldBeAbleToCarryGetRequest() throws Exception {
final String expectedResponse = "<message>SUCCESS</message>";
server.get(by(uri("/get_path"))).response(expectedResponse);
running(server?? new Runnable() {
@Override
public void run() {
XmlEndPointResponse response =
xmlEndPoint.get("http://localhost:12306/get_path");
assertThat(response.getStatusCode()?? equalTo(STATUS_SUCCESS));
assertThat(response.getResponseBody()?? equalTo(expectedResponse));
}
});
}
????????????????????????????????????????“??ó????????”????????????????????XmlEndPoint???get?????????????????
@Test(expected = IdentityServiceSystemException.class)
public void shouldRaiseExceptionIfLoginFails() throws Exception {
configuration.setClientCredential("wrong_password");
running(server?? new Runnable() {
@Override
public void run() {
xmlEndPoint.get("http://localhost:12306/get_path");
}
});
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11