<commit_before>// 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.

package commands

import (
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/gohugoio/hugo/config"
	"github.com/gohugoio/hugo/helpers"
	"golang.org/x/net/context"
	"golang.org/x/sync/errgroup"

	qt "github.com/frankban/quicktest"
)

// Issue 9518
func TestServerPanicOnConfigError(t *testing.T) {
	c := qt.New(t)

	config := `
[markup]
[markup.highlight]
linenos='table'
`

	r := runServerTest(c,
		serverTestOptions{
			config: config,
		},
	)

	c.Assert(r.err, qt.IsNotNil)
	c.Assert(r.err.Error(), qt.Contains, "cannot parse 'Highlight.LineNos' as bool:")
}

func TestServer404(t *testing.T) {
	c := qt.New(t)

	r := runServerTest(c,
		serverTestOptions{
			pathsToGet:  []string{"this/does/not/exist"},
			getNumHomes: 1,
		},
	)

	c.Assert(r.err, qt.IsNil)
	pr := r.pathsResults["this/does/not/exist"]
	c.Assert(pr.statusCode, qt.Equals, http.StatusNotFound)
	c.Assert(pr.body, qt.Contains, "404: 404 Page not found|Not Found.")
}

func TestServerPathEncodingIssues(t *testing.T) {
	c := qt.New(t)

	// Issue 10287
	c.Run("Unicode paths", func(c *qt.C) {
		r := runServerTest(c,
			serverTestOptions{
				pathsToGet:  []string{"hügö/"},
				getNumHomes: 1,
			},
		)

		c.Assert(r.err, qt.IsNil)
		c.Assert(r.pathsResults["hügö/"].body, qt.Contains, "This is hügö")
	})

	// Issue 10314
	c.Run("Windows multilingual 404", func(c *qt.C) {
		config := `
baseURL = 'https://example.org/'
title = 'Hugo Forum Topic #40568'

defaultContentLanguageInSubdir = true

[languages.en]
contentDir = 'content/en'
languageCode = 'en-US'
languageName = 'English'
weight = 1

[languages.es]
contentDir = 'content/es'
languageCode = 'es-ES'
languageName = 'Espanol'
weight = 2

[server]
[[server.redirects]]
from = '/en/**'
to = '/en/404.html'
status = 404

[[server.redirects]]
from = '/es/**'
to = '/es/404.html'
status = 404
`
		r := runServerTest(c,
			serverTestOptions{
				config:      config,
				pathsToGet:  []string{"en/this/does/not/exist", "es/this/does/not/exist"},
				getNumHomes: 1,
			},
		)

		c.Assert(r.err, qt.IsNil)
		pr1 := r.pathsResults["en/this/does/not/exist"]
		pr2 := r.pathsResults["es/this/does/not/exist"]
		c.Assert(pr1.statusCode, qt.Equals, http.StatusNotFound)
		c.Assert(pr2.statusCode, qt.Equals, http.StatusNotFound)
		c.Assert(pr1.body, qt.Contains, "404: 404 Page not found|Not Found.")
		c.Assert(pr2.body, qt.Contains, "404: 404 Page not found|Not Found.")

	})

}
func TestServerFlags(t *testing.T) {
	c := qt.New(t)

	assertPublic := func(c *qt.C, r serverTestResult, renderStaticToDisk bool) {
		c.Assert(r.err, qt.IsNil)
		c.Assert(r.homesContent[0], qt.Contains, "Environment: development")
		c.Assert(r.publicDirnames["myfile.txt"], qt.Equals, renderStaticToDisk)

	}

	for _, test := range []struct {
		flag   string
		assert func(c *qt.C, r serverTestResult)
	}{
		{"", func(c *qt.C, r serverTestResult) {
			assertPublic(c, r, false)
		}},
		{"--renderToDisk", func(c *qt.C, r serverTestResult) {
			assertPublic(c, r, true)
		}},
		{"--renderStaticToDisk", func(c *qt.C, r serverTestResult) {
			assertPublic(c, r, true)
		}},
	} {
		c.Run(test.flag, func(c *qt.C) {
			config := `
baseURL="https://example.org"
`

			var args []string
			if test.flag != "" {
				args = strings.Split(test.flag, "=")
			}

			opts := serverTestOptions{
				config:      config,
				args:        args,
				getNumHomes: 1,
			}

			r := runServerTest(c, opts)

			test.assert(c, r)

		})

	}

}

func TestServerBugs(t *testing.T) {
	c := qt.New(t)

	for _, test := range []struct {
		name       string
		config     string
		flag       string
		numservers int
		assert     func(c *qt.C, r serverTestResult)
	}{
		{"PostProcess, memory", "", "", 1, func(c *qt.C, r serverTestResult) {<commit_msg>Skip flakey server tests on GitHub Action on Windows<commit_after>// 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.

package commands

import (
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/gohugoio/hugo/config"
	"github.com/gohugoio/hugo/helpers"
	"github.com/gohugoio/hugo/htesting"
	"golang.org/x/net/context"
	"golang.org/x/sync/errgroup"

	qt "github.com/frankban/quicktest"
)

// Issue 9518
func TestServerPanicOnConfigError(t *testing.T) {
	c := qt.New(t)

	config := `
[markup]
[markup.highlight]
linenos='table'
`

	r := runServerTest(c,
		serverTestOptions{
			config: config,
		},
	)

	c.Assert(r.err, qt.IsNotNil)
	c.Assert(r.err.Error(), qt.Contains, "cannot parse 'Highlight.LineNos' as bool:")
}

func TestServer404(t *testing.T) {
	c := qt.New(t)

	r := runServerTest(c,
		serverTestOptions{
			pathsToGet:  []string{"this/does/not/exist"},
			getNumHomes: 1,
		},
	)

	c.Assert(r.err, qt.IsNil)
	pr := r.pathsResults["this/does/not/exist"]
	c.Assert(pr.statusCode, qt.Equals, http.StatusNotFound)
	c.Assert(pr.body, qt.Contains, "404: 404 Page not found|Not Found.")
}

func TestServerPathEncodingIssues(t *testing.T) {
	c := qt.New(t)

	// Issue 10287
	c.Run("Unicode paths", func(c *qt.C) {
		r := runServerTest(c,
			serverTestOptions{
				pathsToGet:  []string{"hügö/"},
				getNumHomes: 1,
			},
		)

		c.Assert(r.err, qt.IsNil)
		c.Assert(r.pathsResults["hügö/"].body, qt.Contains, "This is hügö")
	})

	// Issue 10314
	c.Run("Windows multilingual 404", func(c *qt.C) {
		config := `
baseURL = 'https://example.org/'
title = 'Hugo Forum Topic #40568'

defaultContentLanguageInSubdir = true

[languages.en]
contentDir = 'content/en'
languageCode = 'en-US'
languageName = 'English'
weight = 1

[languages.es]
contentDir = 'content/es'
languageCode = 'es-ES'
languageName = 'Espanol'
weight = 2

[server]
[[server.redirects]]
from = '/en/**'
to = '/en/404.html'
status = 404

[[server.redirects]]
from = '/es/**'
to = '/es/404.html'
status = 404
`
		r := runServerTest(c,
			serverTestOptions{
				config:      config,
				pathsToGet:  []string{"en/this/does/not/exist", "es/this/does/not/exist"},
				getNumHomes: 1,
			},
		)

		c.Assert(r.err, qt.IsNil)
		pr1 := r.pathsResults["en/this/does/not/exist"]
		pr2 := r.pathsResults["es/this/does/not/exist"]
		c.Assert(pr1.statusCode, qt.Equals, http.StatusNotFound)
		c.Assert(pr2.statusCode, qt.Equals, http.StatusNotFound)
		c.Assert(pr1.body, qt.Contains, "404: 404 Page not found|Not Found.")
		c.Assert(pr2.body, qt.Contains, "404: 404 Page not found|Not Found.")

	})

}
func TestServerFlags(t *testing.T) {
	c := qt.New(t)

	assertPublic := func(c *qt.C, r serverTestResult, renderStaticToDisk bool) {
		c.Assert(r.err, qt.IsNil)
		c.Assert(r.homesContent[0], qt.Contains, "Environment: development")
		c.Assert(r.publicDirnames["myfile.txt"], qt.Equals, renderStaticToDisk)

	}

	for _, test := range []struct {
		flag   string
		assert func(c *qt.C, r serverTestResult)
	}{
		{"", func(c *qt.C, r serverTestResult) {
			assertPublic(c, r, false)
		}},
		{"--renderToDisk", func(c *qt.C, r serverTestResult) {
			assertPublic(c, r, true)
		}},
		{"--renderStaticToDisk", func(c *qt.C, r serverTestResult) {
			assertPublic(c, r, true)
		}},
	} {
		c.Run(test.flag, func(c *qt.C) {
			config := `
baseURL="https://example.org"
`

			var args []string
			if test.flag != "" {
				args = strings.Split(test.flag, "=")
			}

			opts := serverTestOptions{
				config:      config,
				args:        args,
				getNumHomes: 1,
			}

			r := runServerTest(c, opts)

			test.assert(c, r)

		})

	}

}

func TestServerBugs(t *testing.T) {
	// TODO(bep) this is flaky on Windows on GH Actions.
	if htesting.IsGitHubAction() && runtime.GOOS == "windows" {
		t.Skip("skipping on windows")
	}
	c := qt.New(t)

	for _, test := range []struct {
		name       string
		config     string
		flag       string
		numservers int
		assert     func(c *qt.C, r serverTestResult)
	}{
		{"PostProcess, memory", "", "", 1, func(c *qt.C, r serverTestResult) {
{'fraction_non_alphanumeric': 0.16170590848511773, 'fraction_numerical': 0.017103509551310528, 'mean_word_length': 3.504752376188094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1271088', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3399, 'n_words': 783}