WeTransfer / Mocker

Mock Alamofire and URLSession requests without touching your code implementation
MIT License
1.1k stars 96 forks source link

postBodyArguments doesn't support Collections #119

Closed AllDmeat closed 2 years ago

AllDmeat commented 2 years ago

I'm sending array of objects via network. It's Codable converted to Data and set into URLRequest. But postBodyArguments getter tries to cast it to [String: Any], thus I'm getting nil when accessing it.

URLRequest's extension containing httpBodyStreamData() is private and thus I can't get Data by myself and convert it to [[String: Any]].

As a workaround I've copypasted your URLRequest's extension into my tests and modified it to cast to [[String: Any]]. It's working properly but leads to code duplication.

Generics would have solved this problem

func postBodyArguments<T>() -> T? {
    guard let httpBody = httpBodyStreamData() ?? httpBody else { return nil }
    return try? JSONSerialization.jsonObject(with: httpBody, options: .fragmentsAllowed) as? T
}

let array: [[String: Any]] = postBodyArguments()
let dict: [String: Any] = postBodyArguments()
github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.

RonnyFenrich commented 2 years ago

I just ran into the same issue using Alamofire 5 with the Session.request(_:method:parameters:encoder:headers:interceptor:requestModifier:) method. httpBodyArguments is now just nil in the tests mock.onRequest handlers :-(

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.