-
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.
Merge pull request #32 from mapbox/1ec5-escape-from-space-30
Escape queries
- Loading branch information
Showing
5 changed files
with
93 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import XCTest | ||
import Nocilla | ||
import CoreLocation | ||
@testable import MapboxGeocoder | ||
|
||
class ForwardGeocodingTests: XCTestCase { | ||
override func setUp() { | ||
super.setUp() | ||
LSNocilla.sharedInstance().start() | ||
} | ||
|
||
override func tearDown() { | ||
LSNocilla.sharedInstance().clearStubs() | ||
LSNocilla.sharedInstance().stop() | ||
super.setUp() | ||
} | ||
|
||
func testValidForwardGeocode() { | ||
let expectation = expectationWithDescription("forward geocode should return results") | ||
|
||
let json = Fixture.stringFromFileNamed("forward_valid") | ||
stubRequest("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/1600+pennsylvania+ave+nw.json?access_token=\(BogusToken)&country=ca").andReturn(200).withHeaders(["Content-Type": "application/json"]).withBody(json) | ||
|
||
let geocoder = MBGeocoder(accessToken: BogusToken) | ||
var addressPlacemark: MBPlacemark! = nil | ||
geocoder.geocodeAddressString("1600 pennsylvania ave nw", inCountries: ["CA"]) { (placemarks, error) in | ||
XCTAssertEqual(placemarks?.count, 5, "forward geocode should have 5 results") | ||
addressPlacemark = placemarks![0] | ||
|
||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectationsWithTimeout(1) { (error) in | ||
XCTAssertNil(error, "Error: \(error)") | ||
XCTAssertFalse(geocoder.geocoding) | ||
} | ||
|
||
XCTAssertEqual(addressPlacemark.description, "Pennsylvania Ave, Stellarton, Nova Scotia B0K 1S0, Canada", "forward geocode should populate description") | ||
XCTAssertEqual(addressPlacemark.name, "Pennsylvania Ave", "forward geocode should populate name") | ||
XCTAssertEqual(addressPlacemark.location?.coordinate, CLLocationCoordinate2D(latitude: 45.5562851, longitude: -62.661944), "forward geocode should populate location") | ||
XCTAssertEqual(addressPlacemark.scope, .Address, "forward geocode should populate scope") | ||
XCTAssertEqual(addressPlacemark.ISOcountryCode, "CA", "forward geocode should populate ISO country code") | ||
XCTAssertEqual(addressPlacemark.country, "Canada", "forward geocode should populate country") | ||
XCTAssertEqual(addressPlacemark.postalCode, "B0K 1S0", "forward geocode should populate postal code") | ||
XCTAssertEqual(addressPlacemark.administrativeArea, "Nova Scotia", "forward geocode should populate administrative area") | ||
XCTAssertEqual(addressPlacemark.subAdministrativeArea, "Stellarton", "forward geocode should populate sub-administrative area") | ||
XCTAssertEqual(addressPlacemark.locality, "Stellarton", "forward geocode should populate locality") | ||
XCTAssertEqual(addressPlacemark.thoroughfare, "Pennsylvania Ave", "forward geocode should populate thoroughfare") | ||
XCTAssertNil(addressPlacemark.subThoroughfare, "forward geocode should not populate sub-thoroughfare for street-only result") | ||
|
||
XCTAssertNotNil(addressPlacemark.addressDictionary) | ||
let addressDictionary = addressPlacemark.addressDictionary! | ||
XCTAssertEqual(addressDictionary[MBPostalAddressStreetKey] as? String, "Pennsylvania Ave", "forward geocode should populate street in address dictionary") | ||
XCTAssertEqual(addressDictionary[MBPostalAddressCityKey] as? String, "Stellarton", "forward geocode should populate city in address dictionary") | ||
XCTAssertEqual(addressDictionary[MBPostalAddressStateKey] as? String, "Nova Scotia", "forward geocode should populate state in address dictionary") | ||
XCTAssertEqual(addressDictionary[MBPostalAddressCountryKey] as? String, "Canada", "forward geocode should populate country in address dictionary") | ||
XCTAssertEqual(addressDictionary[MBPostalAddressISOCountryCodeKey] as? String, "CA", "forward geocode should populate ISO country code in address dictionary") | ||
} | ||
|
||
func testInvalidForwardGeocode() { | ||
let json = Fixture.stringFromFileNamed("forward_invalid") | ||
stubRequest("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/Sandy+Island,+New+Caledonia.json?access_token=\(BogusToken)&country=fr&types=region%2Cplace%2Clocality%2Cpoi").andReturn(200).withHeaders(["Content-Type": "application/json"]).withBody(json) | ||
|
||
let expection = expectationWithDescription("forward geocode execute completion handler for invalid query") | ||
let geocoder = MBGeocoder(accessToken: BogusToken) | ||
geocoder.geocodeAddressString("Sandy Island, New Caledonia", withAllowedScopes: [.AdministrativeArea, .Place, .Locality, .PointOfInterest], inCountries: ["FR"]) { (placemarks, error) in | ||
XCTAssertEqual(placemarks?.count, 0, "forward geocode should return no results for invalid query") | ||
expection.fulfill() | ||
} | ||
|
||
waitForExpectationsWithTimeout(1) { (error) in | ||
XCTAssertNil(error, "Error: \(error)") | ||
XCTAssertFalse(geocoder.geocoding) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type":"FeatureCollection","query":["sandy","island","new","caledonia"],"features":[],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type":"FeatureCollection","query":["1600","pennsylvania","ave","nw"],"features":[{"id":"address.12284612417573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Stellarton, Nova Scotia B0K 1S0, Canada","relevance":0.645,"properties":{},"center":[-62.661944,45.5562851],"geometry":{"type":"Point","coordinates":[-62.661944,45.5562851]},"context":[{"id":"place.55130","text":"Stellarton"},{"id":"postcode.13310912212063190","text":"B0K 1S0"},{"id":"region.10539239752558240","text":"Nova Scotia"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca"}]},{"id":"address.18050667373573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada","relevance":0.39,"properties":{},"center":[-79.989025,44.503369],"geometry":{"type":"Point","coordinates":[-79.989025,44.503369]},"context":[{"id":"place.61967","text":"Wasaga Beach"},{"id":"postcode.17609609551414490","text":"L9Z 3A8"},{"id":"region.13373639426376420","text":"Ontario"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca"}]},{"id":"address.6485281316573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X6, Canada","relevance":0.39,"properties":{},"center":[-79.532497,43.802232],"geometry":{"type":"Point","coordinates":[-79.532497,43.802232]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.60547","text":"Vaughan"},{"id":"postcode.6385804433309570","text":"L4K 3X6"},{"id":"region.13373639426376420","text":"Ontario"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca"}]},{"id":"address.6456604709573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X8, Canada","relevance":0.39,"properties":{},"center":[-79.531684,43.802706],"geometry":{"type":"Point","coordinates":[-79.531684,43.802706]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.60547","text":"Vaughan"},{"id":"postcode.6367605302663760","text":"L4K 3X8"},{"id":"region.13373639426376420","text":"Ontario"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca"}]},{"id":"region.14011193683716580","type":"Feature","text":"Northwest Territories","place_name":"Northwest Territories, Canada","relevance":0.24,"properties":{},"bbox":[-141.102750026,59.9981700000001,-102.0004990085,79.229293683],"center":[-128.403942,69.564626],"geometry":{"type":"Point","coordinates":[-128.403942,69.564626]},"context":[{"id":"country.15589894856372040","text":"Canada","short_code":"ca"}]}],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} |