github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/config/commonConfig_test.go (about)

     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/loggers"
    22  	"github.com/gohugoio/hugo/common/types"
    23  
    24  	qt "github.com/frankban/quicktest"
    25  )
    26  
    27  func TestBuild(t *testing.T) {
    28  	c := qt.New(t)
    29  
    30  	v := New()
    31  	v.Set("build", map[string]any{
    32  		"useResourceCacheWhen": "always",
    33  	})
    34  
    35  	b := DecodeBuildConfig(v)
    36  
    37  	c.Assert(b.UseResourceCacheWhen, qt.Equals, "always")
    38  
    39  	v.Set("build", map[string]any{
    40  		"useResourceCacheWhen": "foo",
    41  	})
    42  
    43  	b = DecodeBuildConfig(v)
    44  
    45  	c.Assert(b.UseResourceCacheWhen, qt.Equals, "fallback")
    46  
    47  	c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, true)
    48  	c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, false)
    49  
    50  	b.UseResourceCacheWhen = "always"
    51  	c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, true)
    52  	c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, true)
    53  	c.Assert(b.UseResourceCache(nil), qt.Equals, true)
    54  
    55  	b.UseResourceCacheWhen = "never"
    56  	c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, false)
    57  	c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, false)
    58  	c.Assert(b.UseResourceCache(nil), qt.Equals, false)
    59  }
    60  
    61  func TestServer(t *testing.T) {
    62  	c := qt.New(t)
    63  
    64  	cfg, err := FromConfigString(`[[server.headers]]
    65  for = "/*.jpg"
    66  
    67  [server.headers.values]
    68  X-Frame-Options = "DENY"
    69  X-XSS-Protection = "1; mode=block"
    70  X-Content-Type-Options = "nosniff"
    71  
    72  [[server.redirects]]
    73  from = "/foo/**"
    74  to = "/foo/index.html"
    75  status = 200
    76  
    77  [[server.redirects]]
    78  from = "/google/**"
    79  to = "https://google.com/"
    80  status = 301
    81  
    82  [[server.redirects]]
    83  from = "/**"
    84  to = "/default/index.html"
    85  status = 301
    86  
    87  
    88  
    89  `, "toml")
    90  
    91  	c.Assert(err, qt.IsNil)
    92  
    93  	s, err := DecodeServer(cfg)
    94  	c.Assert(err, qt.IsNil)
    95  	c.Assert(s.CompileConfig(loggers.NewDefault()), qt.IsNil)
    96  
    97  	c.Assert(s.MatchHeaders("/foo.jpg"), qt.DeepEquals, []types.KeyValueStr{
    98  		{Key: "X-Content-Type-Options", Value: "nosniff"},
    99  		{Key: "X-Frame-Options", Value: "DENY"},
   100  		{Key: "X-XSS-Protection", Value: "1; mode=block"},
   101  	})
   102  
   103  	c.Assert(s.MatchRedirect("/foo/bar/baz"), qt.DeepEquals, Redirect{
   104  		From:   "/foo/**",
   105  		To:     "/foo/",
   106  		Status: 200,
   107  	})
   108  
   109  	c.Assert(s.MatchRedirect("/someother"), qt.DeepEquals, Redirect{
   110  		From:   "/**",
   111  		To:     "/default/",
   112  		Status: 301,
   113  	})
   114  
   115  	c.Assert(s.MatchRedirect("/google/foo"), qt.DeepEquals, Redirect{
   116  		From:   "/google/**",
   117  		To:     "https://google.com/",
   118  		Status: 301,
   119  	})
   120  
   121  	// No redirect loop, please.
   122  	c.Assert(s.MatchRedirect("/default/index.html"), qt.DeepEquals, Redirect{})
   123  	c.Assert(s.MatchRedirect("/default/"), qt.DeepEquals, Redirect{})
   124  
   125  	for _, errorCase := range []string{
   126  		`[[server.redirects]]
   127  from = "/**"
   128  to = "/file"
   129  status = 301`,
   130  		`[[server.redirects]]
   131  from = "/**"
   132  to = "/foo/file.html"
   133  status = 301`,
   134  	} {
   135  
   136  		cfg, err := FromConfigString(errorCase, "toml")
   137  		c.Assert(err, qt.IsNil)
   138  		_, err = DecodeServer(cfg)
   139  		c.Assert(err, qt.Not(qt.IsNil))
   140  
   141  	}
   142  }
   143  
   144  func TestBuildConfigCacheBusters(t *testing.T) {
   145  	c := qt.New(t)
   146  	cfg := New()
   147  	conf := DecodeBuildConfig(cfg)
   148  	l := loggers.NewDefault()
   149  	c.Assert(conf.CompileConfig(l), qt.IsNil)
   150  
   151  	m, err := conf.MatchCacheBuster(l, "assets/foo/main.js")
   152  	c.Assert(err, qt.IsNil)
   153  	c.Assert(m, qt.IsNotNil)
   154  	c.Assert(m("scripts"), qt.IsTrue)
   155  	c.Assert(m("asdf"), qt.IsFalse)
   156  
   157  	m, _ = conf.MatchCacheBuster(l, "tailwind.config.js")
   158  	c.Assert(m("css"), qt.IsTrue)
   159  	c.Assert(m("js"), qt.IsFalse)
   160  
   161  	m, err = conf.MatchCacheBuster(l, "assets/foo.json")
   162  	c.Assert(err, qt.IsNil)
   163  	c.Assert(m, qt.IsNotNil)
   164  	c.Assert(m("json"), qt.IsTrue)
   165  
   166  }
   167  
   168  func TestBuildConfigCacheBusterstTailwindSetup(t *testing.T) {
   169  	c := qt.New(t)
   170  	cfg := New()
   171  	cfg.Set("build", map[string]interface{}{
   172  		"cacheBusters": []map[string]string{
   173  			{
   174  				"source": "assets/watching/hugo_stats\\.json",
   175  				"target": "css",
   176  			},
   177  			{
   178  				"source": "(postcss|tailwind)\\.config\\.js",
   179  				"target": "css",
   180  			},
   181  			{
   182  				"source": "assets/.*\\.(js|ts|jsx|tsx)",
   183  				"target": "js",
   184  			},
   185  			{
   186  				"source": "assets/.*\\.(.*)$",
   187  				"target": "$1",
   188  			},
   189  		},
   190  	})
   191  
   192  	conf := DecodeBuildConfig(cfg)
   193  	l := loggers.NewDefault()
   194  	c.Assert(conf.CompileConfig(l), qt.IsNil)
   195  
   196  	m, err := conf.MatchCacheBuster(l, "assets/watching/hugo_stats.json")
   197  	c.Assert(err, qt.IsNil)
   198  	c.Assert(m("css"), qt.IsTrue)
   199  }