Skip to content

Commit

Permalink
Merge pull request #85 from pelias/httpAdditionalHeaders
Browse files Browse the repository at this point in the history
Add additional http header parsing to the manager
  • Loading branch information
sarahsnow1 authored Sep 15, 2017
2 parents fa70c93 + 3591e46 commit 83674eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
18 changes: 18 additions & 0 deletions PeliasTests/PeliasSearchManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PeliasSearchManagerTests: XCTestCase {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
testOperationQueue.isSuspended = true
PeliasSearchManager.sharedInstance.operationQueue.isSuspended = true
PeliasSearchManager.sharedInstance.autocompleteTimeDelay = 0.0
}

Expand Down Expand Up @@ -100,4 +101,21 @@ class PeliasSearchManagerTests: XCTestCase {
XCTAssertEqual(operation.session.configuration, sessionConfig)
}

func testAddingHttpHeaders() {
let testHeaders = ["Hello" : "Hi"]
PeliasSearchManager.sharedInstance.additionalHttpHeaders = testHeaders

let point = GeoPoint.init(latitude: 40.0, longitude: 70.0)
let config = PeliasAutocompleteConfig.init(searchText: "test", focusPoint: point) { (response) in
//
}
let testOp = PeliasSearchManager.sharedInstance.autocompleteQuery(config)
XCTAssertNotNil(testOp.sessionConfig, "Session configuration not created for additional headers")
guard let sessionheaders = testOp.sessionConfig?.httpAdditionalHeaders else {
XCTFail("Headers dictionary not created")
return
}
XCTAssertEqual(sessionheaders["Hello"] as? String, testHeaders["Hello"])
}

}
14 changes: 12 additions & 2 deletions Sources/Core/PeliasSearchManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class PeliasSearchManager {

//! Singleton access
public static let sharedInstance = PeliasSearchManager()
fileprivate let operationQueue = OperationQueue()
internal let operationQueue = OperationQueue()
fileprivate let autocompleteOperationQueue = OperationQueue()
fileprivate var autocompleteQueryTimer: Timer?
internal var queuedAutocompleteOp: PeliasOperation?
Expand All @@ -23,6 +23,8 @@ public final class PeliasSearchManager {
public var baseUrl: URL
/// The query items that should be applied to every request (such as an api key).
public var urlQueryItems: [URLQueryItem]?
// Additional HTTP Headers to append to outbound requests
public var additionalHttpHeaders: [String:String]?

fileprivate init() {
operationQueue.maxConcurrentOperationCount = 4
Expand Down Expand Up @@ -73,8 +75,16 @@ public final class PeliasSearchManager {
configData.appendQueryItem(queryItem.name, value: queryItem.value)
}
}

let operation = PeliasOperation(config: configData)
if let headers = additionalHttpHeaders {
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = headers
operation.sessionConfig = config
}

//Build a operation
return PeliasOperation(config: configData)
return operation
}

fileprivate func executeOperation(_ config: APIConfigData) -> PeliasOperation {
Expand Down

0 comments on commit 83674eb

Please sign in to comment.