Skip to content

Commit

Permalink
Add test for the empty OGP.
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-takano committed Jan 15, 2017
1 parent 24e9986 commit de6830e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
6 changes: 4 additions & 2 deletions OpenGraph.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -45,6 +46,7 @@
7B24FB441D3B27E5005275B0 /* OpenGraphParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGraphParser.swift; sourceTree = "<group>"; };
7B24FB451D3B27E5005275B0 /* OpenGraphResponseError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGraphResponseError.swift; sourceTree = "<group>"; };
7B24FB521D3B2DD4005275B0 /* example.com.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = example.com.html; sourceTree = "<group>"; };
CBEA99A41E2B63980066452C /* empty_ogp.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = empty_ogp.html; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -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 */,
);
Expand Down Expand Up @@ -205,15 +208,14 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6A7310FD1E279C7E00CE1756 /* example3.com.html in Resources */,
6A7310FE1E279C7E00CE1756 /* example2.com.html in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
7B24FB1D1D3B2583005275B0 /* Resources */ = {
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 */,
Expand Down
57 changes: 35 additions & 22 deletions Tests/OpenGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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!
Expand All @@ -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!
Expand All @@ -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!
Expand Down
7 changes: 7 additions & 0 deletions Tests/empty_ogp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<p>Hello, world.</p>
</body>
</html>

0 comments on commit de6830e

Please sign in to comment.