Skip to content

Commit

Permalink
[various] Update invalid NSURL tests (flutter#4959)
Browse files Browse the repository at this point in the history
For applications linked against iOS 17 or later, NSURL no longer returns `nil` for invalid URLs, so tests that were specific to that behavior have to be adjusted to allow for another outcome.

Fixes flutter#134972
Part of flutter#134971
  • Loading branch information
stuartmorgan authored Sep 20, 2023
1 parent 51e74b9 commit d0411e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ final class URLLauncherTests: XCTestCase {
var error: FlutterError?
let result = createPlugin().canLaunchURL("urls can't have spaces", error: &error)

XCTAssertNil(result)
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, "argument_error")
XCTAssertEqual(error?.message, "Unable to parse URL")
XCTAssertEqual(error?.details as? String, "Provided URL: urls can't have spaces")
if (error == nil) {
// When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't
// fail to parse URLs, so the test must allow for either outcome.
XCTAssertNotNil(result)
XCTAssertFalse(result?.boolValue ?? true)
XCTAssertNil(error)
} else {
XCTAssertNil(result)
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, "argument_error")
XCTAssertEqual(error?.message, "Unable to parse URL")
XCTAssertEqual(error?.details as? String, "Provided URL: urls can't have spaces")
}
}

func testLaunchSuccess() {
Expand Down Expand Up @@ -75,11 +83,19 @@ final class URLLauncherTests: XCTestCase {
let expectation = XCTestExpectation(description: "completion called")

createPlugin().launchURL("urls can't have spaces", universalLinksOnly: false) { result, error in
XCTAssertNil(result)
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, "argument_error")
XCTAssertEqual(error?.message, "Unable to parse URL")
XCTAssertEqual(error?.details as? String, "Provided URL: urls can't have spaces")
if (error == nil) {
// When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't
// fail to parse URLs, so the test must allow for either outcome.
XCTAssertNotNil(result)
XCTAssertFalse(result?.boolValue ?? true)
XCTAssertNil(error)
} else {
XCTAssertNil(result)
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, "argument_error")
XCTAssertEqual(error?.message, "Unable to parse URL")
XCTAssertEqual(error?.details as? String, "Provided URL: urls can't have spaces")
}

expectation.fulfill()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ - (void)testLoadRequest {

- (void)testLoadRequestWithInvalidUrl {
FWFWebView *mockWebView = OCMClassMock([FWFWebView class]);
OCMReject([mockWebView loadRequest:OCMOCK_ANY]);

FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];
Expand All @@ -65,16 +64,24 @@ - (void)testLoadRequestWithInvalidUrl {
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

NSString *badURLString = @"%invalidUrl%";
FlutterError *error;
FWFNSUrlRequestData *requestData = [FWFNSUrlRequestData makeWithUrl:@"%invalidUrl%"
FWFNSUrlRequestData *requestData = [FWFNSUrlRequestData makeWithUrl:badURLString
httpMethod:nil
httpBody:nil
allHttpHeaderFields:@{}];
[hostAPI loadRequestForWebViewWithIdentifier:@0 request:requestData error:&error];
XCTAssertNotNil(error);
XCTAssertEqualObjects(error.code, @"FWFURLRequestParsingError");
XCTAssertEqualObjects(error.message, @"Failed instantiating an NSURLRequest.");
XCTAssertEqualObjects(error.details, @"URL was: '%invalidUrl%'");
// When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't
// fail to parse URLs, so the test must allow for either outcome.
if (error) {
XCTAssertEqualObjects(error.code, @"FWFURLRequestParsingError");
XCTAssertEqualObjects(error.message, @"Failed instantiating an NSURLRequest.");
XCTAssertEqualObjects(error.details, @"URL was: '%invalidUrl%'");
} else {
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:badURLString]];
OCMVerify([mockWebView loadRequest:request]);
}
}

- (void)testSetCustomUserAgent {
Expand Down

0 comments on commit d0411e4

Please sign in to comment.