Skip to content

Commit aa312c6

Browse files
committed
test: ensure localhost and 10.0.2.2 are handled
1 parent 96d2663 commit aa312c6

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

packages/firebase_storage/firebase_storage/lib/src/utils.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Map<String, String?>? partsFromHttpUrl(String url) {
4343
return null;
4444
}
4545

46-
// firebase storage url
47-
// 10.0.2.2 is for Android when using firebase emulator
48-
if (decodedUrl.contains(_firebaseStorageHost) ||
49-
decodedUrl.contains('localhost') ||
50-
decodedUrl.contains('10.0.2.2')) {
46+
// 10.0.2.2 is used on Android emulators for connecting to the host machine's Firebase emulator.
47+
final isEmulatorHost =
48+
decodedUrl.contains('localhost') || decodedUrl.contains('10.0.2.2');
49+
final isFirebaseStorageUrl = decodedUrl.contains(_firebaseStorageHost);
50+
if (isFirebaseStorageUrl || isEmulatorHost) {
5151
String origin;
52-
if (decodedUrl.contains('localhost') || decodedUrl.contains('10.0.2.2')) {
52+
if (isEmulatorHost) {
5353
Uri uri = Uri.parse(url);
5454
origin = '^http?://${uri.host}:${uri.port}';
5555
} else {
@@ -71,8 +71,8 @@ Map<String, String?>? partsFromHttpUrl(String url) {
7171
'bucket': match.group(1),
7272
'path': match.group(3),
7373
};
74-
// google cloud storage url
7574
} else {
75+
// Google Cloud storage url
7676
RegExp cloudStorageRegExp = RegExp(
7777
'^https?://$_cloudStorageHost$_optionalPort/$_bucketDomain/$_cloudStoragePath',
7878
caseSensitive: false,

packages/firebase_storage/firebase_storage/test/utils_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,38 @@ void main() {
104104
expect(result?['path'], 'path/to/foo_bar.jpg');
105105
});
106106

107+
test(
108+
'parses HTTP URL correctly when using Android emulator localhost (10.0.2.2)',
109+
() {
110+
const androidLocalhost = '10.0.2.2';
111+
112+
final result = partsFromHttpUrl(
113+
'http://$androidLocalhost:9199/v0/b/myapp.appspot.com/o/path/to/foo_bar.jpg');
114+
115+
expect(
116+
result,
117+
isNotNull,
118+
reason:
119+
'partsFromHttpUrl should not return null for Android localhost URLs',
120+
);
121+
expect(result?['bucket'], 'myapp.appspot.com');
122+
expect(result?['path'], 'path/to/foo_bar.jpg');
123+
});
124+
125+
test('parses HTTP URL correctly when using standard localhost (127.0.0.1)',
126+
() {
127+
final result = partsFromHttpUrl(
128+
'http://localhost:9199/v0/b/myapp.appspot.com/o/path/to/foo_bar.jpg');
129+
130+
expect(
131+
result,
132+
isNotNull,
133+
reason: 'partsFromHttpUrl should not return null for localhost URLs',
134+
);
135+
expect(result?['bucket'], 'myapp.appspot.com');
136+
expect(result?['path'], 'path/to/foo_bar.jpg');
137+
});
138+
107139
// TODO(helenaford): regexp can't handle no paths
108140
// test('sets path to default if null', () {
109141
// String url =

0 commit comments

Comments
 (0)