Skip to content

Commit

Permalink
[3.1.8] Fixed crash when loading an empty fixture file.
Browse files Browse the repository at this point in the history
  • Loading branch information
AliSoftware committed Dec 9, 2014
1 parent e7eab93 commit 2a6f102
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
_You can download previous versions [here](https://github.com/AliSoftware/OHHTTPStubs/tags) and latest version [here](https://github.com/AliSoftware/OHHTTPStubs/zipball/master) (ZIP files generated by GitHub on the fly)_

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

* Use `application/json` instead of `text/json` in `README`'s example ([#75](https://github.com/AliSoftware/OHHTTPStubs/pull/75))
* Fixed an issue with empty files (when using `responseWithFileAtPath:statusCode:headers:` but the file at the specified path is empty)

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

Expand Down
2 changes: 1 addition & 1 deletion OHHTTPStubs.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "OHHTTPStubs"
s.version = "3.1.7"
s.version = "3.1.8"

s.summary = "Stubbing framework for network requests."
s.description = <<-DESC.gsub(/^ +\|/,'')
Expand Down
14 changes: 14 additions & 0 deletions OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
0959820219806B2200807DBE /* OHHTTPStubsResponse+JSON.h in Headers */ = {isa = PBXBuildFile; fileRef = 09110A721980606A00D175E4 /* OHHTTPStubsResponse+JSON.h */; settings = {ATTRIBUTES = (Public, ); }; };
099E8C1919808B14002895AA /* XCTestExpectation+OHRetroCompat.m in Sources */ = {isa = PBXBuildFile; fileRef = 099E8C1819808B14002895AA /* XCTestExpectation+OHRetroCompat.m */; };
099E8C1A19808B14002895AA /* XCTestExpectation+OHRetroCompat.m in Sources */ = {isa = PBXBuildFile; fileRef = 099E8C1819808B14002895AA /* XCTestExpectation+OHRetroCompat.m */; };
47AF337B1A3775B600158C9F /* emptyfile.json in Resources */ = {isa = PBXBuildFile; fileRef = 47AF337A1A37757B00158C9F /* emptyfile.json */; };
47AF337C1A3775B600158C9F /* emptyfile.json in Resources */ = {isa = PBXBuildFile; fileRef = 47AF337A1A37757B00158C9F /* emptyfile.json */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -140,6 +142,7 @@
095981D219806A7900807DBE /* OHHTTPStubs Mac Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "OHHTTPStubs Mac Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
099E8C1719808B14002895AA /* XCTestExpectation+OHRetroCompat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "XCTestExpectation+OHRetroCompat.h"; sourceTree = "<group>"; };
099E8C1819808B14002895AA /* XCTestExpectation+OHRetroCompat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "XCTestExpectation+OHRetroCompat.m"; sourceTree = "<group>"; };
47AF337A1A37757B00158C9F /* emptyfile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = emptyfile.json; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -228,6 +231,7 @@
09110A5A19805F4800D175E4 /* UnitTests */ = {
isa = PBXGroup;
children = (
47AF33791A37755E00158C9F /* Fixtures */,
0959811F1980647A00807DBE /* AFNetworking Source */,
095981461980663000807DBE /* Test Suites */,
09110A5B19805F4800D175E4 /* Supporting Files */,
Expand Down Expand Up @@ -333,6 +337,14 @@
path = "XCTestExpectation+Xcode5";
sourceTree = "<group>";
};
47AF33791A37755E00158C9F /* Fixtures */ = {
isa = PBXGroup;
children = (
47AF337A1A37757B00158C9F /* emptyfile.json */,
);
path = Fixtures;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -463,6 +475,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
47AF337B1A3775B600158C9F /* emptyfile.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -477,6 +490,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
47AF337C1A3775B600158C9F /* emptyfile.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ - (void)streamDataForClient:(id<NSURLProtocolClient>)client
withStubResponse:(OHHTTPStubsResponse*)stubResponse
completion:(void(^)(NSError * error))completion
{
if (stubResponse.inputStream.hasBytesAvailable && !self.stopped)
if ((stubResponse.dataSize>0) && stubResponse.inputStream.hasBytesAvailable && (!self.stopped))
{
// Compute timing data once and for all for this stub

Expand Down Expand Up @@ -472,7 +472,7 @@ - (void) streamDataForClient:(id<NSURLProtocolClient>)client
{
NSParameterAssert(timingInfo.chunkSizePerSlot > 0);

if (inputStream.hasBytesAvailable && !self.stopped)
if (inputStream.hasBytesAvailable && (!self.stopped))
{
// This is needed in case we computed a non-integer chunkSizePerSlot, to avoid cumulative errors
double cumulativeChunkSizeAfterRead = timingInfo.cumulativeChunkSize + timingInfo.chunkSizePerSlot;
Expand Down
Empty file.
57 changes: 55 additions & 2 deletions OHHTTPStubs/UnitTests/Test Suites/NilValuesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,38 @@ - (void)test_NilData
[self waitForExpectationsWithTimeout:kResponseTimeTolerence handler:nil];
}

- (void)test_EmptyData
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:[NSData data] statusCode:400 headers:nil]
requestTime:0.01 responseTime:0.01];
}];

XCTestExpectation* expectation = [self expectationWithDescription:@"Network request's completionHandler called"];

NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.iana.org/domains/example/"]];

[NSURLConnection sendAsynchronousRequest:req
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* resp, NSData* data, NSError* error)
{
XCTAssertEqual(data.length, (NSUInteger)0, @"Data should be empty");

[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:kResponseTimeTolerence handler:nil];
}

- (void)test_NilPath
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithFileAtPath:nil statusCode:501 headers:nil];
return [[OHHTTPStubsResponse responseWithFileAtPath:nil statusCode:501 headers:nil]
requestTime:0.01 responseTime:0.01];
}];

XCTestExpectation* expectation = [self expectationWithDescription:@"Network request's completionHandler called"];
Expand All @@ -90,7 +116,34 @@ - (void)test_InvalidPath
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithFileAtPath:@"-invalid-path-" statusCode:500 headers:nil];
return [[OHHTTPStubsResponse responseWithFileAtPath:@"-invalid-path-" statusCode:500 headers:nil]
requestTime:0.01 responseTime:0.01];
}];

XCTestExpectation* expectation = [self expectationWithDescription:@"Network request's completionHandler called"];

NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.iana.org/domains/example/"]];

[NSURLConnection sendAsynchronousRequest:req
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* resp, NSData* data, NSError* error)
{
XCTAssertEqual(data.length, (NSUInteger)0, @"Data should be empty");

[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:kResponseTimeTolerence handler:nil];
}

- (void)test_EmptyFile
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
NSString* emptyFile = OHPathForFileInBundle(@"emptyfile.json", nil);
return [[OHHTTPStubsResponse responseWithFileAtPath:emptyFile statusCode:500 headers:nil]
requestTime:0.01 responseTime:0.01];
}];

XCTestExpectation* expectation = [self expectationWithDescription:@"Network request's completionHandler called"];
Expand Down

0 comments on commit 2a6f102

Please sign in to comment.