github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/data/resources_test.go (about)

     1  // Copyright 2016 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 data
    15  
    16  import (
    17  	"bytes"
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"net/url"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/gohugoio/hugo/config/security"
    26  	"github.com/gohugoio/hugo/modules"
    27  
    28  	"github.com/gohugoio/hugo/helpers"
    29  
    30  	qt "github.com/frankban/quicktest"
    31  	"github.com/gohugoio/hugo/cache/filecache"
    32  	"github.com/gohugoio/hugo/common/hexec"
    33  	"github.com/gohugoio/hugo/common/loggers"
    34  	"github.com/gohugoio/hugo/config"
    35  	"github.com/gohugoio/hugo/deps"
    36  	"github.com/gohugoio/hugo/hugofs"
    37  	"github.com/gohugoio/hugo/langs"
    38  	"github.com/spf13/afero"
    39  )
    40  
    41  func TestScpGetLocal(t *testing.T) {
    42  	t.Parallel()
    43  	v := config.New()
    44  	fs := hugofs.NewMem(v)
    45  	ps := helpers.FilePathSeparator
    46  
    47  	tests := []struct {
    48  		path    string
    49  		content []byte
    50  	}{
    51  		{"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
    52  		{"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
    53  		{"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)},
    54  		{"은행", []byte(`T€st C은행ontent 123`)},
    55  		{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
    56  	}
    57  
    58  	for _, test := range tests {
    59  		r := bytes.NewReader(test.content)
    60  		err := helpers.WriteToDisk(test.path, r, fs.Source)
    61  		if err != nil {
    62  			t.Error(err)
    63  		}
    64  
    65  		c, err := getLocal(test.path, fs.Source, v)
    66  		if err != nil {
    67  			t.Errorf("Error getting resource content: %s", err)
    68  		}
    69  		if !bytes.Equal(c, test.content) {
    70  			t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
    71  		}
    72  	}
    73  }
    74  
    75  func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) {
    76  	testServer := httptest.NewServer(http.HandlerFunc(handler))
    77  	client := &http.Client{
    78  		Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
    79  			// Remove when https://github.com/golang/go/issues/13686 is fixed
    80  			r.Host = "gohugo.io"
    81  			return url.Parse(testServer.URL)
    82  		}},
    83  	}
    84  	return testServer, client
    85  }
    86  
    87  func TestScpGetRemote(t *testing.T) {
    88  	t.Parallel()
    89  	c := qt.New(t)
    90  	fs := new(afero.MemMapFs)
    91  	cache := filecache.NewCache(fs, 100, "")
    92  
    93  	tests := []struct {
    94  		path    string
    95  		content []byte
    96  	}{
    97  		{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)},
    98  		{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`)},
    99  		{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`)},
   100  		{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`)},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		msg := qt.Commentf("%v", test)
   105  
   106  		req, err := http.NewRequest("GET", test.path, nil)
   107  		c.Assert(err, qt.IsNil, msg)
   108  
   109  		srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
   110  			w.Write(test.content)
   111  		})
   112  		defer func() { srv.Close() }()
   113  
   114  		ns := newTestNs()
   115  		ns.client = cl
   116  
   117  		var cb []byte
   118  		f := func(b []byte) (bool, error) {
   119  			cb = b
   120  			return false, nil
   121  		}
   122  
   123  		err = ns.getRemote(cache, f, req)
   124  		c.Assert(err, qt.IsNil, msg)
   125  		c.Assert(string(cb), qt.Equals, string(test.content))
   126  
   127  		c.Assert(string(cb), qt.Equals, string(test.content))
   128  
   129  	}
   130  }
   131  
   132  func TestScpGetRemoteParallel(t *testing.T) {
   133  	t.Parallel()
   134  	c := qt.New(t)
   135  
   136  	content := []byte(`T€st Content 123`)
   137  	srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
   138  		w.Write(content)
   139  	})
   140  
   141  	defer func() { srv.Close() }()
   142  
   143  	url := "http://Foo.Bar/foo_Bar-Foo"
   144  	req, err := http.NewRequest("GET", url, nil)
   145  	c.Assert(err, qt.IsNil)
   146  
   147  	for _, ignoreCache := range []bool{false} {
   148  		cfg := config.New()
   149  		cfg.Set("ignoreCache", ignoreCache)
   150  		cfg.Set("contentDir", "content")
   151  
   152  		ns := New(newDeps(cfg))
   153  		ns.client = cl
   154  
   155  		var wg sync.WaitGroup
   156  
   157  		for i := 0; i < 1; i++ {
   158  			wg.Add(1)
   159  			go func(gor int) {
   160  				defer wg.Done()
   161  				for j := 0; j < 10; j++ {
   162  					var cb []byte
   163  					f := func(b []byte) (bool, error) {
   164  						cb = b
   165  						return false, nil
   166  					}
   167  					err := ns.getRemote(ns.cacheGetJSON, f, req)
   168  
   169  					c.Assert(err, qt.IsNil)
   170  					if string(content) != string(cb) {
   171  						t.Errorf("expected\n%q\ngot\n%q", content, cb)
   172  					}
   173  
   174  					time.Sleep(23 * time.Millisecond)
   175  				}
   176  			}(i)
   177  		}
   178  
   179  		wg.Wait()
   180  	}
   181  }
   182  
   183  func newDeps(cfg config.Provider) *deps.Deps {
   184  	cfg.Set("resourceDir", "resources")
   185  	cfg.Set("dataDir", "resources")
   186  	cfg.Set("i18nDir", "i18n")
   187  	cfg.Set("assetDir", "assets")
   188  	cfg.Set("layoutDir", "layouts")
   189  	cfg.Set("archetypeDir", "archetypes")
   190  
   191  	langs.LoadLanguageSettings(cfg, nil)
   192  	mod, err := modules.CreateProjectModule(cfg)
   193  	if err != nil {
   194  		panic(err)
   195  	}
   196  	cfg.Set("allModules", modules.Modules{mod})
   197  
   198  	ex := hexec.New(security.DefaultConfig)
   199  
   200  	logger := loggers.NewIgnorableLogger(loggers.NewErrorLogger(), "none")
   201  	cs, err := helpers.NewContentSpec(cfg, logger, afero.NewMemMapFs(), ex)
   202  	if err != nil {
   203  		panic(err)
   204  	}
   205  
   206  	fs := hugofs.NewMem(cfg)
   207  
   208  	p, err := helpers.NewPathSpec(fs, cfg, nil)
   209  	if err != nil {
   210  		panic(err)
   211  	}
   212  
   213  	fileCaches, err := filecache.NewCaches(p)
   214  	if err != nil {
   215  		panic(err)
   216  	}
   217  
   218  	return &deps.Deps{
   219  		Cfg:         cfg,
   220  		Fs:          fs,
   221  		FileCaches:  fileCaches,
   222  		ExecHelper:  ex,
   223  		ContentSpec: cs,
   224  		Log:         logger,
   225  		LogDistinct: helpers.NewDistinctLogger(logger),
   226  	}
   227  }
   228  
   229  func newTestNs() *Namespace {
   230  	v := config.New()
   231  	v.Set("contentDir", "content")
   232  	return New(newDeps(v))
   233  }