2015-12-10 22:19:38 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-08-22 11:59:59 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func TestFixURL(t *testing.T) {
|
2014-08-22 11:59:59 +00:00
|
|
|
type data struct {
|
|
|
|
TestName string
|
2015-03-11 17:34:57 +00:00
|
|
|
CLIBaseURL string
|
|
|
|
CfgBaseURL string
|
2014-08-22 11:59:59 +00:00
|
|
|
AppendPort bool
|
|
|
|
Port int
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
tests := []data{
|
2015-01-16 10:13:03 +00:00
|
|
|
{"Basic http localhost", "", "http://foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Basic https production, http localhost", "", "https://foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar/"},
|
|
|
|
{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com/"},
|
|
|
|
{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar/"},
|
2016-05-12 23:06:56 +00:00
|
|
|
{"No http", "", "foo.com", true, 1313, "//localhost:1313/"},
|
|
|
|
{"Override configured port", "", "foo.com:2020", true, 1313, "//localhost:1313/"},
|
|
|
|
{"No http production", "foo.com", "foo.com", false, 80, "//foo.com/"},
|
|
|
|
{"No http production with port", "foo.com", "foo.com", true, 2020, "//foo.com:2020/"},
|
|
|
|
{"No config", "", "", true, 1313, "//localhost:1313/"},
|
2014-08-22 11:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2017-02-05 03:20:06 +00:00
|
|
|
v := viper.New()
|
2016-02-05 21:58:17 +00:00
|
|
|
baseURL = test.CLIBaseURL
|
2017-02-05 03:20:06 +00:00
|
|
|
v.Set("baseURL", test.CfgBaseURL)
|
2014-08-22 11:59:59 +00:00
|
|
|
serverAppend = test.AppendPort
|
|
|
|
serverPort = test.Port
|
2017-11-02 07:25:20 +00:00
|
|
|
result, err := fixURL(v, baseURL, serverPort)
|
2014-08-22 11:59:59 +00:00
|
|
|
if err != nil {
|
2015-03-06 13:56:44 +00:00
|
|
|
t.Errorf("Test #%d %s: unexpected error %s", i, test.TestName, err)
|
2014-08-22 11:59:59 +00:00
|
|
|
}
|
|
|
|
if result != test.Result {
|
|
|
|
t.Errorf("Test #%d %s: expected %q, got %q", i, test.TestName, test.Result, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|