Local server URLRequest in XCTestCase

Our UI Tests require interaction with physical devices. There is a Flask server on Raspberry Pi in our local network which is able to interact with those devices. From within XCTestCase we send request to this server and server does its job. Everything works on iOS13, but on iOS14 there is always an error that there is no internet connection -1009.

Application's and Test's info.plist has:
  • App Transport Security Settings:

Allow Arbitrary Loads: True
Allows Local Networking: True
  • Privacy - Local Network Usage Description

I suspect this is something with 'Local Network Premission' but setting its description in does not solve the problem.

Thanks

Accepted Reply

It sounds like you’re hitting a local network privacy restriction (see my Local Network Privacy FAQ for more on this). Your XCTestCase is hosted in a test runner process. This is not a standard application and thus the system is unable to present the local network privacy alert on its behalf, and so the system will fail all your local network requests as if the user denied local network access.

I don’t see an easy way around this. In a unit test you can avoid this issue by hosting the unit test within your app, but that’s tricky for a UI test.

What sort of network access patterns do your tests need? If it’s just a matter of setting things up in advance, you could do that from your Mac prior to running the test. But if it’s an interactive sequence — set up the accessory, run a test, set up the accessory in a different way, run the test, and so on — that won’t work.

Another option might be to run a proxy on the Raspberry Pi and configure your test device to use that proxy.

Regardless, I think you should file a bug against Xcode’s testing infrastructure. The local network privacy feature was not created to protect users from UI test code (-:

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
  • Hi eskimo, just for record, after a year the problem is still there. Similar topic: https://developer.apple.com/forums/thread/668729 Someone said there that this bug was resolved and appeared again in newer version of iOS but I have never had successful request on any iOS version.

    Thanks :)

  • I am running into the same issue. Its blocked for me. Do we have a solution for this? Kindly help.

Add a Comment

Replies

It sounds like you’re hitting a local network privacy restriction (see my Local Network Privacy FAQ for more on this). Your XCTestCase is hosted in a test runner process. This is not a standard application and thus the system is unable to present the local network privacy alert on its behalf, and so the system will fail all your local network requests as if the user denied local network access.

I don’t see an easy way around this. In a unit test you can avoid this issue by hosting the unit test within your app, but that’s tricky for a UI test.

What sort of network access patterns do your tests need? If it’s just a matter of setting things up in advance, you could do that from your Mac prior to running the test. But if it’s an interactive sequence — set up the accessory, run a test, set up the accessory in a different way, run the test, and so on — that won’t work.

Another option might be to run a proxy on the Raspberry Pi and configure your test device to use that proxy.

Regardless, I think you should file a bug against Xcode’s testing infrastructure. The local network privacy feature was not created to protect users from UI test code (-:

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
  • Hi eskimo, just for record, after a year the problem is still there. Similar topic: https://developer.apple.com/forums/thread/668729 Someone said there that this bug was resolved and appeared again in newer version of iOS but I have never had successful request on any iOS version.

    Thanks :)

  • I am running into the same issue. Its blocked for me. Do we have a solution for this? Kindly help.

Add a Comment
As always, thank you! :)
Some tests can be done be setting things up in advance but not all so I will try proxy approach for now.

Bug: FB8801770 (Unable to request local server via URLRequest within XCTestCase - iOS14)