From de6830ee387c0fe34e44362abf41483ab1707ac6 Mon Sep 17 00:00:00 2001 From: Satoshi Takano Date: Sun, 15 Jan 2017 17:40:31 +0900 Subject: [PATCH] Add test for the empty OGP. --- OpenGraph.xcodeproj/project.pbxproj | 6 ++- Tests/OpenGraphTests.swift | 57 ++++++++++++++++++----------- Tests/empty_ogp.html | 7 ++++ 3 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 Tests/empty_ogp.html diff --git a/OpenGraph.xcodeproj/project.pbxproj b/OpenGraph.xcodeproj/project.pbxproj index 788588f..8f1ddb8 100644 --- a/OpenGraph.xcodeproj/project.pbxproj +++ b/OpenGraph.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 7B24FB491D3B27E5005275B0 /* OpenGraphParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B24FB441D3B27E5005275B0 /* OpenGraphParser.swift */; }; 7B24FB4A1D3B27E5005275B0 /* OpenGraphResponseError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B24FB451D3B27E5005275B0 /* OpenGraphResponseError.swift */; }; 7B24FB531D3B2DD4005275B0 /* example.com.html in Resources */ = {isa = PBXBuildFile; fileRef = 7B24FB521D3B2DD4005275B0 /* example.com.html */; }; + CBEA99A61E2B65F40066452C /* empty_ogp.html in Resources */ = {isa = PBXBuildFile; fileRef = CBEA99A41E2B63980066452C /* empty_ogp.html */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -45,6 +46,7 @@ 7B24FB441D3B27E5005275B0 /* OpenGraphParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGraphParser.swift; sourceTree = ""; }; 7B24FB451D3B27E5005275B0 /* OpenGraphResponseError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGraphResponseError.swift; sourceTree = ""; }; 7B24FB521D3B2DD4005275B0 /* example.com.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = example.com.html; sourceTree = ""; }; + CBEA99A41E2B63980066452C /* empty_ogp.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = empty_ogp.html; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,6 +106,7 @@ 7B24FB521D3B2DD4005275B0 /* example.com.html */, 6A7310FC1E279C7E00CE1756 /* example2.com.html */, 6A7310FB1E279C7E00CE1756 /* example3.com.html */, + CBEA99A41E2B63980066452C /* empty_ogp.html */, 7B24FB261D3B2583005275B0 /* Info.plist */, 7B24FB3D1D3B26C4005275B0 /* OpenGraphTests.swift */, ); @@ -205,8 +208,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 6A7310FD1E279C7E00CE1756 /* example3.com.html in Resources */, - 6A7310FE1E279C7E00CE1756 /* example2.com.html in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -214,6 +215,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + CBEA99A61E2B65F40066452C /* empty_ogp.html in Resources */, 6A7310FF1E279D5D00CE1756 /* example3.com.html in Resources */, 6A7311001E279D5D00CE1756 /* example2.com.html in Resources */, 7B24FB531D3B2DD4005275B0 /* example.com.html in Resources */, diff --git a/Tests/OpenGraphTests.swift b/Tests/OpenGraphTests.swift index 117ccbf..b5dd395 100644 --- a/Tests/OpenGraphTests.swift +++ b/Tests/OpenGraphTests.swift @@ -15,15 +15,19 @@ class OpenGraphTests: XCTestCase { OHHTTPStubs.removeAllStubs() } - func testCustomHeader() { - let responseArrived = expectation(description: "response of async request has arrived") - + func setupStub(htmlFileName: String) { OHHTTPStubs.stubRequests(passingTest: { request -> Bool in return true }) { request -> OHHTTPStubsResponse in - let path = Bundle(for: type(of: self)).path(forResource: "example.com", ofType: "html") + let path = Bundle(for: type(of: self)).path(forResource: htmlFileName, ofType: "html") return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) } + } + + func testCustomHeader() { + let responseArrived = expectation(description: "response of async request has arrived") + + setupStub(htmlFileName: "example.com") let url = URL(string: "https://www.example.com")! var og: OpenGraph! @@ -48,12 +52,7 @@ class OpenGraphTests: XCTestCase { func testFetching() { let responseArrived = expectation(description: "response of async request has arrived") - OHHTTPStubs.stubRequests(passingTest: { request -> Bool in - return true - }) { request -> OHHTTPStubsResponse in - let path = Bundle(for: type(of: self)).path(forResource: "example.com", ofType: "html") - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) - } + setupStub(htmlFileName: "example.com") let url = URL(string: "https://www.example.com")! var og: OpenGraph! @@ -74,16 +73,35 @@ class OpenGraphTests: XCTestCase { } } + func testEmptyOGP() { + let responseArrived = expectation(description: "response of async request has arrived") + + setupStub(htmlFileName: "empty_ogp") + + let url = URL(string: "https://www.example2.com")! + var og: OpenGraph! + var error: Error? + OpenGraph.fetch(url: url) { _og, _error in + og = _og + error = _error + responseArrived.fulfill() + } + + waitForExpectations(timeout: 10) { _ in + XCTAssert(og[.title] == nil) + XCTAssert(og[.type] == nil) + XCTAssert(og[.url] == nil) + XCTAssert(og[.image] == nil) + + XCTAssert(error == nil) + } + } + // Detect og:xxx also when order of attributes are reversed. func testFetching2() { let responseArrived = expectation(description: "response of async request has arrived") - OHHTTPStubs.stubRequests(passingTest: { request -> Bool in - return true - }) { request -> OHHTTPStubsResponse in - let path = Bundle(for: type(of: self)).path(forResource: "example2.com", ofType: "html") - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) - } + setupStub(htmlFileName: "example2.com") let url = URL(string: "https://www.example2.com")! var og: OpenGraph! @@ -108,12 +126,7 @@ class OpenGraphTests: XCTestCase { func testFetching3() { let responseArrived = expectation(description: "response of async request has arrived") - OHHTTPStubs.stubRequests(passingTest: { request -> Bool in - return true - }) { request -> OHHTTPStubsResponse in - let path = Bundle(for: type(of: self)).path(forResource: "example3.com", ofType: "html") - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) - } + setupStub(htmlFileName: "example3.com") let url = URL(string: "https://www.example3.com")! var og: OpenGraph! diff --git a/Tests/empty_ogp.html b/Tests/empty_ogp.html new file mode 100644 index 0000000..fc2f3cd --- /dev/null +++ b/Tests/empty_ogp.html @@ -0,0 +1,7 @@ + + + + +

Hello, world.

+ +