OHHTTPStubs
OHHTTPStubs copied to clipboard
Subs are not getting Alamofire 5 parameters
New Issue Checklist
- [X] I have tried with the latest version of OHHTTPStubs
- [X] I have read the README
- [X] I have read the Using the right Swift Version of
OHHTTPStubs
for your project section - [X] I have searched in the existing issues
- [X] I have read the OHHTTPStubs wiki to see if there wasn't a detailed page talking about my issue
Environment
- version of OHHTTPStubs: 9.0.0
- integration method you are using:
- [X] Cocoapods
- [ ] Carthage
- [ ] submodule
- [ ] other
- version of the tool you use: 1.8.4
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: "[email protected]", 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\\\":\\\"[email protected]\\\",\\\"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\": \"[email protected]\", \n \"password\": \"testPassword\"\n }, \n \"origin\": \"187.39.113.117\", \n \"url\": \"https://httpbin.org/post\"\n}\n")
Hi, I am facing the same issue now, and I'm not even using Alamofire.
I found it here. The request's body is actually on ohhttpStubs_HTTPBody
.