Skip to content

Commit

Permalink
Rename/cleanup tests & comments
Browse files Browse the repository at this point in the history
  • Loading branch information
captainbarbosa committed Jul 4, 2018
1 parent d475a79 commit 9fbb49a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 13 deletions.
40 changes: 32 additions & 8 deletions MapboxGeocoder.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
071123A720E6EC890043CB51 /* permanent_forward_single_no_results.json in Resources */ = {isa = PBXBuildFile; fileRef = 071123A620E6EC880043CB51 /* permanent_forward_single_no_results.json */; };
071123A820E6EC890043CB51 /* permanent_forward_single_no_results.json in Resources */ = {isa = PBXBuildFile; fileRef = 071123A620E6EC880043CB51 /* permanent_forward_single_no_results.json */; };
071123A920E6EC890043CB51 /* permanent_forward_single_no_results.json in Resources */ = {isa = PBXBuildFile; fileRef = 071123A620E6EC880043CB51 /* permanent_forward_single_no_results.json */; };
079F41E820E42EEF007EEB5E /* BatchForwardGeocodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 079F41E720E42EEF007EEB5E /* BatchForwardGeocodingTests.swift */; };
079F41E920E42EEF007EEB5E /* BatchForwardGeocodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 079F41E720E42EEF007EEB5E /* BatchForwardGeocodingTests.swift */; };
079F41EA20E42EEF007EEB5E /* BatchForwardGeocodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 079F41E720E42EEF007EEB5E /* BatchForwardGeocodingTests.swift */; };
079F41E820E42EEF007EEB5E /* BatchGeocodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 079F41E720E42EEF007EEB5E /* BatchGeocodingTests.swift */; };
079F41E920E42EEF007EEB5E /* BatchGeocodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 079F41E720E42EEF007EEB5E /* BatchGeocodingTests.swift */; };
079F41EA20E42EEF007EEB5E /* BatchGeocodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 079F41E720E42EEF007EEB5E /* BatchGeocodingTests.swift */; };
07CF858220EC60B8007B26B6 /* permanent_invalid.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858120EC60B8007B26B6 /* permanent_invalid.json */; };
07CF858320EC60B8007B26B6 /* permanent_invalid.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858120EC60B8007B26B6 /* permanent_invalid.json */; };
07CF858420EC60B8007B26B6 /* permanent_invalid.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858120EC60B8007B26B6 /* permanent_invalid.json */; };
07CF858620EC6382007B26B6 /* permanent_invalid_token.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858520EC6382007B26B6 /* permanent_invalid_token.json */; };
07CF858720EC6382007B26B6 /* permanent_invalid_token.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858520EC6382007B26B6 /* permanent_invalid_token.json */; };
07CF858820EC6382007B26B6 /* permanent_invalid_token.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858520EC6382007B26B6 /* permanent_invalid_token.json */; };
07CF858A20EC647F007B26B6 /* permanent_invalid_token_scope.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858920EC647F007B26B6 /* permanent_invalid_token_scope.json */; };
07CF858B20EC647F007B26B6 /* permanent_invalid_token_scope.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858920EC647F007B26B6 /* permanent_invalid_token_scope.json */; };
07CF858C20EC647F007B26B6 /* permanent_invalid_token_scope.json in Resources */ = {isa = PBXBuildFile; fileRef = 07CF858920EC647F007B26B6 /* permanent_invalid_token_scope.json */; };
35506B8B200F856400629509 /* BridgingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 35506B8A200F856400629509 /* BridgingTests.m */; };
35506B8C200F856400629509 /* BridgingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 35506B8A200F856400629509 /* BridgingTests.m */; };
35506B8D200F856400629509 /* BridgingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 35506B8A200F856400629509 /* BridgingTests.m */; };
Expand Down Expand Up @@ -189,7 +198,10 @@
0711239E20E5A53E0043CB51 /* permanent_forward_multiple_valid.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = permanent_forward_multiple_valid.json; path = ../../../../Desktop/permanent_forward_multiple_valid.json; sourceTree = "<group>"; };
071123A220E6EC7F0043CB51 /* permanent_forward_multiple_no_results.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = permanent_forward_multiple_no_results.json; path = ../../../../Desktop/permanent_forward_multiple_no_results.json; sourceTree = "<group>"; };
071123A620E6EC880043CB51 /* permanent_forward_single_no_results.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = permanent_forward_single_no_results.json; path = ../../../../Desktop/permanent_forward_single_no_results.json; sourceTree = "<group>"; };
079F41E720E42EEF007EEB5E /* BatchForwardGeocodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchForwardGeocodingTests.swift; sourceTree = "<group>"; };
079F41E720E42EEF007EEB5E /* BatchGeocodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchGeocodingTests.swift; sourceTree = "<group>"; };
07CF858120EC60B8007B26B6 /* permanent_invalid.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = permanent_invalid.json; path = ../../../../Desktop/permanent_invalid.json; sourceTree = "<group>"; };
07CF858520EC6382007B26B6 /* permanent_invalid_token.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = permanent_invalid_token.json; path = ../../../../Desktop/permanent_invalid_token.json; sourceTree = "<group>"; };
07CF858920EC647F007B26B6 /* permanent_invalid_token_scope.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = permanent_invalid_token_scope.json; path = ../../../../Desktop/permanent_invalid_token_scope.json; sourceTree = "<group>"; };
35506B8A200F856400629509 /* BridgingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BridgingTests.m; sourceTree = "<group>"; };
357B4357202CC90A00735521 /* reverse_address.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = reverse_address.json; sourceTree = "<group>"; };
DA1AC0211E5C23B8006DF1D6 /* Contacts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Contacts.framework; path = System/Library/Frameworks/Contacts.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -422,7 +434,7 @@
DDF1E84B1BD6F7BA00C40C78 /* MapboxGeocoderTests */ = {
isa = PBXGroup;
children = (
079F41E720E42EEF007EEB5E /* BatchForwardGeocodingTests.swift */,
079F41E720E42EEF007EEB5E /* BatchGeocodingTests.swift */,
DA701C001CB1292C00B0E520 /* GeocoderTests.swift */,
DA210BAA1CB4BE73008088FD /* ForwardGeocodingTests.swift */,
DDF1E84C1BD6F7BA00C40C78 /* ReverseGeocodingTests.swift */,
Expand All @@ -437,6 +449,9 @@
DDF1E8571BD700EB00C40C78 /* Fixtures */ = {
isa = PBXGroup;
children = (
07CF858920EC647F007B26B6 /* permanent_invalid_token_scope.json */,
07CF858520EC6382007B26B6 /* permanent_invalid_token.json */,
07CF858120EC60B8007B26B6 /* permanent_invalid.json */,
071123A620E6EC880043CB51 /* permanent_forward_single_no_results.json */,
071123A220E6EC7F0043CB51 /* permanent_forward_multiple_no_results.json */,
0711239E20E5A53E0043CB51 /* permanent_forward_multiple_valid.json */,
Expand Down Expand Up @@ -751,14 +766,17 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
07CF858720EC6382007B26B6 /* permanent_invalid_token.json in Resources */,
071123A820E6EC890043CB51 /* permanent_forward_single_no_results.json in Resources */,
071123A420E6EC800043CB51 /* permanent_forward_multiple_no_results.json in Resources */,
DA5170B81CF1B1F900CD6DCF /* forward_valid.json in Resources */,
357B4359202CC90A00735521 /* reverse_address.json in Resources */,
DA5170B91CF1B1F900CD6DCF /* forward_invalid.json in Resources */,
DA5170BB1CF1B1F900CD6DCF /* reverse_invalid.json in Resources */,
DA5170BA1CF1B1F900CD6DCF /* reverse_valid.json in Resources */,
07CF858B20EC647F007B26B6 /* permanent_invalid_token_scope.json in Resources */,
071123A020E5A53E0043CB51 /* permanent_forward_multiple_valid.json in Resources */,
07CF858320EC60B8007B26B6 /* permanent_invalid.json in Resources */,
0711239420E59DEA0043CB51 /* permanent_forward_single_valid.json in Resources */,
DA58D8A21DB2ADDD00530CBB /* forward_valid_zh.json in Resources */,
);
Expand All @@ -775,14 +793,17 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
07CF858820EC6382007B26B6 /* permanent_invalid_token.json in Resources */,
071123A920E6EC890043CB51 /* permanent_forward_single_no_results.json in Resources */,
071123A520E6EC800043CB51 /* permanent_forward_multiple_no_results.json in Resources */,
DA5170E21CF2542B00CD6DCF /* forward_valid.json in Resources */,
357B435A202CC90A00735521 /* reverse_address.json in Resources */,
DA5170E31CF2542B00CD6DCF /* forward_invalid.json in Resources */,
DA5170E51CF2542B00CD6DCF /* reverse_invalid.json in Resources */,
DA5170E41CF2542B00CD6DCF /* reverse_valid.json in Resources */,
07CF858C20EC647F007B26B6 /* permanent_invalid_token_scope.json in Resources */,
071123A120E5A53E0043CB51 /* permanent_forward_multiple_valid.json in Resources */,
07CF858420EC60B8007B26B6 /* permanent_invalid.json in Resources */,
0711239520E59DEA0043CB51 /* permanent_forward_single_valid.json in Resources */,
DA58D8A31DB2ADDD00530CBB /* forward_valid_zh.json in Resources */,
);
Expand Down Expand Up @@ -822,14 +843,17 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
07CF858620EC6382007B26B6 /* permanent_invalid_token.json in Resources */,
071123A720E6EC890043CB51 /* permanent_forward_single_no_results.json in Resources */,
071123A320E6EC800043CB51 /* permanent_forward_multiple_no_results.json in Resources */,
DA210BAD1CB4BFF7008088FD /* forward_valid.json in Resources */,
357B4358202CC90A00735521 /* reverse_address.json in Resources */,
DDF1E85C1BD70E4C00C40C78 /* reverse_invalid.json in Resources */,
DDF1E85D1BD70E4C00C40C78 /* reverse_valid.json in Resources */,
DA210BAF1CB4C5A7008088FD /* forward_invalid.json in Resources */,
07CF858A20EC647F007B26B6 /* permanent_invalid_token_scope.json in Resources */,
0711239F20E5A53E0043CB51 /* permanent_forward_multiple_valid.json in Resources */,
07CF858220EC60B8007B26B6 /* permanent_invalid.json in Resources */,
0711239320E59DEA0043CB51 /* permanent_forward_single_valid.json in Resources */,
DA58D8A11DB2ADDD00530CBB /* forward_valid_zh.json in Resources */,
);
Expand Down Expand Up @@ -922,7 +946,7 @@
DA5170B61CF1B1EF00CD6DCF /* ReverseGeocodingTests.swift in Sources */,
DA4D90081DD63AEC006EC71A /* PlacemarkScopeTests.swift in Sources */,
DA5170B41CF1B1EF00CD6DCF /* GeocoderTests.swift in Sources */,
079F41E920E42EEF007EEB5E /* BatchForwardGeocodingTests.swift in Sources */,
079F41E920E42EEF007EEB5E /* BatchGeocodingTests.swift in Sources */,
DA5170B51CF1B1EF00CD6DCF /* ForwardGeocodingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -947,7 +971,7 @@
DA5170E11CF2542800CD6DCF /* ReverseGeocodingTests.swift in Sources */,
DA4D90091DD63AEC006EC71A /* PlacemarkScopeTests.swift in Sources */,
DA5170DF1CF2542800CD6DCF /* GeocoderTests.swift in Sources */,
079F41EA20E42EEF007EEB5E /* BatchForwardGeocodingTests.swift in Sources */,
079F41EA20E42EEF007EEB5E /* BatchGeocodingTests.swift in Sources */,
DA5170E01CF2542800CD6DCF /* ForwardGeocodingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1003,7 +1027,7 @@
DDF1E84D1BD6F7BA00C40C78 /* ReverseGeocodingTests.swift in Sources */,
DA4D90071DD63AEC006EC71A /* PlacemarkScopeTests.swift in Sources */,
DA210BAB1CB4BE73008088FD /* ForwardGeocodingTests.swift in Sources */,
079F41E820E42EEF007EEB5E /* BatchForwardGeocodingTests.swift in Sources */,
079F41E820E42EEF007EEB5E /* BatchGeocodingTests.swift in Sources */,
DA701C011CB1292C00B0E520 /* GeocoderTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 3 additions & 3 deletions MapboxGeocoder/MBGeocoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ open class Geocoder: NSObject {
let decoder = JSONDecoder()

do {

let result: [GeocodeResult]

do {
// Decode multiple batch geocoding queries
result = try decoder.decode([GeocodeResult].self, from: data)
Expand All @@ -241,8 +243,6 @@ open class Geocoder: NSObject {
}

}) { (error) in
// testInvalidForwardSingleBatchGeocode fails here
// testInvalidForwardMultipleBatchGeocode fails here
completionHandler(nil, nil, error)
}
task.resume()
Expand All @@ -260,6 +260,7 @@ open class Geocoder: NSObject {
*/
fileprivate func dataTaskWithURL(_ url: URL, completionHandler: @escaping (_ data: Data?) -> Void, errorHandler: @escaping (_ error: NSError) -> Void) -> URLSessionDataTask {
var request = URLRequest(url: url)

request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
return URLSession.shared.dataTask(with: request) { (data, response, error) in

Expand All @@ -286,7 +287,6 @@ open class Geocoder: NSObject {
do {
let result = try decoder.decode(GeocodeAPIResult.self, from: data)
// Check if geocoding query failed
// testInvalidForwardSingleBatchGeocode & testInvalidForwardMultipleBatchGeocode being caught here
guard result.message == nil else {
let apiError = Geocoder.descriptiveError(["message": result.message!], response: response, underlyingError: error as NSError?)
DispatchQueue.main.async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 9fbb49a

Please sign in to comment.