github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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  
    19  	"github.com/gohugoio/hugo/common/loggers"
    20  
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"net/url"
    24  	"path/filepath"
    25  	"sync"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/gohugoio/hugo/config/testconfig"
    30  
    31  	"github.com/gohugoio/hugo/helpers"
    32  
    33  	qt "github.com/frankban/quicktest"
    34  	"github.com/gohugoio/hugo/cache/filecache"
    35  	"github.com/gohugoio/hugo/config"
    36  	"github.com/gohugoio/hugo/deps"
    37  	"github.com/gohugoio/hugo/hugofs"
    38  	"github.com/spf13/afero"
    39  )
    40  
    41  func TestScpGetLocal(t *testing.T) {
    42  	t.Parallel()
    43  	v := config.New()
    44  	workingDir := "/my/working/dir"
    45  	v.Set("workingDir", workingDir)
    46  	v.Set("publishDir", "public")
    47  	fs := hugofs.NewFromOld(afero.NewMemMapFs(), v)
    48  	ps := helpers.FilePathSeparator
    49  
    50  	tests := []struct {
    51  		path    string
    52  		content []byte
    53  	}{
    54  		{"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
    55  		{"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
    56  		{"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)},
    57  		{"은행", []byte(`T€st C은행ontent 123`)},
    58  		{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
    59  	}
    60  
    61  	for _, test := range tests {
    62  		r := bytes.NewReader(test.content)
    63  		err := helpers.WriteToDisk(filepath.Join(workingDir, test.path), r, fs.Source)
    64  		if err != nil {
    65  			t.Error(err)
    66  		}
    67  
    68  		c, err := getLocal(workingDir, test.path, fs.Source)
    69  		if err != nil {
    70  			t.Errorf("Error getting resource content: %s", err)
    71  		}
    72  		if !bytes.Equal(c, test.content) {
    73  			t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
    74  		}
    75  	}
    76  }
    77  
    78  func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) {
    79  	testServer := httptest.NewServer(http.HandlerFunc(handler))
    80  	client := &http.Client{
    81  		Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
    82  			// Remove when https://github.com/golang/go/issues/13686 is fixed
    83  			r.Host = "gohugo.io"
    84  			return url.Parse(testServer.URL)
    85  		}},
    86  	}
    87  	return testServer, client
    88  }
    89  
    90  func TestScpGetRemote(t *testing.T) {
    91  	t.Parallel()
    92  	c := qt.New(t)
    93  	fs := new(afero.MemMapFs)
    94  	cache := filecache.NewCache(fs, 100, "")
    95  
    96  	tests := []struct {
    97  		path    string
    98  		content []byte
    99  	}{
   100  		{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)},
   101  		{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`)},
   102  		{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`)},
   103  		{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`)},
   104  	}
   105  
   106  	for _, test := range tests {
   107  		msg := qt.Commentf("%v", test)
   108  
   109  		req, err := http.NewRequest("GET", test.path, nil)
   110  		c.Assert(err, qt.IsNil, msg)
   111  
   112  		srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
   113  			w.Write(test.content)
   114  		})
   115  		defer func() { srv.Close() }()
   116  
   117  		ns := newTestNs()
   118  		ns.client = cl
   119  
   120  		var cb []byte
   121  		f := func(b []byte) (bool, error) {
   122  			cb = b
   123  			return false, nil
   124  		}
   125  
   126  		err = ns.getRemote(cache, f, req)
   127  		c.Assert(err, qt.IsNil, msg)
   128  		c.Assert(string(cb), qt.Equals, string(test.content))
   129  
   130  		c.Assert(string(cb), qt.Equals, string(test.content))
   131  
   132  	}
   133  }
   134  
   135  func TestScpGetRemoteParallel(t *testing.T) {
   136  	t.Parallel()
   137  	c := qt.New(t)
   138  
   139  	content := []byte(`T€st Content 123`)
   140  	srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
   141  		w.Write(content)
   142  	})
   143  
   144  	defer func() { srv.Close() }()
   145  
   146  	url := "http://Foo.Bar/foo_Bar-Foo"
   147  	req, err := http.NewRequest("GET", url, nil)
   148  	c.Assert(err, qt.IsNil)
   149  
   150  	for _, ignoreCache := range []bool{false} {
   151  		cfg := config.New()
   152  		cfg.Set("ignoreCache", ignoreCache)
   153  
   154  		ns := New(newDeps(cfg))
   155  		ns.client = cl
   156  
   157  		var wg sync.WaitGroup
   158  
   159  		for i := 0; i < 1; i++ {
   160  			wg.Add(1)
   161  			go func(gor int) {
   162  				defer wg.Done()
   163  				for j := 0; j < 10; j++ {
   164  					var cb []byte
   165  					f := func(b []byte) (bool, error) {
   166  						cb = b
   167  						return false, nil
   168  					}
   169  					err := ns.getRemote(ns.cacheGetJSON, f, req)
   170  
   171  					c.Assert(err, qt.IsNil)
   172  					if string(content) != string(cb) {
   173  						t.Errorf("expected\n%q\ngot\n%q", content, cb)
   174  					}
   175  
   176  					time.Sleep(23 * time.Millisecond)
   177  				}
   178  			}(i)
   179  		}
   180  
   181  		wg.Wait()
   182  	}
   183  }
   184  
   185  func newDeps(cfg config.Provider) *deps.Deps {
   186  	conf := testconfig.GetTestConfig(nil, cfg)
   187  	logger := loggers.NewDefault()
   188  	fs := hugofs.NewFrom(afero.NewMemMapFs(), conf.BaseConfig())
   189  
   190  	d := &deps.Deps{
   191  		Fs:   fs,
   192  		Log:  logger,
   193  		Conf: conf,
   194  	}
   195  	if err := d.Init(); err != nil {
   196  		panic(err)
   197  	}
   198  	return d
   199  }
   200  
   201  func newTestNs() *Namespace {
   202  	return New(newDeps(config.New()))
   203  }