Skip to content

Commit

Permalink
Merge #172 (fixes #170)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliSoftware committed May 11, 2016
2 parents 75a8cd5 + 4bdd8dd commit ec65530
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Declare in the project settings that the library contains swift code.
[@rodericj](https://github.com/rodericj), [#173](https://github.com/AliSoftware/OHHTTPStubs/pull/173)

* Adjusted parsing of Mocktail files to allow headers to start on line 4.
[@Ashton-W](https://github.com/Ashton-W), [#172](https://github.com/AliSoftware/OHHTTPStubs/pull/172)

## [5.0.0](https://github.com/AliSoftware/OHHTTPStubs/releases/tag/5.0.0)

* Added `pathStartsWith(_:)` to the `Swift` helpers.
Expand Down
30 changes: 30 additions & 0 deletions OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
221C34A81B0CCF9D00FCA8FF /* empty.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 221C34A61B0CCF9D00FCA8FF /* empty.bundle */; };
221C34A91B0CCFF200FCA8FF /* OHPathHelpersTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 221C34A41B0CCF0600FCA8FF /* OHPathHelpersTests.m */; };
3EB635BCF186B952750E2AEE /* libPods-OHHTTPStubs iOS Fmk Tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FC7C82D3D508702D3B282621 /* libPods-OHHTTPStubs iOS Fmk Tests.a */; };
46C732921CDAC2DB00CDBCB5 /* login_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C7328F1CDAC2DB00CDBCB5 /* login_headers.tail */; };
46C732931CDAC2DC00CDBCB5 /* login_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C7328F1CDAC2DB00CDBCB5 /* login_headers.tail */; };
46C732941CDAC2DC00CDBCB5 /* login_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C7328F1CDAC2DB00CDBCB5 /* login_headers.tail */; };
46C732951CDAC2DC00CDBCB5 /* login_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C7328F1CDAC2DB00CDBCB5 /* login_headers.tail */; };
46C732961CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732901CDAC2DB00CDBCB5 /* login_content_type_and_headers.tail */; };
46C732971CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732901CDAC2DB00CDBCB5 /* login_content_type_and_headers.tail */; };
46C732981CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732901CDAC2DB00CDBCB5 /* login_content_type_and_headers.tail */; };
46C732991CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732901CDAC2DB00CDBCB5 /* login_content_type_and_headers.tail */; };
46C7329A1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732911CDAC2DB00CDBCB5 /* login_content_type.tail */; };
46C7329B1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732911CDAC2DB00CDBCB5 /* login_content_type.tail */; };
46C7329C1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732911CDAC2DB00CDBCB5 /* login_content_type.tail */; };
46C7329D1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */ = {isa = PBXBuildFile; fileRef = 46C732911CDAC2DB00CDBCB5 /* login_content_type.tail */; };
47AF337B1A3775B600158C9F /* emptyfile.json in Resources */ = {isa = PBXBuildFile; fileRef = 47AF337A1A37757B00158C9F /* emptyfile.json */; };
47AF337C1A3775B600158C9F /* emptyfile.json in Resources */ = {isa = PBXBuildFile; fileRef = 47AF337A1A37757B00158C9F /* emptyfile.json */; };
725CD9B41A9EB6F600F84C8B /* OHHTTPStubs.m in Sources */ = {isa = PBXBuildFile; fileRef = 09110A6B1980605A00D175E4 /* OHHTTPStubs.m */; };
Expand Down Expand Up @@ -237,6 +249,9 @@
221C34A41B0CCF0600FCA8FF /* OHPathHelpersTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OHPathHelpersTests.m; sourceTree = "<group>"; };
221C34A61B0CCF9D00FCA8FF /* empty.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = empty.bundle; sourceTree = "<group>"; };
446DA13BA37A8466FF4F9D35 /* libPods-OHHTTPStubs iOS Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-OHHTTPStubs iOS Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
46C7328F1CDAC2DB00CDBCB5 /* login_headers.tail */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = login_headers.tail; path = UnitTests/login_headers.tail; sourceTree = SOURCE_ROOT; };
46C732901CDAC2DB00CDBCB5 /* login_content_type_and_headers.tail */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = login_content_type_and_headers.tail; path = UnitTests/login_content_type_and_headers.tail; sourceTree = SOURCE_ROOT; };
46C732911CDAC2DB00CDBCB5 /* login_content_type.tail */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = login_content_type.tail; path = UnitTests/login_content_type.tail; sourceTree = SOURCE_ROOT; };
47AF337A1A37757B00158C9F /* emptyfile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = emptyfile.json; sourceTree = "<group>"; };
725CD99B1A9EB65100F84C8B /* OHHTTPStubs.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OHHTTPStubs.framework; sourceTree = BUILT_PRODUCTS_DIR; };
78215AC3F712A93089D77E60 /* Pods-OHHTTPStubs iOS Fmk Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OHHTTPStubs iOS Fmk Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-OHHTTPStubs iOS Fmk Tests/Pods-OHHTTPStubs iOS Fmk Tests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -522,6 +537,9 @@
47AF33791A37755E00158C9F /* Fixtures */ = {
isa = PBXGroup;
children = (
46C7328F1CDAC2DB00CDBCB5 /* login_headers.tail */,
46C732901CDAC2DB00CDBCB5 /* login_content_type_and_headers.tail */,
46C732911CDAC2DB00CDBCB5 /* login_content_type.tail */,
221C34A61B0CCF9D00FCA8FF /* empty.bundle */,
47AF337A1A37757B00158C9F /* emptyfile.json */,
1D0F8E7F1B6E31A70049A7D2 /* login.tail */,
Expand Down Expand Up @@ -804,21 +822,27 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
46C732921CDAC2DB00CDBCB5 /* login_headers.tail in Resources */,
46C732961CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */,
47AF337B1A3775B600158C9F /* emptyfile.json in Resources */,
221C34A71B0CCF9D00FCA8FF /* empty.bundle in Resources */,
1D0F8E821B6E31B00049A7D2 /* MocktailFolder in Resources */,
1D0F8E801B6E31A70049A7D2 /* login.tail in Resources */,
46C7329A1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
093442F01B80EC4A00A91535 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
46C732931CDAC2DC00CDBCB5 /* login_headers.tail in Resources */,
46C732971CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */,
093442F11B80EC4A00A91535 /* emptyfile.json in Resources */,
093442F21B80EC4A00A91535 /* empty.bundle in Resources */,
093442F31B80EC4A00A91535 /* MocktailFolder in Resources */,
093442F41B80EC4A00A91535 /* login.tail in Resources */,
46C7329B1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -833,10 +857,13 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
46C732941CDAC2DC00CDBCB5 /* login_headers.tail in Resources */,
46C732981CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */,
094906D91B7F523500B047DA /* MocktailFolder in Resources */,
47AF337C1A3775B600158C9F /* emptyfile.json in Resources */,
221C34A81B0CCF9D00FCA8FF /* empty.bundle in Resources */,
094906DA1B7F523900B047DA /* login.tail in Resources */,
46C7329C1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -851,10 +878,13 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
46C732951CDAC2DC00CDBCB5 /* login_headers.tail in Resources */,
46C732991CDAC2DC00CDBCB5 /* login_content_type_and_headers.tail in Resources */,
EA9D27241BE15CA00078CAA0 /* empty.bundle in Resources */,
EA9D27251BE15CA00078CAA0 /* emptyfile.json in Resources */,
EA9D27261BE15CA00078CAA0 /* login.tail in Resources */,
EA9D27271BE15CA00078CAA0 /* MocktailFolder in Resources */,
46C7329D1CDAC2DC00CDBCB5 /* login_content_type.tail in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
26 changes: 24 additions & 2 deletions OHHTTPStubs/Sources/Mocktail/OHHTTPStubs+Mocktail.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ +(NSArray *)stubRequestsUsingMocktailsAtPath:(NSString *)path inBundle:(nullable

NSMutableDictionary *headers = @{@"Content-Type":lines[3]}.mutableCopy;

// From line 5 to '\n\n', expect HTTP response headers.
// From line 4 to '\n\n', expect HTTP response headers.
NSRegularExpression *headerPattern = [NSRegularExpression regularExpressionWithPattern:@"^([^:]+):\\s+(.*)" options:0 error:&bError];
if (bError)
{
Expand All @@ -175,10 +175,32 @@ +(NSArray *)stubRequestsUsingMocktailsAtPath:(NSString *)path inBundle:(nullable
return nil;
}

for (NSUInteger line = 4; line < lines.count; line ++) {

// Allow bare Content-Type header on line 4 before named HTTP response headers
NSRegularExpression *bareContentTypePattern = [NSRegularExpression regularExpressionWithPattern:@"^([^:]+)+$" options:0 error:&bError];
if (bError)
{
if (error)
{
*error = [NSError errorWithDomain:MocktailErrorDomain code:OHHTTPStubsMocktailErrorInternalError userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Internal error while stubbing file '%@'.", fileURL.absoluteString]}];
}
return nil;
}

for (NSUInteger line = 3; line < lines.count; line ++) {
NSString *headerLine = lines[line];
NSTextCheckingResult *match = [headerPattern firstMatchInString:headerLine options:0 range:NSMakeRange(0, headerLine.length)];

if (line == 3 && !match) {
match = [bareContentTypePattern firstMatchInString:headerLine options:0 range:NSMakeRange(0, headerLine.length)];
if (match) {
NSString *key = @"Content-Type";
NSString *value = [headerLine substringWithRange:[match rangeAtIndex:1]];
headers[key] = value;
continue;
}
}

if (match)
{
NSString *key = [headerLine substringWithRange:[match rangeAtIndex:1]];
Expand Down
46 changes: 40 additions & 6 deletions OHHTTPStubs/UnitTests/Test Suites/MocktailTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (void)tearDown
self.session = nil;
}

- (void)testMoctTailLoginSuccess
- (void)testMocktailLoginSuccess
{
NSError *error = nil;
NSBundle *bundle = [NSBundle bundleForClass:self.class];
Expand All @@ -75,7 +75,36 @@ - (void)testMocktailsAtFolder
[self runGetCards];
}

- (void)runLogin
- (void)testMocktailHeaders
{
NSError *error = nil;
NSBundle *bundle = [NSBundle bundleForClass:self.class];
[OHHTTPStubs stubRequestsUsingMocktailNamed:@"login_headers" inBundle:bundle error: &error];
XCTAssertNil(error, @"Error while stubbing 'login_headers.tail':%@", [error localizedDescription]);
NSHTTPURLResponse *response = [self runLogin];
XCTAssertEqualObjects(response.allHeaderFields[@"Connection"], @"Close");
}

- (void)testMocktailContentType
{
NSError *error = nil;
NSBundle *bundle = [NSBundle bundleForClass:self.class];
[OHHTTPStubs stubRequestsUsingMocktailNamed:@"login_content_type" inBundle:bundle error: &error];
XCTAssertNil(error, @"Error while stubbing 'login_content_type.tail':%@", [error localizedDescription]);
[self runLogin];
}

- (void)testMocktailContentTypeAndHeaders
{
NSError *error = nil;
NSBundle *bundle = [NSBundle bundleForClass:self.class];
[OHHTTPStubs stubRequestsUsingMocktailNamed:@"login_content_type_and_headers" inBundle:bundle error: &error];
XCTAssertNil(error, @"Error while stubbing 'login_content_type_and_headers.tail':%@", [error localizedDescription]);
NSHTTPURLResponse *response = [self runLogin];
XCTAssertEqualObjects(response.allHeaderFields[@"Connection"], @"Close");
}

- (NSHTTPURLResponse *)runLogin
{
NSURL *url = [NSURL URLWithString:@"http://happywebservice.com/users"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
Expand All @@ -93,10 +122,10 @@ - (void)runLogin

XCTestExpectation* expectation = [self expectationWithDescription:@"NSURLSessionDataTask completed"];

__block NSHTTPURLResponse *capturedResponse;
NSURLSessionDataTask *postDataTask = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

if ([response isKindOfClass:[NSHTTPURLResponse class]]) capturedResponse = (id)response;
XCTAssertNil(error, @"Error while logging in.");

NSDictionary *json = nil;
if(!error && [@"application/json" isEqual:response.MIMEType])
{
Expand All @@ -113,9 +142,11 @@ - (void)runLogin
[postDataTask resume];

[self waitForExpectationsWithTimeout:10 handler:nil];

return capturedResponse;
}

- (void)runGetCards
- (NSHTTPURLResponse *)runGetCards
{
NSURL *url = [NSURL URLWithString:@"http://happywebservice.com/cards"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
Expand All @@ -129,8 +160,9 @@ - (void)runGetCards

XCTestExpectation* expectation = [self expectationWithDescription:@"NSURLSessionDataTask completed"];

__block NSHTTPURLResponse *capturedResponse;
NSURLSessionDataTask *getDataTask = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

if ([response isKindOfClass:[NSHTTPURLResponse class]]) capturedResponse = (id)response;
XCTAssertNil(error, @"Error while getting cards.");

NSArray *json = nil;
Expand All @@ -149,6 +181,8 @@ - (void)runGetCards
[getDataTask resume];

[self waitForExpectationsWithTimeout:10 handler:nil];

return capturedResponse;
}

@end
10 changes: 10 additions & 0 deletions OHHTTPStubs/UnitTests/login_content_type.tail
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GET|POST
.*/users
200
application/json

{
"user_id": "happyuser1",
"user_token": "happytoken",
"status": "SUCCESS"
}
11 changes: 11 additions & 0 deletions OHHTTPStubs/UnitTests/login_content_type_and_headers.tail
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GET|POST
.*/users
200
application/json
Connection: Close

{
"user_id": "happyuser1",
"user_token": "happytoken",
"status": "SUCCESS"
}
11 changes: 11 additions & 0 deletions OHHTTPStubs/UnitTests/login_headers.tail
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GET|POST
.*/users
200
Content-Type: application/json
Connection: Close

{
"user_id": "happyuser1",
"user_token": "happytoken",
"status": "SUCCESS"
}

0 comments on commit ec65530

Please sign in to comment.