Skip to content
This repository was archived by the owner on Dec 12, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions VOKMockUrlProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
*/
@interface VOKMockUrlProtocol : NSURLProtocol

/**
* Mock data should go into a directory with the name defined in this string.
* It defaults to "VOKMockData", but its default can be overwritten by subclassing
* VOKMockUrlProtocol and overriding - initWithRequest:cachedResponse:client.
*
* If set to nil, this property will default to VOKMockData
*/
@property (copy, nonatomic) NSString *mockDataDirectory;

@end
15 changes: 12 additions & 3 deletions VOKMockUrlProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif
#endif

static NSString *const MockDataDirectory = @"VOKMockData";
static NSString *const DefaultMockDataDirectory = @"VOKMockData";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about exporting this publicly?
that would allow us to reset back to the default and create subdirectories and keep things organized.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it so the property will return the default if set to nil, which is kind of in the same vein. Would you prefer I do this too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you've got is fine for now.


static NSString *const AppendSeparatorFormat = @"|%@";

Expand All @@ -49,6 +49,15 @@ + (instancetype)containerWithResponse:(NSHTTPURLResponse *)response data:(NSData

@implementation VOKMockUrlProtocol

- (NSString *)mockDataDirectory
{
if (_mockDataDirectory == nil) {
return DefaultMockDataDirectory;
}

return _mockDataDirectory;
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
return YES;
Expand Down Expand Up @@ -314,7 +323,7 @@ - (VOKMockUrlProtocolResponseAndDataContainer *)responseAndData
for (NSString *resourceName in resourceNames) {
filePath = [[NSBundle bundleForClass:[self class]] pathForResource:resourceName
ofType:@"http"
inDirectory:MockDataDirectory];
inDirectory:self.mockDataDirectory];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:NULL];
Expand All @@ -329,7 +338,7 @@ - (VOKMockUrlProtocolResponseAndDataContainer *)responseAndData
for (NSString *resourceName in resourceNames) {
filePath = [[NSBundle bundleForClass:[self class]] pathForResource:resourceName
ofType:@"json"
inDirectory:MockDataDirectory];
inDirectory:self.mockDataDirectory];
NSData *data = [NSData dataWithContentsOfFile:filePath];
if (data) {
// We've got a JSON data file, so send it.
Expand Down