无法使用模拟HTTP / HTTPS获取json / xml响应的发布请求

时间:2015-01-08 10:19:56

标签: java hibernate unit-testing junit

这是我的junit代码。

我正在关注模拟服务器http://www.mock-server.com/

的此文档
mockServerClient=new MockServerClient("127.0.0.1", 8080)
    .when(
            request()
                    .withMethod("POST")
                    .withPath("/login")
                    .withQueryStringParameters(
                            new Parameter("returnUrl", "/account")
                    )
                    .withCookies(
                            new Cookie("sessionId", "2By8LOhBmaW5nZXJwcmludCIlMDAzMW")
                    )
                    .withBody(exact("{username: 'foo', password: 'bar'}")),
            exactly(1)
    )
    .respond(
            response()
                    .withStatusCode(401)
                    .withHeaders(
                            new Header("Content-Type", "application/json; charset=utf-8"),
                            new Header("Cache-Control", "public, max-age=86400")
                    )
                    .withBody("{ message: 'incorrect username and password combination' }")
                    .withDelay(new Delay(TimeUnit.SECONDS, 1))
    );

问题是请求,响应,确切,准确,标题未在编辑器中得到解决。 必须进行一些额外的导入,但我找不到任何有用的示例/备用。

我只需要模拟服务器URL请求和在Junits中使用的响应。 作为模拟服务器的替代方案,我还尝试了Mockwebserver - https://github.com/square/okhttp/tree/master/mockwebserver

但它也有许多限制,比如我无法使用mockwebserver发布帖子。 另请查看此处建议的其他选项https://stackoverflow.com/questions/393099/mocking-http-server。 如果有人知道工作解决方案,请帮助我。 提前致谢

1 个答案:

答案 0 :(得分:0)

您使用的是Windows吗?如果是这样,你可以试试我的模拟服务器吗? https://mockserver.codeplex.com/