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
Answered by DTS Engineer in 640886022
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"
Accepted Answer
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"
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)

I've found a solution for asking LocalNetwork permission on UI Test target and described it here in my answer.

@Rashidov I saw your answer, I did the same but when I granted permission a second request had the same error: The internet connection appears to be offline. Have you tried your solution is it work for you? :'(

UPD: Lol. I had to restart my iPhone 0_o. I wasted a whole day to understand what was wrong and the problem was only this great device that should be restarted, omg... The previous comment is not actual

Lol. I had to restart my iPhone 0_o. I wasted a whole day to understand what was wrong and the problem was only this great device that should be restarted, omg... The previous comment is not actual

Local server URLRequest in XCTestCase
 
 
Q