kasketis / netfox

A lightweight, one line setup, iOS / OSX network debugging library! 🦊
MIT License
3.67k stars 374 forks source link

Custom `URLSessionConfiguration.httpCookieStorage` is ignored #234

Open guillermomuntaner opened 2 years ago

guillermomuntaner commented 2 years ago

Describe the bug Custom URLSessionConfiguration.httpCookieStorage are not honored:

public extension Session {
    static let customSession: Session = {
        let sessionConfiguraton = URLSessionConfiguration.default
        sessionConfiguraton.protocolClasses?.insert(NFXProtocol.self, at: 0)
        sessionConfiguraton.httpCookieStorage = customCookieStorage
        return Session(configuration: sessionConfiguraton)
    }()
}

To Reproduce

  1. Use a custom httpCookieStorage. In our case, we use a HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "groupname") to share cookies between app and extensions
  2. Plug netfox, do any request
  3. Cookies are not added from / saved to the expected httpCookieStorage

Expected behavior Cookies are get/set into the configured httpCookieStorage. Instead they seem to be saved into the default one.

Additional context My guess is that cookie handling happens in the URLProtocol implementation that the NFXProtocol shadows. Internally the NFXProtocol uses a URLSession(configuration: .default, ...) session which doesn't use the httpCookieStorage we want it to use.

I guess the solution is either or:

  1. (Ideally) Fix the NFXProtocol to use the httpCookieStorage. Unfortunately I am not familiar with URLProtocol so I don't know exactly what is missing here.
  2. (Probably good enought) Provide a way to configure the URLSession that NFXProtocol uses so we can configure it too