From 2a6f102178dc696414d6d7cfcc3bc5c94db45b70 Mon Sep 17 00:00:00 2001 From: Olivier Halligon Date: Tue, 9 Dec 2014 19:32:28 +0100 Subject: [PATCH] [3.1.8] Fixed crash when loading an empty fixture file. --- CHANGELOG.md | 3 +- OHHTTPStubs.podspec | 2 +- .../OHHTTPStubs.xcodeproj/project.pbxproj | 14 +++++ OHHTTPStubs/Sources/OHHTTPStubs.m | 4 +- OHHTTPStubs/UnitTests/Fixtures/emptyfile.json | 0 .../UnitTests/Test Suites/NilValuesTests.m | 57 ++++++++++++++++++- 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 OHHTTPStubs/UnitTests/Fixtures/emptyfile.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f078e08..804cb2d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/OHHTTPStubs.podspec b/OHHTTPStubs.podspec index 88fef9cb..39841871 100644 --- a/OHHTTPStubs.podspec +++ b/OHHTTPStubs.podspec @@ -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(/^ +\|/,'') diff --git a/OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj b/OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj index 30d5deed..9a95ed44 100644 --- a/OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj +++ b/OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj @@ -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 */ @@ -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 = ""; }; 099E8C1819808B14002895AA /* XCTestExpectation+OHRetroCompat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "XCTestExpectation+OHRetroCompat.m"; sourceTree = ""; }; + 47AF337A1A37757B00158C9F /* emptyfile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = emptyfile.json; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -228,6 +231,7 @@ 09110A5A19805F4800D175E4 /* UnitTests */ = { isa = PBXGroup; children = ( + 47AF33791A37755E00158C9F /* Fixtures */, 0959811F1980647A00807DBE /* AFNetworking Source */, 095981461980663000807DBE /* Test Suites */, 09110A5B19805F4800D175E4 /* Supporting Files */, @@ -333,6 +337,14 @@ path = "XCTestExpectation+Xcode5"; sourceTree = ""; }; + 47AF33791A37755E00158C9F /* Fixtures */ = { + isa = PBXGroup; + children = ( + 47AF337A1A37757B00158C9F /* emptyfile.json */, + ); + path = Fixtures; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -463,6 +475,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 47AF337B1A3775B600158C9F /* emptyfile.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -477,6 +490,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 47AF337C1A3775B600158C9F /* emptyfile.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/OHHTTPStubs/Sources/OHHTTPStubs.m b/OHHTTPStubs/Sources/OHHTTPStubs.m index 47c94eee..5889ffc2 100644 --- a/OHHTTPStubs/Sources/OHHTTPStubs.m +++ b/OHHTTPStubs/Sources/OHHTTPStubs.m @@ -425,7 +425,7 @@ - (void)streamDataForClient:(id)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 @@ -472,7 +472,7 @@ - (void) streamDataForClient:(id)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; diff --git a/OHHTTPStubs/UnitTests/Fixtures/emptyfile.json b/OHHTTPStubs/UnitTests/Fixtures/emptyfile.json new file mode 100644 index 00000000..e69de29b diff --git a/OHHTTPStubs/UnitTests/Test Suites/NilValuesTests.m b/OHHTTPStubs/UnitTests/Test Suites/NilValuesTests.m index 1e50e417..03d245ac 100644 --- a/OHHTTPStubs/UnitTests/Test Suites/NilValuesTests.m +++ b/OHHTTPStubs/UnitTests/Test Suites/NilValuesTests.m @@ -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"]; @@ -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"];