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

Add ability to pass in a custom test bundle #32

Merged
merged 5 commits into from
Oct 8, 2015
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: objective-c
osx_image: xcode7
xcode_workspace: Example/VOKMockUrlProtocol.xcworkspace
xcode_scheme: VOKMockUrlProtocol
xcode_sdk: iphonesimulator
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ In order to switch back and forth between mock and live, you can also take out t
[currentProtocolClasses removeObject:mockURLProtocol];
self.sessionConfiguration.protocolClasses = currentProtocolClasses;
```

### Using with Frameworks/Swift

When `VOKMockURLProtocol` is built as a framework (usually for use with Swift), make sure to call the `setTestBundle:` class method and pass in your test bundle. Since the default behavior is to fall back to the bundle for the current class, that would look in the Framework's bundle rather than the test bundle, and nothing would work.
8 changes: 8 additions & 0 deletions VOKMockUrlProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@
*/
@interface VOKMockUrlProtocol : NSURLProtocol

/**
* Sets a custom test bundle to use to look for the mock data files. Primarily useful
* when VOKMockURLProtocol is being used as a framework (for example, with Swift).
*
* @param bundle The test bundle to use.
*/
+ (void)setTestBundle:(__nonnull NSBundle *)bundle;

@end
16 changes: 13 additions & 3 deletions VOKMockUrlProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ + (instancetype)containerWithResponse:(NSHTTPURLResponse *)response data:(NSData

@implementation VOKMockUrlProtocol

static NSBundle *testBundle = nil;
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 setting testBundle = [NSBundle bundleForClass:[self class]] here instead of checking below when it's used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because if for some reason someone nulls it out at any point I want it to pick up the test bundle again - although I suppose I could handle that case in setTestBundle too...

Copy link
Contributor

Choose a reason for hiding this comment

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

good point, it's fine as is

+ (void)setTestBundle:(NSBundle *)bundle
{
testBundle = bundle;
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
return YES;
Expand Down Expand Up @@ -312,9 +318,13 @@ - (VOKMockUrlProtocolResponseAndDataContainer *)responseAndData

// First, look for a complete-HTTP-response file.
for (NSString *resourceName in resourceNames) {
filePath = [[NSBundle bundleForClass:[self class]] pathForResource:resourceName
ofType:@"http"
inDirectory:MockDataDirectory];
if (!testBundle) {
testBundle = [NSBundle bundleForClass:[self class]];
}

filePath = [testBundle pathForResource:resourceName
ofType:@"http"
inDirectory:MockDataDirectory];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:NULL];
Expand Down
2 changes: 1 addition & 1 deletion VOKMockUrlProtocol.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "VOKMockUrlProtocol"
s.version = "2.0.4"
s.version = "2.1.0"
s.summary = "A url protocol that parses and returns fake responses with mock data."
s.homepage = "https://github.com/vokal/VOKMockUrlProtocol"
s.license = { :type => "MIT", :file => "LICENSE"}
Expand Down