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