hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

commonConfig_test.go (3447B)

    1 // Copyright 2020 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 config
   15 
   16 import (
   17 	"errors"
   18 	"testing"
   19 
   20 	"github.com/gohugoio/hugo/common/herrors"
   21 	"github.com/gohugoio/hugo/common/types"
   22 
   23 	qt "github.com/frankban/quicktest"
   24 )
   25 
   26 func TestBuild(t *testing.T) {
   27 	c := qt.New(t)
   28 
   29 	v := New()
   30 	v.Set("build", map[string]any{
   31 		"useResourceCacheWhen": "always",
   32 	})
   33 
   34 	b := DecodeBuild(v)
   35 
   36 	c.Assert(b.UseResourceCacheWhen, qt.Equals, "always")
   37 
   38 	v.Set("build", map[string]any{
   39 		"useResourceCacheWhen": "foo",
   40 	})
   41 
   42 	b = DecodeBuild(v)
   43 
   44 	c.Assert(b.UseResourceCacheWhen, qt.Equals, "fallback")
   45 
   46 	c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, true)
   47 	c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, false)
   48 
   49 	b.UseResourceCacheWhen = "always"
   50 	c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, true)
   51 	c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, true)
   52 	c.Assert(b.UseResourceCache(nil), qt.Equals, true)
   53 
   54 	b.UseResourceCacheWhen = "never"
   55 	c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, false)
   56 	c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, false)
   57 	c.Assert(b.UseResourceCache(nil), qt.Equals, false)
   58 }
   59 
   60 func TestServer(t *testing.T) {
   61 	c := qt.New(t)
   62 
   63 	cfg, err := FromConfigString(`[[server.headers]]
   64 for = "/*.jpg"
   65 
   66 [server.headers.values]
   67 X-Frame-Options = "DENY"
   68 X-XSS-Protection = "1; mode=block"
   69 X-Content-Type-Options = "nosniff"
   70 
   71 [[server.redirects]]
   72 from = "/foo/**"
   73 to = "/foo/index.html"
   74 status = 200
   75 
   76 [[server.redirects]]
   77 from = "/google/**"
   78 to = "https://google.com/"
   79 status = 301
   80 
   81 [[server.redirects]]
   82 from = "/**"
   83 to = "/default/index.html"
   84 status = 301
   85 
   86 
   87 
   88 `, "toml")
   89 
   90 	c.Assert(err, qt.IsNil)
   91 
   92 	s, err := DecodeServer(cfg)
   93 	c.Assert(err, qt.IsNil)
   94 
   95 	c.Assert(s.MatchHeaders("/foo.jpg"), qt.DeepEquals, []types.KeyValueStr{
   96 		{Key: "X-Content-Type-Options", Value: "nosniff"},
   97 		{Key: "X-Frame-Options", Value: "DENY"},
   98 		{Key: "X-XSS-Protection", Value: "1; mode=block"},
   99 	})
  100 
  101 	c.Assert(s.MatchRedirect("/foo/bar/baz"), qt.DeepEquals, Redirect{
  102 		From:   "/foo/**",
  103 		To:     "/foo/",
  104 		Status: 200,
  105 	})
  106 
  107 	c.Assert(s.MatchRedirect("/someother"), qt.DeepEquals, Redirect{
  108 		From:   "/**",
  109 		To:     "/default/",
  110 		Status: 301,
  111 	})
  112 
  113 	c.Assert(s.MatchRedirect("/google/foo"), qt.DeepEquals, Redirect{
  114 		From:   "/google/**",
  115 		To:     "https://google.com/",
  116 		Status: 301,
  117 	})
  118 
  119 	// No redirect loop, please.
  120 	c.Assert(s.MatchRedirect("/default/index.html"), qt.DeepEquals, Redirect{})
  121 	c.Assert(s.MatchRedirect("/default/"), qt.DeepEquals, Redirect{})
  122 
  123 	for _, errorCase := range []string{
  124 		`[[server.redirects]]
  125 from = "/**"
  126 to = "/file"
  127 status = 301`,
  128 		`[[server.redirects]]
  129 from = "/**"
  130 to = "/foo/file.html"
  131 status = 301`,
  132 	} {
  133 
  134 		cfg, err := FromConfigString(errorCase, "toml")
  135 		c.Assert(err, qt.IsNil)
  136 		_, err = DecodeServer(cfg)
  137 		c.Assert(err, qt.Not(qt.IsNil))
  138 
  139 	}
  140 }