github.com/whatlly/hugo@v0.47.1/commands/server_test.go (about)

     1  // Copyright 2015 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 commands
    15  
    16  import (
    17  	"fmt"
    18  	"net/http"
    19  	"os"
    20  	"runtime"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/gohugoio/hugo/helpers"
    25  
    26  	"github.com/spf13/viper"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestServer(t *testing.T) {
    31  	if isWindowsCI() {
    32  		// TODO(bep) not sure why server tests have started to fail on the Windows CI server.
    33  		t.Skip("Skip server test on appveyor")
    34  	}
    35  	assert := require.New(t)
    36  	dir, err := createSimpleTestSite(t)
    37  	assert.NoError(err)
    38  
    39  	// Let us hope that this port is available on all systems ...
    40  	port := 1331
    41  
    42  	defer func() {
    43  		os.RemoveAll(dir)
    44  	}()
    45  
    46  	stop := make(chan bool)
    47  
    48  	b := newCommandsBuilder()
    49  	scmd := b.newServerCmdSignaled(stop)
    50  
    51  	cmd := scmd.getCommand()
    52  	cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
    53  
    54  	go func() {
    55  		_, err = cmd.ExecuteC()
    56  		assert.NoError(err)
    57  	}()
    58  
    59  	// There is no way to know exactly when the server is ready for connections.
    60  	// We could improve by something like https://golang.org/pkg/net/http/httptest/#Server
    61  	// But for now, let us sleep and pray!
    62  	time.Sleep(2 * time.Second)
    63  
    64  	resp, err := http.Get("http://localhost:1331/")
    65  	assert.NoError(err)
    66  	defer resp.Body.Close()
    67  	homeContent := helpers.ReaderToString(resp.Body)
    68  
    69  	assert.Contains(homeContent, "List: Hugo Commands")
    70  
    71  	// Stop the server.
    72  	stop <- true
    73  
    74  }
    75  
    76  func TestFixURL(t *testing.T) {
    77  	type data struct {
    78  		TestName   string
    79  		CLIBaseURL string
    80  		CfgBaseURL string
    81  		AppendPort bool
    82  		Port       int
    83  		Result     string
    84  	}
    85  	tests := []data{
    86  		{"Basic http localhost", "", "http://foo.com", true, 1313, "http://localhost:1313/"},
    87  		{"Basic https production, http localhost", "", "https://foo.com", true, 1313, "http://localhost:1313/"},
    88  		{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar/"},
    89  		{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com/"},
    90  		{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar/"},
    91  		{"No http", "", "foo.com", true, 1313, "//localhost:1313/"},
    92  		{"Override configured port", "", "foo.com:2020", true, 1313, "//localhost:1313/"},
    93  		{"No http production", "foo.com", "foo.com", false, 80, "//foo.com/"},
    94  		{"No http production with port", "foo.com", "foo.com", true, 2020, "//foo.com:2020/"},
    95  		{"No config", "", "", true, 1313, "//localhost:1313/"},
    96  	}
    97  
    98  	for i, test := range tests {
    99  		b := newCommandsBuilder()
   100  		s := b.newServerCmd()
   101  		v := viper.New()
   102  		baseURL := test.CLIBaseURL
   103  		v.Set("baseURL", test.CfgBaseURL)
   104  		s.serverAppend = test.AppendPort
   105  		s.serverPort = test.Port
   106  		result, err := s.fixURL(v, baseURL, s.serverPort)
   107  		if err != nil {
   108  			t.Errorf("Test #%d %s: unexpected error %s", i, test.TestName, err)
   109  		}
   110  		if result != test.Result {
   111  			t.Errorf("Test #%d %s: expected %q, got %q", i, test.TestName, test.Result, result)
   112  		}
   113  	}
   114  }
   115  
   116  func isWindowsCI() bool {
   117  	return runtime.GOOS == "windows" && os.Getenv("CI") != ""
   118  }