Network Loging

With TestFairy, you can log all your network requests. This gives you an easy way to monitor network access your app is doing.

Syntax

TestFairy.addNetworkEvent(URI uri, String method, int code, long startTimeMillis, long endTimeMillis, long requestSize, long responseSize, String errorMessage);

Code Example

If you are using OkHttp or Retrofit all you need to do is add CustomHttpInterceptor to your client:

// Be sure to import TestFairy
import com.testfairy.TestFairy;

public class CustomHttpInterceptor implements Interceptor {
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {

        Request request = chain.request();
        long startTimeMillis = System.currentTimeMillis();
        Long requestSize = request.body() != null ? request.body().contentLength() : 0;
        Response response;
        try {
            response = chain.proceed(request);
        } catch (IOException e) {
            long endTimeMillis = System.currentTimeMillis();
            TestFairy.addNetworkEvent(request.url().uri(), request.method(), -1, startTimeMillis, endTimeMillis, requestSize, -1, e.getMessage());
            throw e;
        }

        long endTimeMillis = System.currentTimeMillis();
        long responseSize = response.body() != null ? response.body().contentLength() : 0;
        TestFairy.addNetworkEvent(request.url().uri(), request.method(), response.code(), startTimeMillis, endTimeMillis, requestSize, responseSize, null);
        return response;
    }
}


OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new CustomHttpInterceptor())
    .build();
            

Syntax

[TestFairy addNetwork:(NSURLSessionTask *)task error:(NSError *)error]

Code Example

If you are using NSURLConnection all you need to do is add callback to your request at the beginning of the request and at the end.

Note: If you have AFNetworking added to your project, network requests will be automatically captured when enabled in your build settings.

// Be sure to import TestFairy
#import "TestFairy.h"

__block NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // ...
    [TestFairy addNetwork:task error:error];
}];
[TestFairy addNetwork:task error:nil];
[task resume];
            

Syntax

TestFairy.addNetwork(<URLSessionTask>, error:<NSError>)

Code Example

If you are using URLConnection all you need to do is add callback to your request at the beginning of the request and at the end:

var task: URLSessionTask? = nil
task = URLSession.shared.dataTask(with: URL(string:"")!) { (data, response, error) in
    TestFairy.addNetwork(task, error: error)
}
TestFairy.addNetwork(task, error: nil)
task?.resume()
            

With Alamofire, it's even easier:

import Alamofire

NotificationCenter.default.addObserver(forName: Request.didResume, object: nil, queue: nil) { (notification) in
    let info = notification.userInfo
    let request = info?["org.alamofire.notification.key.request"] as! Request
    request.tasks.forEach { TestFairy.addNetwork($0, error: nil) }
}

NotificationCenter.default.addObserver(forName: Request.didComplete, object: nil, queue: nil) { (notification) in
    let info = notification.userInfo
    let request = info?["org.alamofire.notification.key.request"] as! Request
    request.tasks.forEach { TestFairy.addNetwork($0, error: nil) }
}

Last updated on 2019-09-09