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