github.com/stackb/hugo@v0.47.1/hugolib/paths/baseURL_test.go (about) 1 // Copyright 2018 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package paths 15 16 import ( 17 "testing" 18 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestBaseURL(t *testing.T) { 23 b, err := newBaseURLFromString("http://example.com") 24 require.NoError(t, err) 25 require.Equal(t, "http://example.com", b.String()) 26 27 p, err := b.WithProtocol("webcal://") 28 require.NoError(t, err) 29 require.Equal(t, "webcal://example.com", p) 30 31 p, err = b.WithProtocol("webcal") 32 require.NoError(t, err) 33 require.Equal(t, "webcal://example.com", p) 34 35 _, err = b.WithProtocol("mailto:") 36 require.Error(t, err) 37 38 b, err = newBaseURLFromString("mailto:hugo@rules.com") 39 require.NoError(t, err) 40 require.Equal(t, "mailto:hugo@rules.com", b.String()) 41 42 // These are pretty constructed 43 p, err = b.WithProtocol("webcal") 44 require.NoError(t, err) 45 require.Equal(t, "webcal:hugo@rules.com", p) 46 47 p, err = b.WithProtocol("webcal://") 48 require.NoError(t, err) 49 require.Equal(t, "webcal://hugo@rules.com", p) 50 51 // Test with "non-URLs". Some people will try to use these as a way to get 52 // relative URLs working etc. 53 b, err = newBaseURLFromString("/") 54 require.NoError(t, err) 55 require.Equal(t, "/", b.String()) 56 57 b, err = newBaseURLFromString("") 58 require.NoError(t, err) 59 require.Equal(t, "", b.String()) 60 61 // BaseURL with sub path 62 b, err = newBaseURLFromString("http://example.com/sub") 63 require.NoError(t, err) 64 require.Equal(t, "http://example.com/sub", b.String()) 65 require.Equal(t, "http://example.com", b.HostURL()) 66 }