Skip to content

Commit 752af16

Browse files
CopilotSandPod
andauthored
fix: Normalize server address to handle missing trailing slash (serverpod#4406)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SandPod <137198655+SandPod@users.noreply.github.com>
1 parent 382bdac commit 752af16

3 files changed

Lines changed: 60 additions & 8 deletions

File tree

packages/serverpod_client/lib/src/serverpod_client_shared.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ abstract class ServerpodClientShared extends EndpointCaller {
218218

219219
/// Creates a new ServerpodClientShared.
220220
ServerpodClientShared(
221-
this.host,
221+
String host,
222222
this.serializationManager, {
223223
dynamic securityContext,
224224
@Deprecated(
@@ -230,15 +230,12 @@ abstract class ServerpodClientShared extends EndpointCaller {
230230
this.onFailedCall,
231231
this.onSucceededCall,
232232
bool? disconnectStreamsOnLostInternetConnection,
233-
}) : connectionTimeout = connectionTimeout ?? const Duration(seconds: 20),
233+
}) : host = host.endsWith('/') ? host : '$host/',
234+
connectionTimeout = connectionTimeout ?? const Duration(seconds: 20),
234235
streamingConnectionTimeout =
235236
streamingConnectionTimeout ?? const Duration(seconds: 5) {
236237
assert(
237-
host.endsWith('/'),
238-
'host must end with a slash, eg: https://example.com/',
239-
);
240-
assert(
241-
host.startsWith('http://') || host.startsWith('https://'),
238+
this.host.startsWith('http://') || this.host.startsWith('https://'),
242239
'host must include protocol, eg: https://example.com/',
243240
);
244241
_requestDelegate = ServerpodClientRequestDelegateImpl(
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:test/test.dart';
2+
3+
import 'test_utils/test_serverpod_client.dart';
4+
5+
void main() {
6+
test(
7+
'Given a server address without trailing slash when initializing client then host should have trailing slash added.',
8+
() {
9+
var client = TestServerpodClient(
10+
host: Uri.parse('http://localhost:8080'),
11+
);
12+
expect(client.host, 'http://localhost:8080/');
13+
},
14+
);
15+
16+
test(
17+
'Given a server address with trailing slash when initializing client then host should remain unchanged.',
18+
() {
19+
var client = TestServerpodClient(
20+
host: Uri.parse('http://localhost:8080/'),
21+
);
22+
expect(client.host, 'http://localhost:8080/');
23+
},
24+
);
25+
26+
test(
27+
'Given an HTTPS server address without trailing slash when initializing client then host should have trailing slash added.',
28+
() {
29+
var client = TestServerpodClient(
30+
host: Uri.parse('https://example.com'),
31+
);
32+
expect(client.host, 'https://example.com/');
33+
},
34+
);
35+
36+
test(
37+
'Given a server address with path and without trailing slash when initializing client then host should have trailing slash added.',
38+
() {
39+
var client = TestServerpodClient(
40+
host: Uri.parse('http://localhost:8080/api'),
41+
);
42+
expect(client.host, 'http://localhost:8080/api/');
43+
},
44+
);
45+
46+
test(
47+
'Given a server address with path and with trailing slash when initializing client then host should remain unchanged.',
48+
() {
49+
var client = TestServerpodClient(
50+
host: Uri.parse('http://localhost:8080/api/'),
51+
);
52+
expect(client.host, 'http://localhost:8080/api/');
53+
},
54+
);
55+
}

packages/serverpod_client/test/test_utils/test_serverpod_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TestServerpodClient extends ServerpodClientShared {
99
required Uri host,
1010
ClientAuthKeyProvider? authKeyProvider,
1111
}) : super(
12-
'${host.toString()}/',
12+
host.toString(),
1313
TestSerializationManager(),
1414
streamingConnectionTimeout: const Duration(seconds: 5),
1515
connectionTimeout: const Duration(seconds: 20),

0 commit comments

Comments
 (0)