summaryrefslogtreecommitdiffstats
path: root/common/urls
diff options
context:
space:
mode:
Diffstat (limited to 'common/urls')
-rw-r--r--common/urls/baseURL.go28
-rw-r--r--common/urls/baseURL_test.go28
2 files changed, 36 insertions, 20 deletions
diff --git a/common/urls/baseURL.go b/common/urls/baseURL.go
index df26730ec..2958a2a04 100644
--- a/common/urls/baseURL.go
+++ b/common/urls/baseURL.go
@@ -1,4 +1,4 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,10 +23,12 @@ import (
// A BaseURL in Hugo is normally on the form scheme://path, but the
// form scheme: is also valid (mailto:hugo@rules.com).
type BaseURL struct {
- url *url.URL
- WithPath string
- WithoutPath string
- BasePath string
+ url *url.URL
+ WithPath string
+ WithPathNoTrailingSlash string
+ WithoutPath string
+ BasePath string
+ BasePathNoTrailingSlash string
}
func (b BaseURL) String() string {
@@ -92,19 +94,19 @@ func NewBaseURLFromString(b string) (BaseURL, error) {
return BaseURL{}, err
}
return newBaseURLFromURL(u)
-
}
func newBaseURLFromURL(u *url.URL) (BaseURL, error) {
- baseURL := BaseURL{url: u, WithPath: u.String()}
- var baseURLNoPath = baseURL.URL()
+ // A baseURL should always have a trailing slash, see #11669.
+ if !strings.HasSuffix(u.Path, "/") {
+ u.Path += "/"
+ }
+ baseURL := BaseURL{url: u, WithPath: u.String(), WithPathNoTrailingSlash: strings.TrimSuffix(u.String(), "/")}
+ baseURLNoPath := baseURL.URL()
baseURLNoPath.Path = ""
baseURL.WithoutPath = baseURLNoPath.String()
-
- basePath := u.Path
- if basePath != "" && basePath != "/" {
- baseURL.BasePath = basePath
- }
+ baseURL.BasePath = u.Path
+ baseURL.BasePathNoTrailingSlash = strings.TrimSuffix(u.Path, "/")
return baseURL, nil
}
diff --git a/common/urls/baseURL_test.go b/common/urls/baseURL_test.go
index 95dc73339..ba337aac8 100644
--- a/common/urls/baseURL_test.go
+++ b/common/urls/baseURL_test.go
@@ -1,4 +1,4 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,17 +21,24 @@ import (
func TestBaseURL(t *testing.T) {
c := qt.New(t)
- b, err := NewBaseURLFromString("http://example.com")
+
+ b, err := NewBaseURLFromString("http://example.com/")
+ c.Assert(err, qt.IsNil)
+ c.Assert(b.String(), qt.Equals, "http://example.com/")
+
+ b, err = NewBaseURLFromString("http://example.com")
c.Assert(err, qt.IsNil)
- c.Assert(b.String(), qt.Equals, "http://example.com")
+ c.Assert(b.String(), qt.Equals, "http://example.com/")
+ c.Assert(b.WithPathNoTrailingSlash, qt.Equals, "http://example.com")
+ c.Assert(b.BasePath, qt.Equals, "/")
p, err := b.WithProtocol("webcal://")
c.Assert(err, qt.IsNil)
- c.Assert(p.String(), qt.Equals, "webcal://example.com")
+ c.Assert(p.String(), qt.Equals, "webcal://example.com/")
p, err = b.WithProtocol("webcal")
c.Assert(err, qt.IsNil)
- c.Assert(p.String(), qt.Equals, "webcal://example.com")
+ c.Assert(p.String(), qt.Equals, "webcal://example.com/")
_, err = b.WithProtocol("mailto:")
c.Assert(err, qt.Not(qt.IsNil))
@@ -57,11 +64,18 @@ func TestBaseURL(t *testing.T) {
b, err = NewBaseURLFromString("")
c.Assert(err, qt.IsNil)
- c.Assert(b.String(), qt.Equals, "")
+ c.Assert(b.String(), qt.Equals, "/")
// BaseURL with sub path
b, err = NewBaseURLFromString("http://example.com/sub")
c.Assert(err, qt.IsNil)
- c.Assert(b.String(), qt.Equals, "http://example.com/sub")
+ c.Assert(b.String(), qt.Equals, "http://example.com/sub/")
+ c.Assert(b.WithPathNoTrailingSlash, qt.Equals, "http://example.com/sub")
+ c.Assert(b.BasePath, qt.Equals, "/sub/")
+ c.Assert(b.BasePathNoTrailingSlash, qt.Equals, "/sub")
+
+ b, err = NewBaseURLFromString("http://example.com/sub/")
+ c.Assert(err, qt.IsNil)
+ c.Assert(b.String(), qt.Equals, "http://example.com/sub/")
c.Assert(b.HostURL(), qt.Equals, "http://example.com")
}