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

Commit

Permalink
Merge pull request #32 from designatednerd/master
Browse files Browse the repository at this point in the history
Add ability to pass in a custom test bundle
  • Loading branch information
designatednerd committed Oct 8, 2015
2 parents 7c1cfa1 + a0a4431 commit fc33f02
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
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, or nil to reset to the bundle VOKMockUrlProtocol is in.
*/
+ (void)setTestBundle:(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;
+ (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

0 comments on commit fc33f02

Please sign in to comment.