2015-01-08 46 views
0

這是我的junit代碼。無法使用模擬HTTP/HTTPS獲取json/xml響應的發佈請求

我下面這些文件,以模擬服務器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。 如果有人知道工作解決方案,請幫助我。 在此先感謝

+0

我可以使用[Wiremock](http://wiremock.org/index.html)工作。 – SwANDp

回答

0

您使用的是Windows嗎?如果是這樣,也許你可以嘗試我的模擬服務器呢? https://mockserver.codeplex.com/

+0

不,我在Ubuntu上工作。 但我會稍後再試 – SwANDp