Skip to content

Commit

Permalink
Merge pull request #113 from Autoc0diq/fix-nsdata-matcher
Browse files Browse the repository at this point in the history
Converting NSData to NSString can be nil if arbitrary data.
  • Loading branch information
robb committed May 8, 2015
2 parents bcf211f + b383e86 commit f6f85ba
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Nocilla.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
A0BB870615FE7D8F0090236F /* LSStubRequestSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = A0BB870515FE7D8F0090236F /* LSStubRequestSpec.m */; };
A0BBF5CA16DD9BDD00D660D2 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A0BBF5C916DD9BDD00D660D2 /* CoreGraphics.framework */; };
AA0A44061758E27100D2F909 /* NSURLRequest+LSHTTPRequestSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = AA0A44051758E27100D2F909 /* NSURLRequest+LSHTTPRequestSpec.m */; };
E48784AC1AFD7678001B5F2D /* LSDataMatcherSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = E48784AB1AFD7678001B5F2D /* LSDataMatcherSpec.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -195,6 +196,7 @@
A0EE631715ECDAF300009A08 /* LSHTTPRequestDiffSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = LSHTTPRequestDiffSpec.m; path = Diff/LSHTTPRequestDiffSpec.m; sourceTree = "<group>"; };
AA0A44051758E27100D2F909 /* NSURLRequest+LSHTTPRequestSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSURLRequest+LSHTTPRequestSpec.m"; path = "Hooks/NSURLRequest/NSURLRequest+LSHTTPRequestSpec.m"; sourceTree = "<group>"; };
CADACB517CAB41DD8263C667 /* libPods-NocillaTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-NocillaTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
E48784AB1AFD7678001B5F2D /* LSDataMatcherSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = LSDataMatcherSpec.m; path = Matchers/LSDataMatcherSpec.m; sourceTree = "<group>"; };
F7621264AB6B45CE84724138 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -496,6 +498,7 @@
A08EE2201728A1FC001830E6 /* Matchers */ = {
isa = PBXGroup;
children = (
E48784AB1AFD7678001B5F2D /* LSDataMatcherSpec.m */,
A08EE2231728A241001830E6 /* LSStringMatcherSpec.m */,
A02889421728CD6000288022 /* LSRegexMatcherSpec.m */,
);
Expand Down Expand Up @@ -738,6 +741,7 @@
A02889431728CD6000288022 /* LSRegexMatcherSpec.m in Sources */,
A01CEBED1760081F00637CE7 /* LSNocillaSpec.m in Sources */,
A01CEBDE175FFE3200637CE7 /* ASIHTTPRequestStubbingSpec.m in Sources */,
E48784AC1AFD7678001B5F2D /* LSDataMatcherSpec.m in Sources */,
AA0A44061758E27100D2F909 /* NSURLRequest+LSHTTPRequestSpec.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
4 changes: 2 additions & 2 deletions Nocilla/Matchers/LSDataMatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ - (instancetype)initWithData:(NSData *)data {
return self;
}

- (BOOL)matches:(NSString *)string {
return [self.data isEqualToData:[string dataUsingEncoding:NSUTF8StringEncoding]];
- (BOOL)matchesData:(NSData *)data {
return [self.data isEqualToData:data];
}

@end
2 changes: 2 additions & 0 deletions Nocilla/Matchers/LSMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

- (BOOL)matches:(NSString *)string;

- (BOOL)matchesData:(NSData *)data;

@end
4 changes: 4 additions & 0 deletions Nocilla/Matchers/LSMatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ - (BOOL)matches:(NSString *)string {
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[LSMatcher matches:] is an abstract method" userInfo:nil];
}

- (BOOL)matchesData:(NSData *)data {
return [self matches:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}

@end
2 changes: 1 addition & 1 deletion Nocilla/Stubs/LSStubRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ -(BOOL)matchesHeaders:(id<LSHTTPRequest>)request {

-(BOOL)matchesBody:(id<LSHTTPRequest>)request {
NSData *reqBody = request.body;
if (!self.body || [self.body matches:[[NSString alloc] initWithData:reqBody encoding:NSUTF8StringEncoding]]) {
if (!self.body || [self.body matchesData:reqBody]) {
return YES;
}
return NO;
Expand Down
30 changes: 30 additions & 0 deletions NocillaTests/Matchers/LSDataMatcherSpec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#import "Kiwi.h"
#import "LSDataMatcher.h"

SPEC_BEGIN(LSDataMatcherSpec)

__block LSDataMatcher *matcher = nil;
__block NSData *data = nil;

beforeEach(^{
const char bytes[] = { 0xF1, 0x00, 0xFF };
data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
matcher = [[LSDataMatcher alloc] initWithData:data];
});

context(@"when both data are equal", ^{
it(@"matches", ^{
[[theValue([matcher matchesData:[data copy]]) should] beYes];
});
});

context(@"when both data are different", ^{
it(@"does not match", ^{
const char other_bytes[] = { 0xA1, 0x00, 0xAF };
NSData *other_data = [NSData dataWithBytes:other_bytes length:sizeof(other_bytes)];

[[theValue([matcher matchesData:other_data]) should] beNo];
});
});

SPEC_END

0 comments on commit f6f85ba

Please sign in to comment.