-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d475a79
commit 9fbb49a
Showing
3 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,16 @@ import OHHTTPStubs | |
import CoreLocation | ||
@testable import MapboxGeocoder | ||
|
||
class BatchForwardGeocodingTests: XCTestCase { | ||
class BatchGeocodingTests: XCTestCase { | ||
override func tearDown() { | ||
OHHTTPStubs.removeAllStubs() | ||
super.tearDown() | ||
} | ||
|
||
/** | ||
Forward batch geocoding tests | ||
*/ | ||
|
||
func testValidForwardSingleBatchGeocode() { | ||
let expectation = self.expectation(description: "forward batch geocode with single query should return results") | ||
|
||
|
@@ -85,7 +89,7 @@ class BatchForwardGeocodingTests: XCTestCase { | |
|
||
func testNoResultsForwardSingleBatchGeocode() { | ||
_ = stub(condition: isHost("api.mapbox.com") | ||
&& isPath("/geocoding/v5/mapbox.places-permanent/#[email protected]") // Text does not need to be encoded | ||
&& isPath("/geocoding/v5/mapbox.places-permanent/#[email protected]") | ||
&& containsQueryParams(["access_token": BogusToken])) { _ in | ||
let path = Bundle(for: type(of: self)).path(forResource: "permanent_forward_single_no_results", ofType: "json") | ||
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/vnd.geo+json"]) | ||
|
@@ -140,4 +144,87 @@ class BatchForwardGeocodingTests: XCTestCase { | |
XCTAssertEqual(task.state, URLSessionTask.State.completed) | ||
} | ||
} | ||
|
||
/** | ||
General batch geocoding tests - invalid queries, invalid tokens, token scope checking, etc. | ||
*/ | ||
|
||
func testInvalidBatchGeocode() { | ||
_ = stub(condition: isHost("api.mapbox.com") | ||
&& isPath("/geocoding/v5/mapbox.places-permanent////.json") | ||
&& containsQueryParams(["access_token": BogusToken])) { _ in | ||
let path = Bundle(for: type(of: self)).path(forResource: "permanent_invalid", ofType: "json") | ||
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/vnd.geo+json"]) | ||
} | ||
|
||
let expectation = self.expectation(description: "invalid batch geocoding query should not return results") | ||
let geocoder = Geocoder(accessToken: BogusToken) | ||
let options = ForwardBatchGeocodeOptions(query: "///") | ||
let task = geocoder.batchGeocode(options) { (placemarks, attribution, error) in | ||
|
||
XCTAssertEqual(error!.localizedFailureReason, "Not Found") | ||
|
||
expectation.fulfill() | ||
} | ||
XCTAssertNotNil(task) | ||
|
||
waitForExpectations(timeout: 1) { (error) in | ||
XCTAssertNil(error, "Error: \(error!)") | ||
XCTAssertEqual(task.state, URLSessionTask.State.completed) | ||
} | ||
} | ||
|
||
func testInvalidTokenForBatchGeocode() { | ||
let invalidToken = "xyz" | ||
|
||
_ = stub(condition: isHost("api.mapbox.com") | ||
&& isPath("/geocoding/v5/mapbox.places-permanent/85+2nd+st+san+francisco.json") | ||
&& containsQueryParams(["access_token": invalidToken])) { _ in | ||
let path = Bundle(for: type(of: self)).path(forResource: "permanent_invalid_token", ofType: "json") | ||
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/vnd.geo+json"]) | ||
} | ||
|
||
let expectation = self.expectation(description: "invalid token use in batch geocoding query should return an error") | ||
let geocoder = Geocoder(accessToken: invalidToken) | ||
let options = ForwardBatchGeocodeOptions(query: "85 2nd st san francisco") | ||
let task = geocoder.batchGeocode(options) { (placemarks, attribution, error) in | ||
|
||
XCTAssertEqual(error!.localizedFailureReason, "Not Authorized - Invalid Token") | ||
|
||
expectation.fulfill() | ||
} | ||
XCTAssertNotNil(task) | ||
|
||
waitForExpectations(timeout: 1) { (error) in | ||
XCTAssertNil(error, "Error: \(error!)") | ||
XCTAssertEqual(task.state, URLSessionTask.State.completed) | ||
} | ||
} | ||
|
||
func testInvalidTokenScopeForBatchGeocoding() { | ||
let incorrectTokenScope = "pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA" | ||
|
||
_ = stub(condition: isHost("api.mapbox.com") | ||
&& isPath("/geocoding/v5/mapbox.places-permanent/85+2nd+st+san+francisco.json") | ||
&& containsQueryParams(["access_token": incorrectTokenScope])) { _ in | ||
let path = Bundle(for: type(of: self)).path(forResource: "permanent_invalid_token_scope", ofType: "json") | ||
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/vnd.geo+json"]) | ||
} | ||
|
||
let expectation = self.expectation(description: "invalid token use in batch geocoding query should return an error") | ||
let geocoder = Geocoder(accessToken: incorrectTokenScope) | ||
let options = ForwardBatchGeocodeOptions(query: "85 2nd st san francisco") | ||
let task = geocoder.batchGeocode(options) { (placemarks, attribution, error) in | ||
|
||
XCTAssertEqual(error!.localizedFailureReason, "Permanent geocodes are not enabled for this account. Contact [email protected] to enable this feature.") | ||
|
||
expectation.fulfill() | ||
} | ||
XCTAssertNotNil(task) | ||
|
||
waitForExpectations(timeout: 1) { (error) in | ||
XCTAssertNil(error, "Error: \(error!)") | ||
XCTAssertEqual(task.state, URLSessionTask.State.completed) | ||
} | ||
} | ||
} |