Hello Folks I am exploring WireMock for the iOS ap...
# help
m
Hello Folks I am exploring WireMock for the iOS app. I have developed simple posts app. I am consuming https://jsonplaceholder.typicode.com/posts. I want to stub below cases for ui tests with the help of WireMock • Success with data • Success with empty data • Failed I have tried priority and state however not getting expected result. My mapping look like this:
Copy code
{
  "mappings": [
    {
      "priority": 1,
      "request": {
        "method": "GET",
        "url": "/posts"
      },
      "response": {
        "status": 200,
        "bodyFileName": "posts-default.json"
      }
    },
    {
      "priority": 2,
      "request": {
        "url": "/posts"
      },
      "response": {
        "status": 200,
        "bodyFileName": "posts-empty.json"
      }
    },
    {
      "priority": 3,
      "request": {
        "url": "/posts"
      },
      "response": {
        "status": 500,
        "bodyFileName": "posts-error.json"
      }
    }
  ]
}
Can anyone help me how to achieve one url which has multiple response?
k
There are multiple ways to achieve this. I m not sure about the best alternative. One of way is to send meta header indicating what sort of response is expected and you can add mapping for the same.
m
@Kapish Malik can you share an example?
k
it ll be same mapping. U just need to include header in this example and send header in the request.
Copy code
"headers": {
            "response-type": {
                "equalTo": "error"
            }
        }
response-type u need to send in the request based on response u want
m
@Kapish Malik this is working from postman. It is not working from UITests. I cannot add header
response-type
header. I am using https://github.com/mobileforming/WiremockClient.git. My Stubbing goes like this:
Copy code
let stub = StubMapping.stubFor(requestMethod: .GET,
                                       urlMatchCondition: .urlPathEqualTo,
                                       url: endPoint)
            .withHeader("response-type", matchCondition: .equalTo, value: "success")
            .willReturn(ResponseDefinition())
I have tried to serve response from local file aprt from this approach. It doesn't work. Can you throw some insights how wiremock pick the response apart from most recent stub?
k
checking ur example
@mahbaleshwar hegde hey are u sending this response-type from your code when u r making actual request.
This looks fine to me.
Ur code snippet should work... it seems like u might be missing sending response-type.
as it is working from Postman
m
This stubbing in my unit tests. I am not sending "response-type" in actual request. My request does not "response-type".
apiClient.register {
RESTAPIClient(url: URL(string: "<http://localhost:8080/>")!)
}
Copy code
func test_02ShouldDisplayBlogList() throws {
        app.launch()
        blogPostListStub.successResponse()
        let cell = app.tables["blogPostTableView"].cells.firstMatch
        XCTAssertTrue(cell.waitForExistence(timeout: 10.0))
    }
k
ohh okay... it won't work for UT as u need to pass and make code changes in ur existing code.
@Oleg Nenashev @Tom if u guys can help
@mahbaleshwar hegde if it is just UT then u can test it as different UT all together.
m
I was on vacation. sorry for the late respone. I am trying different tests only. I think you got the problem. I cannot modify production code. is there any other way to get the similar behaviour? I tried scenario, It require new state and required state to simulate. Now tests are coupled.
@Kapish Malik @Oleg Nenashev @Tom can you please throw some insights?
o
You can always do it by stateful behavior. You can define multiple sequential states which give different responses. https://wiremock.org/docs/stateful-behaviour/