capitalone / SWHttpTrafficRecorder

A simple library empowering you to record/capture HTTP(s) traffic of an iOS app for mocking/stubbing later.
Apache License 2.0
205 stars 48 forks source link

Request sent with NSURLSession doesn't get recordet #5

Open chrs1885 opened 8 years ago

chrs1885 commented 8 years ago

Hi,

I'm currently trying out your demo project. While this is working without any problem, requests sent with NSURLSession don't get recorded. To get a repro, simply replace

@IBAction func goButtonTapped(sender: AnyObject) {
    if let urlString = urlTextField.text, url = NSURL(string: urlString){
        browseWebView.loadRequest(NSURLRequest(URL: url))
    }
}

with

@IBAction func goButtonTapped(sender: AnyObject) {
    let request = NSMutableURLRequest(URL: NSURL(string: "http://www.google.com/")!)
    httpGet(request) {
        (resultString, error) -> Void in
        print(resultString)
    }
}

func httpGet(request: NSMutableURLRequest!, callback: (String,
    String?) -> Void) {
    let configuration =
        NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: configuration,
                               delegate: nil,
                               delegateQueue:NSOperationQueue.mainQueue())
    let task = session.dataTaskWithRequest(request){
        (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        callback(response.debugDescription, error?.localizedDescription)
    }
    task.resume()
}

in the ViewController class of the demo project.

Any idea? Thanks for your help.

chrs1885 commented 8 years ago

BTW, here's my setup code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    
    if let sharedRecorder = SWHttpTrafficRecorder.sharedRecorder() {
        sharedRecorder.recordingFormat = SWHTTPTrafficRecordingFormat.BodyOnly
        sharedRecorder.progressDelegate = self
        let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true) as [String]
        try! sharedRecorder.startRecordingAtPath(paths[0], forSessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
    }
    return true
}

I'm currently using 1.0.1 of your library.

Thank you!

Ashton-W commented 8 years ago

NSURLSessionConfiguration.defaultSessionConfiguration() creates a new session configuration every time you call it, it is not an accessor for a shared configuration.

Ashton-W commented 8 years ago

We should consider carefully swizzling NSURLSession to enable recording of all NSURLSessions. eg: https://github.com/AliSoftware/OHHTTPStubs/blob/master/OHHTTPStubs/Sources/NSURLSession/OHHTTPStubs%2BNSURLSessionConfiguration.m

JinlianWang commented 8 years ago

@chrs1885 @Ashton-W Can you save the session configuration instance of the http calls and pass it to recorder? I can also swizzle NSURLSession as suggested, though it does not give precise control of which http calls to be recorded.

chrs1885 commented 8 years ago

Hi,

thank you for the info. you are right about the default session configuration. didn't check the docs and the naming was a bit missleading. unfortunately, I can't access the session configuration, since I'm trying to record the traffic produced by several 3rd party libraries (they don't expose the config instances). swizzling NSURLSession sounds like a valid option for my usecase and is still better than forking 3 repos just to expose the config.

JinlianWang commented 8 years ago

@chrs1885 You convinced me. One way to fix this is to change the implementation for 'SWHttpTrafficRecorder.sharedRecorder().startRecording()' to swizzle NSURLSession to enable recording of all NSURLSessions as suggested by @Ashton-W previously. And still keep the other interface with session configuration as one of the parameters so that users can control to record one particular session when needed. With this said, I am on some other projects now and may not get time to implement this recently. Is it something you are willing to take on and submit a PR?

Amindv1 commented 7 years ago

It would be really useful if you could implement this. I'm not too sure on what you mean by 'swizzle'. I'm having the same issue as OP where I don't have access to the session configuration.