-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No Response Fix #314
Merged
Merged
No Response Fix #314
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
696f413
Fixing issue where erroneous (such as 413) responses from API can res…
107f8b7
Adding 413 scenario to "Informative error" generating logic.
8b90a2a
Changing MBDirectionsErrorDomain string over to Apple-suggested schema.
b07cf0d
oops
1ba0e64
Adding tests that ensure handling known and unknown bad responses.
9addf1e
Reversing now-unneeded sanity check, as root cause was fixed deeper down
a0b9a3c
Copyedited failure messages
1ec5 188e546
No need to return the raw response
1ec5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,32 @@ import OHHTTPStubs | |
@testable import MapboxDirections | ||
|
||
let BogusToken = "pk.feedCafeDadeDeadBeef-BadeBede.FadeCafeDadeDeed-BadeBede" | ||
let BadResponse = """ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> | ||
<TITLE>ERROR: The request could not be satisfied</TITLE> | ||
</HEAD><BODY> | ||
<H1>413 ERROR</H1> | ||
<H2>The request could not be satisfied.</H2> | ||
<HR noshade size="1px"> | ||
Bad request. | ||
|
||
<BR clear="all"> | ||
<HR noshade size="1px"> | ||
<PRE> | ||
Generated by cloudfront (CloudFront) | ||
Request ID: RAf2XH13mMVxQ96Z1cVQMPrd-hJoVA6LfaWVFDbdN2j-J1VkzaPvZg== | ||
</PRE> | ||
<ADDRESS> | ||
</ADDRESS> | ||
</BODY></HTML> | ||
""" | ||
|
||
class DirectionsTests: XCTestCase { | ||
override func setUp() { | ||
// Make sure tests run in all time zones | ||
NSTimeZone.default = TimeZone(secondsFromGMT: 0)! | ||
} | ||
|
||
override func tearDown() { | ||
OHHTTPStubs.removeAllStubs() | ||
super.tearDown() | ||
|
@@ -21,6 +40,55 @@ class DirectionsTests: XCTestCase { | |
XCTAssertEqual(directions.apiEndpoint.absoluteString, "https://api.mapbox.com") | ||
} | ||
|
||
func testKnownBadResponse() { | ||
let pass = "The operation couldn’t be completed. The request is too large." | ||
|
||
OHHTTPStubs.stubRequests(passingTest: { (request) -> Bool in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return request.url!.absoluteString.contains("https://api.mapbox.com/directions") | ||
}) { (_) -> OHHTTPStubsResponse in | ||
return OHHTTPStubsResponse(data: BadResponse.data(using: .utf8)!, statusCode: 413, headers: ["Content-Type" : "text/html"]) | ||
} | ||
let expectation = XCTestExpectation(description: "Async callback") | ||
let one = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)) | ||
let two = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 2.0, longitude: 2.0)) | ||
|
||
let directions = Directions(accessToken: BogusToken) | ||
let opts = RouteOptions(locations: [one, two]) | ||
directions.calculate(opts, completionHandler: { (waypoints, routes, error) in | ||
expectation.fulfill() | ||
XCTAssertNil(routes, "Unexpected route response") | ||
XCTAssertNotNil(error, "No error returned") | ||
XCTAssertNil(error?.userInfo[NSUnderlyingErrorKey]) | ||
XCTAssertEqual(error?.localizedDescription, pass, "Wrong type of error received") | ||
}) | ||
wait(for: [expectation], timeout: 2.0) | ||
} | ||
|
||
func testUnknownBadResponse() { | ||
let pass = "The operation couldn’t be completed. server error" | ||
|
||
OHHTTPStubs.stubRequests(passingTest: { (request) -> Bool in | ||
return request.url!.absoluteString.contains("https://api.mapbox.com/directions") | ||
}) { (_) -> OHHTTPStubsResponse in | ||
let message = "Enhance your calm, John Spartan." | ||
return OHHTTPStubsResponse(data: message.data(using: .utf8)!, statusCode: 420, headers: ["Content-Type" : "text/plain"]) | ||
} | ||
let expectation = XCTestExpectation(description: "Async callback") | ||
let one = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)) | ||
let two = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 2.0, longitude: 2.0)) | ||
|
||
let directions = Directions(accessToken: BogusToken) | ||
let opts = RouteOptions(locations: [one, two]) | ||
directions.calculate(opts, completionHandler: { (waypoints, routes, error) in | ||
expectation.fulfill() | ||
XCTAssertNil(routes, "Unexpected route response") | ||
XCTAssertNotNil(error, "No error returned") | ||
XCTAssertNil(error?.userInfo[NSUnderlyingErrorKey]) | ||
XCTAssertEqual(error?.localizedDescription, pass, "Wrong type of error received") | ||
}) | ||
wait(for: [expectation], timeout: 2.0) | ||
} | ||
|
||
func testRateLimitErrorParsing() { | ||
let json = ["message" : "Hit rate limit"] | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before anyone asks, this is a great (and rather rare) example of "Do as I say, not as I do" from Apple:
Doc Link