AliSoftware / OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
MIT License
5.03k stars 602 forks source link

Subs are not getting Alamofire 5 parameters #322

Open ricardorauber opened 4 years ago

ricardorauber commented 4 years ago

New Issue Checklist

Environment

Issue Description

I am trying to use it in a project with Alamofire 5.0.5 and for some reason, it is not getting the parameters. I just tried the sample they use on their guide:

stub(condition: isMethodPOST() && isPath("/post")) { request in
    print("Method:", request.httpMethod ?? "-")
    print("Request:", request)
    print("Body:", request.httpBody ?? "-")
    return HTTPStubsResponse(jsonObject: ["result": true], statusCode: 200, headers: nil)
}
struct Login: Encodable {
    let email: String
    let password: String
}
let login = Login(email: "test@test.test", password: "testPassword")
AF.request("https://httpbin.org/post",
           method: .post,
           parameters: login,
           encoder: JSONParameterEncoder.default).validate().responseString { response in
    print("response", response)
}
Complete output when you encounter the issue (if any)
Method: POST
Request: https://httpbin.org/post
Body: -
response success("{\"result\":true}")

When using a breakpoint and PO the request, this is what I get:

▿ https://httpbin.org/post
  ▿ url : Optional<URL>
    ▿ some : https://httpbin.org/post
      - _url : https://httpbin.org/post
  - cachePolicy : 0
  - timeoutInterval : 60.0
  - mainDocumentURL : nil
  - networkServiceType : __C.NSURLRequestNetworkServiceType
  - allowsCellularAccess : true
  ▿ httpMethod : Optional<String>
    - some : "POST"
  ▿ allHTTPHeaderFields : Optional<Dictionary<String, String>>
    ▿ some : 5 elements
      ▿ 0 : 2 elements
        - key : "Accept-Language"
        - value : "en;q=1.0"
      ▿ 1 : 2 elements
        - key : "Content-Type"
        - value : "application/json"
      ▿ 2 : 2 elements
        - key : "Content-Length"
        - value : "52"
      ▿ 3 : 2 elements
        - key : "User-Agent"
        - value : "dummy/1.0 (dummy; build:1; iOS 13.3.0) Alamofire/5.0.5"
      ▿ 4 : 2 elements
        - key : "Accept-Encoding"
        - value : "br;q=1.0, gzip;q=0.9, deflate;q=0.8"
  - httpBody : nil
  ▿ httpBodyStream : Optional<NSInputStream>
    - some : <__NSCFInputStream: 0x6000037d6ac0>
  - httpShouldHandleCookies : true
  - httpShouldUsePipelining : false

If I remove the stub, it works perfectly with the real server:

response success("{\n  \"args\": {}, \n  \"data\": \"{\\\"email\\\":\\\"test@test.test\\\",\\\"password\\\":\\\"testPassword\\\"}\", \n  \"files\": {}, \n  \"form\": {}, \n  \"headers\": {\n    \"Accept\": \"*/*\", \n    \"Accept-Encoding\": \"br;q=1.0, gzip;q=0.9, deflate;q=0.8\", \n    \"Accept-Language\": \"en;q=1.0\", \n    \"Content-Length\": \"52\", \n    \"Content-Type\": \"application/json\", \n    \"Host\": \"httpbin.org\", \n    \"X-Amzn-Trace-Id\": \"Root=1-5e7d1048-dd2e0ff448f3900c34e0ec90\"\n  }, \n  \"json\": {\n    \"email\": \"test@test.test\", \n    \"password\": \"testPassword\"\n  }, \n  \"origin\": \"187.39.113.117\", \n  \"url\": \"https://httpbin.org/post\"\n}\n")
henrique-morbin-ifood commented 4 years ago

Hi, I am facing the same issue now, and I'm not even using Alamofire.

henrique-morbin-ifood commented 4 years ago

I found it here. The request's body is actually on ohhttpStubs_HTTPBody.