2015-12-10 17:19:38 -05: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 07:59:59 -04:00
|
|
|
package commands
|
|
|
|
|
2018-04-11 02:55:50 -04:00
|
|
|
import (
|
2018-04-11 03:38:58 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-07-18 03:43:31 -04:00
|
|
|
"runtime"
|
2018-04-11 02:55:50 -04:00
|
|
|
"testing"
|
2018-04-11 03:38:58 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2014-08-22 07:59:59 -04:00
|
|
|
|
2018-04-11 02:55:50 -04:00
|
|
|
"github.com/spf13/viper"
|
2018-04-11 03:38:58 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-11 02:55:50 -04:00
|
|
|
)
|
2014-08-22 07:59:59 -04:00
|
|
|
|
2018-04-11 03:38:58 -04:00
|
|
|
func TestServer(t *testing.T) {
|
2018-07-18 03:43:31 -04:00
|
|
|
if isWindowsCI() {
|
|
|
|
// TODO(bep) not sure why server tests have started to fail on the Windows CI server.
|
|
|
|
t.Skip("Skip server test on appveyor")
|
|
|
|
}
|
2018-04-11 03:38:58 -04:00
|
|
|
assert := require.New(t)
|
|
|
|
dir, err := createSimpleTestSite(t)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// Let us hope that this port is available on all systems ...
|
|
|
|
port := 1331
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
2018-04-12 03:31:53 -04:00
|
|
|
stop := make(chan bool)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
b := newCommandsBuilder()
|
|
|
|
scmd := b.newServerCmdSignaled(stop)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
|
|
|
cmd := scmd.getCommand()
|
|
|
|
cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = cmd.ExecuteC()
|
|
|
|
assert.NoError(err)
|
|
|
|
}()
|
|
|
|
|
2018-04-12 03:31:53 -04:00
|
|
|
// There is no way to know exactly when the server is ready for connections.
|
|
|
|
// We could improve by something like https://golang.org/pkg/net/http/httptest/#Server
|
|
|
|
// But for now, let us sleep and pray!
|
2018-07-18 03:43:31 -04:00
|
|
|
time.Sleep(2 * time.Second)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
|
|
|
resp, err := http.Get("http://localhost:1331/")
|
|
|
|
assert.NoError(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
homeContent := helpers.ReaderToString(resp.Body)
|
|
|
|
|
|
|
|
assert.Contains(homeContent, "List: Hugo Commands")
|
|
|
|
|
|
|
|
// Stop the server.
|
|
|
|
stop <- true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-11 02:55:50 -04:00
|
|
|
func TestFixURL(t *testing.T) {
|
2014-08-22 07:59:59 -04:00
|
|
|
type data struct {
|
|
|
|
TestName string
|
2015-03-11 13:34:57 -04:00
|
|
|
CLIBaseURL string
|
|
|
|
CfgBaseURL string
|
2014-08-22 07:59:59 -04:00
|
|
|
AppendPort bool
|
|
|
|
Port int
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
tests := []data{
|
2015-01-16 05:13:03 -05: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 19:06:56 -04: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 07:59:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2018-04-13 02:42:29 -04:00
|
|
|
b := newCommandsBuilder()
|
|
|
|
s := b.newServerCmd()
|
2017-02-04 22:20:06 -05:00
|
|
|
v := viper.New()
|
2018-04-11 02:55:50 -04:00
|
|
|
baseURL := test.CLIBaseURL
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("baseURL", test.CfgBaseURL)
|
2018-04-11 02:55:50 -04:00
|
|
|
s.serverAppend = test.AppendPort
|
|
|
|
s.serverPort = test.Port
|
|
|
|
result, err := s.fixURL(v, baseURL, s.serverPort)
|
2014-08-22 07:59:59 -04:00
|
|
|
if err != nil {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test #%d %s: unexpected error %s", i, test.TestName, err)
|
2014-08-22 07:59:59 -04:00
|
|
|
}
|
|
|
|
if result != test.Result {
|
|
|
|
t.Errorf("Test #%d %s: expected %q, got %q", i, test.TestName, test.Result, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 03:43:31 -04:00
|
|
|
|
|
|
|
func isWindowsCI() bool {
|
|
|
|
return runtime.GOOS == "windows" && os.Getenv("CI") != ""
|
|
|
|
}
|