From 9a9a5dfa920afa3bc129c7549f2a015bb29273b2 Mon Sep 17 00:00:00 2001 From: mt-hodaka Date: Fri, 13 Jan 2017 12:50:40 +0900 Subject: [PATCH] Add 2 test patterns - Detect og:xxx also when order of attributes are reversed. - When the meta tag contains other attributes. --- OpenGraph.xcodeproj/project.pbxproj | 10 +++++ Tests/OpenGraphTests.swift | 60 +++++++++++++++++++++++++++++ Tests/example2.com.html | 12 ++++++ Tests/example3.com.html | 12 ++++++ 4 files changed, 94 insertions(+) create mode 100644 Tests/example2.com.html create mode 100644 Tests/example3.com.html diff --git a/OpenGraph.xcodeproj/project.pbxproj b/OpenGraph.xcodeproj/project.pbxproj index 66e70c3..788588f 100644 --- a/OpenGraph.xcodeproj/project.pbxproj +++ b/OpenGraph.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 6A7310FF1E279D5D00CE1756 /* example3.com.html in Resources */ = {isa = PBXBuildFile; fileRef = 6A7310FB1E279C7E00CE1756 /* example3.com.html */; }; + 6A7311001E279D5D00CE1756 /* example2.com.html in Resources */ = {isa = PBXBuildFile; fileRef = 6A7310FC1E279C7E00CE1756 /* example2.com.html */; }; 7B24FB191D3B2583005275B0 /* OpenGraph.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B24FB181D3B2583005275B0 /* OpenGraph.h */; settings = {ATTRIBUTES = (Public, ); }; }; 7B24FB201D3B2583005275B0 /* OpenGraph.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B24FB151D3B2583005275B0 /* OpenGraph.framework */; }; 7B24FB3E1D3B26C4005275B0 /* OpenGraphTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B24FB3D1D3B26C4005275B0 /* OpenGraphTests.swift */; }; @@ -29,6 +31,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 6A7310FB1E279C7E00CE1756 /* example3.com.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = example3.com.html; sourceTree = ""; }; + 6A7310FC1E279C7E00CE1756 /* example2.com.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = example2.com.html; sourceTree = ""; }; 7B24FB151D3B2583005275B0 /* OpenGraph.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OpenGraph.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7B24FB181D3B2583005275B0 /* OpenGraph.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OpenGraph.h; sourceTree = ""; }; 7B24FB1A1D3B2583005275B0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -98,6 +102,8 @@ isa = PBXGroup; children = ( 7B24FB521D3B2DD4005275B0 /* example.com.html */, + 6A7310FC1E279C7E00CE1756 /* example2.com.html */, + 6A7310FB1E279C7E00CE1756 /* example3.com.html */, 7B24FB261D3B2583005275B0 /* Info.plist */, 7B24FB3D1D3B26C4005275B0 /* OpenGraphTests.swift */, ); @@ -199,6 +205,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6A7310FD1E279C7E00CE1756 /* example3.com.html in Resources */, + 6A7310FE1E279C7E00CE1756 /* example2.com.html in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -206,6 +214,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6A7310FF1E279D5D00CE1756 /* example3.com.html in Resources */, + 6A7311001E279D5D00CE1756 /* example2.com.html in Resources */, 7B24FB531D3B2DD4005275B0 /* example.com.html in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Tests/OpenGraphTests.swift b/Tests/OpenGraphTests.swift index 6ec73f0..117ccbf 100644 --- a/Tests/OpenGraphTests.swift +++ b/Tests/OpenGraphTests.swift @@ -74,6 +74,66 @@ class OpenGraphTests: XCTestCase { } } + // 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) + } + + 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] == "example2.com title") + XCTAssert(og[.type] == "website2") + XCTAssert(og[.url] == "https://www.example2.com") + XCTAssert(og[.image] == "https://www.example2.com/images/example2.png") + + XCTAssert(error == nil) + } + } + + // When the meta tag contains other attributes. + 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) + } + + let url = URL(string: "https://www.example3.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] == "example3.com title") + XCTAssert(og[.type] == "website3") + XCTAssert(og[.url] == "https://www.example3.com") + XCTAssert(og[.image] == "https://www.example3.com/images/example3.png") + + XCTAssert(error == nil) + } + } + func testHTTPResponseError() { let responseArrived = expectation(description: "response of async request has arrived") diff --git a/Tests/example2.com.html b/Tests/example2.com.html new file mode 100644 index 0000000..d5d4660 --- /dev/null +++ b/Tests/example2.com.html @@ -0,0 +1,12 @@ + + + + + + + + + +

Hello, world.

+ + diff --git a/Tests/example3.com.html b/Tests/example3.com.html new file mode 100644 index 0000000..7fbbe85 --- /dev/null +++ b/Tests/example3.com.html @@ -0,0 +1,12 @@ + + + + + + + + + +

Hello, world.

+ +