Skip to content

Commit

Permalink
Merge pull request satoshi-takano#22 from satoshi-takano/add_customiz…
Browse files Browse the repository at this point in the history
…e_header_api

enhance customize header API
  • Loading branch information
satoshi-takano authored Jan 5, 2017
2 parents 65739ca + cf108bb commit 2c510f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
9 changes: 4 additions & 5 deletions OpenGraph/OpenGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public struct OpenGraph {
task.resume()
}

public static func fetch(url: URL,header:[String:String], completion: @escaping (OpenGraph?, Error?) -> Void) {
public static func fetch(url: URL, headers: [String:String], completion: @escaping (OpenGraph?, Error?) -> Void) {

var mutableURLRequest = URLRequest(url: url)
mutableURLRequest.setValue("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", forHTTPHeaderField: "User-Agent")
for hkey in header.keys {
let value:String! = header[hkey]
for hkey in headers.keys {
let value:String! = headers[hkey]
if value != nil {
mutableURLRequest.setValue(value, forHTTPHeaderField: hkey)
}
Expand All @@ -35,7 +34,7 @@ public struct OpenGraph {
task.resume()
}

internal static func handleFetchResult(data:Data?,response:URLResponse?,error:Error?,callback: @escaping (OpenGraph?, Error?) -> Void){
private static func handleFetchResult(data: Data?, response: URLResponse?, error: Error?, callback: @escaping (OpenGraph?, Error?) -> Void){
switch (data, response, error) {
case (_, _, let error?):
callback(nil, error)
Expand Down
30 changes: 30 additions & 0 deletions Tests/OpenGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ class OpenGraphTests: XCTestCase {
OHHTTPStubs.removeAllStubs()
}

func testCustomHeader() {
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)
}

let url = URL(string: "https://www.example.com")!
var og: OpenGraph!
var error: Error?
let headers = ["User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"]
OpenGraph.fetch(url: url, headers: headers) { (_og, _error) in
og = _og
error = _error
responseArrived.fulfill()
}

waitForExpectations(timeout: 10) { _ in
XCTAssert(og[.title] == "example.com title")
XCTAssert(og[.type] == "website")
XCTAssert(og[.url] == "https://www.example.com")
XCTAssert(og[.image] == "https://www.example.com/images/example.png")

XCTAssert(error == nil)
}
}

func testFetching() {
let responseArrived = expectation(description: "response of async request has arrived")

Expand Down

0 comments on commit 2c510f9

Please sign in to comment.