Skip to content

Commit d3a1e56

Browse files
committed
fix: base_url() removes trailing slash in baseURL
1 parent 9a59d99 commit d3a1e56

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function base_url($relativePath = '', ?string $scheme = null): string
124124
$config = clone config('App');
125125
$config->indexPage = '';
126126

127-
return rtrim(site_url($relativePath, $scheme, $config), '/');
127+
return site_url($relativePath, $scheme, $config);
128128
}
129129
}
130130

tests/system/Helpers/URLHelper/SiteUrlTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,31 @@ protected function tearDown(): void
5959
* @param bool $secure
6060
* @param string $path
6161
* @param string $expectedSiteUrl
62+
* @param string $expectedBaseUrl
6263
*
6364
* @dataProvider configProvider
6465
*/
65-
public function testUrls($baseURL, $indexPage, $scheme, $secure, $path, $expectedSiteUrl)
66-
{
66+
public function testUrls(
67+
$baseURL,
68+
$indexPage,
69+
$scheme,
70+
$secure,
71+
$path,
72+
$expectedSiteUrl,
73+
$expectedBaseUrl
74+
) {
6775
// Set the config
6876
$this->config->baseURL = $baseURL;
6977
$this->config->indexPage = $indexPage;
7078
$this->config->forceGlobalSecureRequests = $secure;
7179

7280
$this->assertSame($expectedSiteUrl, site_url($path, $scheme, $this->config));
73-
74-
// base_url is always the trimmed site_url without index page
75-
$expectedBaseUrl = $indexPage === '' ? $expectedSiteUrl : str_replace('/' . $indexPage, '', $expectedSiteUrl);
76-
$expectedBaseUrl = rtrim($expectedBaseUrl, '/');
7781
$this->assertSame($expectedBaseUrl, base_url($path, $scheme));
7882
}
7983

8084
public function configProvider()
8185
{
82-
// baseURL, indexPage, scheme, secure, path, expectedSiteUrl
86+
// baseURL, indexPage, scheme, secure, path, expectedSiteUrl, expectedBaseUrl
8387
return [
8488
'forceGlobalSecure' => [
8589
'http://example.com/',
@@ -88,6 +92,7 @@ public function configProvider()
8892
true,
8993
'',
9094
'https://example.com/index.php',
95+
'https://example.com/',
9196
],
9297
[
9398
'http://example.com/',
@@ -96,14 +101,16 @@ public function configProvider()
96101
false,
97102
'',
98103
'http://example.com/index.php',
104+
'http://example.com/',
99105
],
100-
[
106+
'baseURL missing /' => [
101107
'http://example.com',
102108
'index.php',
103109
null,
104110
false,
105111
'',
106112
'http://example.com/index.php',
113+
'http://example.com/',
107114
],
108115
[
109116
'http://example.com/',
@@ -112,6 +119,7 @@ public function configProvider()
112119
false,
113120
'',
114121
'http://example.com/',
122+
'http://example.com/',
115123
],
116124
[
117125
'http://example.com/',
@@ -120,6 +128,7 @@ public function configProvider()
120128
false,
121129
'',
122130
'http://example.com/banana.php',
131+
'http://example.com/',
123132
],
124133
[
125134
'http://example.com/',
@@ -128,6 +137,7 @@ public function configProvider()
128137
false,
129138
'abc',
130139
'http://example.com/abc',
140+
'http://example.com/abc',
131141
],
132142
'URL decode' => [
133143
'http://example.com/',
@@ -136,6 +146,7 @@ public function configProvider()
136146
false,
137147
'template/meet-%26-greet',
138148
'http://example.com/template/meet-&-greet',
149+
'http://example.com/template/meet-&-greet',
139150
],
140151
'URL encode' => [
141152
'http://example.com/',
@@ -144,6 +155,7 @@ public function configProvider()
144155
false,
145156
'<s>alert</s>',
146157
'http://example.com/%3Cs%3Ealert%3C/s%3E',
158+
'http://example.com/%3Cs%3Ealert%3C/s%3E',
147159
],
148160
[
149161
'http://example.com/public/',
@@ -152,6 +164,7 @@ public function configProvider()
152164
false,
153165
'',
154166
'http://example.com/public/index.php',
167+
'http://example.com/public/',
155168
],
156169
[
157170
'http://example.com/public/',
@@ -160,6 +173,7 @@ public function configProvider()
160173
false,
161174
'',
162175
'http://example.com/public/',
176+
'http://example.com/public/',
163177
],
164178
[
165179
'http://example.com/public',
@@ -168,6 +182,7 @@ public function configProvider()
168182
false,
169183
'',
170184
'http://example.com/public/',
185+
'http://example.com/public/',
171186
],
172187
[
173188
'http://example.com/public',
@@ -176,6 +191,7 @@ public function configProvider()
176191
false,
177192
'/',
178193
'http://example.com/public/index.php/',
194+
'http://example.com/public/',
179195
],
180196
[
181197
'http://example.com/public/',
@@ -184,6 +200,7 @@ public function configProvider()
184200
false,
185201
'/',
186202
'http://example.com/public/index.php/',
203+
'http://example.com/public/',
187204
],
188205
[
189206
'http://example.com/',
@@ -192,6 +209,7 @@ public function configProvider()
192209
false,
193210
'foo',
194211
'http://example.com/index.php/foo',
212+
'http://example.com/foo',
195213
],
196214
[
197215
'http://example.com/',
@@ -200,6 +218,7 @@ public function configProvider()
200218
false,
201219
'0',
202220
'http://example.com/index.php/0',
221+
'http://example.com/0',
203222
],
204223
[
205224
'http://example.com/public',
@@ -208,6 +227,7 @@ public function configProvider()
208227
false,
209228
'foo',
210229
'http://example.com/public/index.php/foo',
230+
'http://example.com/public/foo',
211231
],
212232
[
213233
'http://example.com/',
@@ -216,6 +236,7 @@ public function configProvider()
216236
false,
217237
'foo?bar=bam',
218238
'http://example.com/index.php/foo?bar=bam',
239+
'http://example.com/foo?bar=bam',
219240
],
220241
[
221242
'http://example.com/',
@@ -224,6 +245,7 @@ public function configProvider()
224245
false,
225246
'test#banana',
226247
'http://example.com/index.php/test#banana',
248+
'http://example.com/test#banana',
227249
],
228250
[
229251
'http://example.com/',
@@ -232,6 +254,7 @@ public function configProvider()
232254
false,
233255
'foo',
234256
'ftp://example.com/index.php/foo',
257+
'ftp://example.com/foo',
235258
],
236259
[
237260
'http://example.com/',
@@ -240,6 +263,7 @@ public function configProvider()
240263
false,
241264
'news/local/123',
242265
'http://example.com/index.php/news/local/123',
266+
'http://example.com/news/local/123',
243267
],
244268
[
245269
'http://example.com/',
@@ -248,6 +272,7 @@ public function configProvider()
248272
false,
249273
['news', 'local', '123'],
250274
'http://example.com/index.php/news/local/123',
275+
'http://example.com/news/local/123',
251276
],
252277
];
253278
}
@@ -267,12 +292,12 @@ public function testBaseURLDiscovery()
267292
$_SERVER['HTTP_HOST'] = 'example.com';
268293
$_SERVER['REQUEST_URI'] = '/test';
269294

270-
$this->assertSame('http://example.com', base_url());
295+
$this->assertSame('http://example.com/', base_url());
271296

272297
$_SERVER['HTTP_HOST'] = 'example.com';
273298
$_SERVER['REQUEST_URI'] = '/test/page';
274299

275-
$this->assertSame('http://example.com', base_url());
300+
$this->assertSame('http://example.com/', base_url());
276301
$this->assertSame('http://example.com/profile', base_url('profile'));
277302
}
278303

0 commit comments

Comments
 (0)