From 380982d5d06d1b18eb6c109ea0968a403f9b7ebc Mon Sep 17 00:00:00 2001 From: ArekAras Date: Fri, 20 Nov 2026 18:12:06 +0100 Subject: [PATCH] test(iOS): cover the Configure Bundler prefill host and port --- .../React/CoreModules/RCTDevMenu.h | 17 +++ .../React/CoreModules/RCTDevMenu.mm | 32 ++++-- .../RNTesterPods.xcodeproj/project.pbxproj | 4 + .../RNTesterUnitTests/RCTDevMenuTests.m | 100 ++++++++++++++++++ 4 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 packages/rn-tester/RNTesterUnitTests/RCTDevMenuTests.m diff --git a/packages/react-native/React/CoreModules/RCTDevMenu.h b/packages/react-native/React/CoreModules/RCTDevMenu.h index ab6132f0a442..16899f22c68d 100644 --- a/packages/react-native/React/CoreModules/RCTDevMenu.h +++ b/packages/react-native/React/CoreModules/RCTDevMenu.h @@ -14,6 +14,23 @@ RCT_EXTERN NSString *const RCTShowDevMenuNotification; +#if RCT_DEV_MENU + +/** + * The bundler host and port shown in Configure Bundler. Exposed for testing. + * The host comes from the bundle URL, then `jsLocation`, then `ipFileHost`, then localhost. + * The port comes from the same source as the host, otherwise it is the default Metro port. + */ +RCT_EXTERN void RCTDevMenuGetCurrentBundler( + NSURL *bundleURL, + NSString *jsLocation, + NSString *packagerScheme, + NSString *ipFileHost, + NSString **host, + NSNumber **port); + +#endif + @interface RCTDevMenuConfiguration : NSObject #if RCT_DEV_MENU diff --git a/packages/react-native/React/CoreModules/RCTDevMenu.mm b/packages/react-native/React/CoreModules/RCTDevMenu.mm index 56226f95f76a..44044fcddb58 100644 --- a/packages/react-native/React/CoreModules/RCTDevMenu.mm +++ b/packages/react-native/React/CoreModules/RCTDevMenu.mm @@ -31,28 +31,29 @@ - (RCTDevMenuItem *)devMenuItem; // The bundler the app uses now, to prefill Configure Bundler: the host of the bundle URL, then the saved // jsLocation, then ip.txt (written by react-native-xcode.sh for devices), then localhost. -static void RCTDevMenuGetCurrentBundler(NSURL *bundleURL, NSString **host, NSNumber **port) +// The port comes from the same source as the host, otherwise it is the default Metro port. +void RCTDevMenuGetCurrentBundler( + NSURL *bundleURL, + NSString *jsLocation, + NSString *packagerScheme, + NSString *ipFileHost, + NSString **host, + NSNumber **port) { NSString *currentHost = bundleURL.host; NSNumber *currentPort = bundleURL.port; - RCTBundleURLProvider *bundleURLProvider = [RCTBundleURLProvider sharedSettings]; - NSString *jsLocation = bundleURLProvider.jsLocation; if (currentHost.length == 0 && jsLocation.length > 0) { NSString *jsLocationURLString = [jsLocation containsString:@"://"] ? jsLocation - : [NSString stringWithFormat:@"%@://%@", bundleURLProvider.packagerScheme, jsLocation]; + : [NSString stringWithFormat:@"%@://%@", packagerScheme, jsLocation]; NSURLComponents *jsLocationComponents = [NSURLComponents componentsWithString:jsLocationURLString]; currentHost = jsLocationComponents.host; currentPort = jsLocationComponents.port; } if (currentHost.length == 0) { - NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; - if (ipPath != nil) { - currentHost = [[NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil] - stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - } + currentHost = [ipFileHost stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } *host = currentHost.length > 0 ? currentHost : @"localhost"; @@ -402,7 +403,18 @@ - (void)setDefaultJSBundle handler:^{ NSString *currentHost = nil; NSNumber *currentPort = nil; - RCTDevMenuGetCurrentBundler(bundleManager.bundleURL, ¤tHost, ¤tPort); + RCTBundleURLProvider *bundleURLProvider = [RCTBundleURLProvider sharedSettings]; + NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"]; + NSString *ipFileHost = ipPath != nil + ? [NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil] + : nil; + RCTDevMenuGetCurrentBundler( + bundleManager.bundleURL, + bundleURLProvider.jsLocation, + bundleURLProvider.packagerScheme, + ipFileHost, + ¤tHost, + ¤tPort); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Configure Bundler" diff --git a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj index f0a5f5305351..bd644d2fb17a 100644 --- a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj +++ b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ E62F11832A5C6580000BF1C8 /* FlexibleSizeExampleView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.mm */; }; E62F11842A5C6584000BF1C8 /* UpdatePropertiesExampleView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.mm */; }; E7DB20D122B2BAA6005AC45F /* RCTBundleURLProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20A922B2BAA3005AC45F /* RCTBundleURLProviderTests.m */; }; + E7DB20D222B2BAA6005AC45F /* RCTDevMenuTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AA22B2BAA3005AC45F /* RCTDevMenuTests.m */; }; E7DB20D322B2BAA6005AC45F /* RCTBlobManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AB22B2BAA3005AC45F /* RCTBlobManagerTests.m */; }; E7DB20D522B2BAA6005AC45F /* RCTPerformanceLoggerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AD22B2BAA3005AC45F /* RCTPerformanceLoggerTests.m */; }; E7DB20D622B2BAA6005AC45F /* RCTFontTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DB20AF22B2BAA4005AC45F /* RCTFontTests.m */; }; @@ -106,6 +107,7 @@ E7DB209F22B2BA84005AC45F /* RNTesterUnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNTesterUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; E7DB20A322B2BA84005AC45F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; E7DB20A922B2BAA3005AC45F /* RCTBundleURLProviderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTBundleURLProviderTests.m; sourceTree = ""; }; + E7DB20AA22B2BAA3005AC45F /* RCTDevMenuTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDevMenuTests.m; sourceTree = ""; }; E7DB20AB22B2BAA3005AC45F /* RCTBlobManagerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTBlobManagerTests.m; sourceTree = ""; }; E7DB20AD22B2BAA3005AC45F /* RCTPerformanceLoggerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPerformanceLoggerTests.m; sourceTree = ""; }; E7DB20AF22B2BAA4005AC45F /* RCTFontTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTFontTests.m; sourceTree = ""; }; @@ -299,6 +301,7 @@ E7DB20B222B2BAA4005AC45F /* RCTAnimationUtilsTests.m */, E7DB20AB22B2BAA3005AC45F /* RCTBlobManagerTests.m */, E7DB20A922B2BAA3005AC45F /* RCTBundleURLProviderTests.m */, + E7DB20AA22B2BAA3005AC45F /* RCTDevMenuTests.m */, E7DB20CA22B2BAA5005AC45F /* RCTConvert_NSURLTests.m */, E7DB20CE22B2BAA5005AC45F /* RCTConvert_YGValueTests.m */, 383889D923A7398900D06C3E /* RCTConvert_UIColorTests.m */, @@ -621,6 +624,7 @@ E7DB20D522B2BAA6005AC45F /* RCTPerformanceLoggerTests.m in Sources */, E7DB20D922B2BAA6005AC45F /* RCTAnimationUtilsTests.m in Sources */, E7DB20D122B2BAA6005AC45F /* RCTBundleURLProviderTests.m in Sources */, + E7DB20D222B2BAA6005AC45F /* RCTDevMenuTests.m in Sources */, E7DB20EC22B2BAA6005AC45F /* RCTMultipartStreamReaderTests.m in Sources */, E7DB20E022B2BAA6005AC45F /* RCTMethodArgumentTests.m in Sources */, E7DB20E822B2BAA6005AC45F /* RCTModuleMethodTests.mm in Sources */, diff --git a/packages/rn-tester/RNTesterUnitTests/RCTDevMenuTests.m b/packages/rn-tester/RNTesterUnitTests/RCTDevMenuTests.m new file mode 100644 index 000000000000..dfb3401930f2 --- /dev/null +++ b/packages/rn-tester/RNTesterUnitTests/RCTDevMenuTests.m @@ -0,0 +1,100 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import +#import + +#if RCT_DEV_MENU + +@interface RCTDevMenuTests : XCTestCase +@end + +@implementation RCTDevMenuTests + +- (void)assertBundlerForBundleURL:(NSURL *)bundleURL + jsLocation:(NSString *)jsLocation + ipFileHost:(NSString *)ipFileHost + host:(NSString *)expectedHost + port:(NSNumber *)expectedPort +{ + NSString *host = nil; + NSNumber *port = nil; + RCTDevMenuGetCurrentBundler(bundleURL, jsLocation, @"http", ipFileHost, &host, &port); + XCTAssertEqualObjects(host, expectedHost); + XCTAssertEqualObjects(port, expectedPort); +} + +- (void)testBundleURLHostAndPort +{ + [self assertBundlerForBundleURL:[NSURL URLWithString:@"http://192.168.1.5:8082/index.bundle?platform=ios"] + jsLocation:@"10.0.0.1:9000" + ipFileHost:@"10.0.0.2" + host:@"192.168.1.5" + port:@8082]; +} + +- (void)testBundleURLWithoutPortUsesDefaultPort +{ + [self assertBundlerForBundleURL:[NSURL URLWithString:@"https://bundler.example.com/index.bundle?platform=ios"] + jsLocation:nil + ipFileHost:nil + host:@"bundler.example.com" + port:@(RCT_METRO_PORT)]; +} + +- (void)testJSLocationHostAndPortWhenBundleURLHasNoHost +{ + [self assertBundlerForBundleURL:[NSURL fileURLWithPath:@"/tmp/main.jsbundle"] + jsLocation:@"192.168.1.5:8082" + ipFileHost:@"10.0.0.2" + host:@"192.168.1.5" + port:@8082]; +} + +- (void)testJSLocationWithoutPortDoesNotUseBundleURLPort +{ + // A bundle URL with a port but no host must not lend its port to a jsLocation without one. + [self assertBundlerForBundleURL:[NSURL URLWithString:@"file://:8082/main.jsbundle"] + jsLocation:@"192.168.1.5" + ipFileHost:nil + host:@"192.168.1.5" + port:@(RCT_METRO_PORT)]; +} + +- (void)testJSLocationWithScheme +{ + [self assertBundlerForBundleURL:nil + jsLocation:@"https://bundler.example.com:8443" + ipFileHost:nil + host:@"bundler.example.com" + port:@8443]; +} + +- (void)testJSLocationIPv6 +{ + [self assertBundlerForBundleURL:nil jsLocation:@"[::1]:8082" ipFileHost:nil host:@"[::1]" port:@8082]; +} + +- (void)testIPFileHostWhenNoBundleURLOrJSLocation +{ + [self assertBundlerForBundleURL:nil + jsLocation:nil + ipFileHost:@"192.168.1.5\n" + host:@"192.168.1.5" + port:@(RCT_METRO_PORT)]; +} + +- (void)testLocalhostByDefault +{ + [self assertBundlerForBundleURL:nil jsLocation:@"" ipFileHost:nil host:@"localhost" port:@(RCT_METRO_PORT)]; +} + +@end + +#endif