Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from dcx841/SessionConfig
Browse files Browse the repository at this point in the history
Handle recording for a NSURLSessionConfiguration.
  • Loading branch information
JinlianWang committed Oct 26, 2015
2 parents e258c20 + a357460 commit ccd0dde
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
10 changes: 9 additions & 1 deletion SWHttpTrafficRecorder.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,18 @@ FOUNDATION_EXPORT NSString * const SWHttpTrafficRecorderErrorDomain;

/**
* Method to start recording and saves recorded files at a specified location.
* @param path The path where recorded files are saved.
* @param error An out value that returns any error encountered while accessing the recordingPath. Returns an NSError object if any error; otherwise returns nil.
*/
- (void)startRecordingAtPath:(NSString *)recordingPath error:(NSError **) error;

/**
* Method to start recording and saves recorded files at a specified location using default session configuration.
* @param recordingPath The path where recorded files are saved.
* @param sessionConfig The NSURLSessionConfiguration which will be modified.
* @param error An out value that returns any error encountered while accessing the recordingPath. Returns an NSError object if any error; otherwise returns nil.
*/
- (void)startRecordingAtPath:(NSString*)recordingPath error:(NSError **)error;
- (void)startRecordingAtPath:(NSString *)recordingPath forSessionConfiguration:(NSURLSessionConfiguration *)sessionConfig error:(NSError **) error;

/**
* Method to stop recording.
Expand Down
52 changes: 37 additions & 15 deletions SWHttpTrafficRecorder.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@

NSString * const SWHttpTrafficRecorderErrorDomain = @"RECORDER_ERROR_DOMAIN";

@interface SWRecordingProtocol : NSURLProtocol @end

@interface SWHttpTrafficRecorder()
@property(nonatomic, assign, readwrite) BOOL isRecording;
@property(nonatomic, strong) NSString *recordingPath;
@property(nonatomic, assign) int fileNo;
@property(nonatomic, strong) NSOperationQueue *fileCreationQueue;
@property(nonatomic, strong) NSURLSessionConfiguration *sessionConfig;
@end

@interface SWRecordingProtocol : NSURLProtocol @end

@implementation SWHttpTrafficRecorder

+ (instancetype)sharedRecorder
Expand All @@ -57,38 +58,58 @@ + (instancetype)sharedRecorder
}

- (void)startRecording{
[self startRecordingAtPath:nil error:nil];
[self startRecordingAtPath:nil forSessionConfiguration:nil error:nil];
}

- (void)startRecordingAtPath:(NSString *)path error:(NSError **)error{
- (void)startRecordingAtPath:(NSString *)recordingPath error:(NSError **) error {
[self startRecordingAtPath:recordingPath forSessionConfiguration:nil error:error];
}

- (void)startRecordingAtPath:(NSString *)recordingPath forSessionConfiguration:(NSURLSessionConfiguration *)sessionConfig error:(NSError **) error {
if(!self.isRecording){
if(path){
self.recordingPath = path;
if(recordingPath){
self.recordingPath = recordingPath;
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path]){
if(![fileManager fileExistsAtPath:recordingPath]){
NSError *bError = nil;
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&bError];
[fileManager createDirectoryAtPath:recordingPath withIntermediateDirectories:YES attributes:nil error:&bError];
if(bError){
*error = [NSError errorWithDomain:SWHttpTrafficRecorderErrorDomain code:SWHttpTrafficRecorderErrorPathFailedToCreate userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path '%@' does not exist and error while creating it.", path]}];
*error = [NSError errorWithDomain:SWHttpTrafficRecorderErrorDomain code:SWHttpTrafficRecorderErrorPathFailedToCreate userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path '%@' does not exist and error while creating it.", recordingPath]}];
return;
}
} else if(![fileManager isWritableFileAtPath:path]){
*error = [NSError errorWithDomain:SWHttpTrafficRecorderErrorDomain code:SWHttpTrafficRecorderErrorPathNotWritable userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path '%@' is not writable.", path]}];
} else if(![fileManager isWritableFileAtPath:recordingPath]){
*error = [NSError errorWithDomain:SWHttpTrafficRecorderErrorDomain code:SWHttpTrafficRecorderErrorPathNotWritable userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Path '%@' is not writable.", recordingPath]}];
return;
}
} else {
self.recordingPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
}

[NSURLProtocol registerClass:[SWRecordingProtocol class]];
if(sessionConfig){
self.sessionConfig = sessionConfig;
NSMutableArray *mutableProtocols = [[NSMutableArray alloc] initWithArray:sessionConfig.protocolClasses];
[mutableProtocols insertObject:[SWRecordingProtocol class] atIndex:0];
sessionConfig.protocolClasses = mutableProtocols;
}
else {
[NSURLProtocol registerClass:[SWRecordingProtocol class]];
}
}
self.isRecording = YES;
}


- (void)stopRecording{
if(self.isRecording){
[NSURLProtocol unregisterClass:[SWRecordingProtocol class]];
if(self.sessionConfig) {
NSMutableArray *mutableProtocols = [[NSMutableArray alloc] initWithArray:self.sessionConfig.protocolClasses];
[mutableProtocols removeObject:[SWRecordingProtocol class]];
self.sessionConfig.protocolClasses = mutableProtocols;
self.sessionConfig = nil;
}
else {
[NSURLProtocol unregisterClass:[SWRecordingProtocol class]];
}

}
self.isRecording = NO;
}
Expand Down Expand Up @@ -338,6 +359,7 @@ -(void)createBodyOnlyFileWithRequest:(NSURLRequest*)request response:(NSHTTPURLR
-(void)createMocktailFileWithRequest:(NSURLRequest*)request response:(NSHTTPURLResponse*)response data:(NSData*)data atFilePath:(NSString *)filePath
{
NSMutableString *tail = NSMutableString.new;

[tail appendFormat:@"%@\n", request.HTTPMethod];
[tail appendFormat:@"%@\n", [self getURLRegexPattern:request]];
[tail appendFormat:@"%ld\n", (long)response.statusCode];
Expand Down Expand Up @@ -428,4 +450,4 @@ + (void)updateRecorderProgressDelegate:(SWHTTPTrafficRecordingProgressKind)progr
}
}

@end
@end

0 comments on commit ccd0dde

Please sign in to comment.