resources_test.go (5885B)
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.NewWithTestDefaults() 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.NewWithTestDefaults() 149 cfg.Set("ignoreCache", ignoreCache) 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 ex := hexec.New(security.DefaultConfig) 198 199 logger := loggers.NewIgnorableLogger(loggers.NewErrorLogger(), "none") 200 cs, err := helpers.NewContentSpec(cfg, logger, afero.NewMemMapFs(), ex) 201 if err != nil { 202 panic(err) 203 } 204 205 fs := hugofs.NewMem(cfg) 206 207 p, err := helpers.NewPathSpec(fs, cfg, nil) 208 if err != nil { 209 panic(err) 210 } 211 212 fileCaches, err := filecache.NewCaches(p) 213 if err != nil { 214 panic(err) 215 } 216 217 return &deps.Deps{ 218 Cfg: cfg, 219 Fs: fs, 220 FileCaches: fileCaches, 221 ExecHelper: ex, 222 ContentSpec: cs, 223 Log: logger, 224 LogDistinct: helpers.NewDistinctLogger(logger), 225 } 226 } 227 228 func newTestNs() *Namespace { 229 return New(newDeps(config.NewWithTestDefaults())) 230 }