WeTransfer / Mocker

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

Support for application/x-www-form-urlencoded data type and dynamic request body inspection #115

Closed csanfilippo closed 2 years ago

csanfilippo commented 2 years ago

Do you guys have any plan to support "application/x-www-form-urlencoded" as Mocker data type? Let me explain my needs.

I'm using your code for mocking some request to a service requiring a body encoded as "application/x-www-form-urlencoded".

I noticed that the onRequest closure of the Mock object assumes that the body of the request is of kind [String: Any]?.

Would be great if Mock.DataType would also have a case for applicationForm and an onRequest closure which takes as a second paramenter a deserialization of the request body aligned to the data type (not always presuming the data type to be a json).

Just for better explaining my stream of consciusness, I'm referring to the fact that the code below automatically deserialize the URLRequest body to a dictionary of [String: Any] (see the code below).

private extension URLRequest {
    var postBodyArguments: [String: Any]? {
        guard let httpBody = httpBodyStreamData() ?? httpBody else { return nil }
        return try? JSONSerialization.jsonObject(with: httpBody, options: .fragmentsAllowed) as? [String: Any]
    }

    /// We need to use the http body stream data as the URLRequest once launched converts the `httpBody` to this stream of data.
    private func httpBodyStreamData() -> Data? {
        // ...
    }
}

Would be great if that deserialization would be dynamic according to the dataType of the current request. Have you ever consider to implement something like that? Is it on your roadmap? Thanks

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.