github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/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 "fmt" 19 "net/http" 20 "net/http/httptest" 21 "net/url" 22 "sync" 23 "testing" 24 "time" 25 26 "github.com/gohugoio/hugo/config" 27 "github.com/gohugoio/hugo/deps" 28 "github.com/gohugoio/hugo/helpers" 29 "github.com/gohugoio/hugo/hugofs" 30 "github.com/gohugoio/hugo/langs" 31 "github.com/spf13/afero" 32 "github.com/spf13/viper" 33 "github.com/stretchr/testify/assert" 34 "github.com/stretchr/testify/require" 35 ) 36 37 func TestScpGetLocal(t *testing.T) { 38 t.Parallel() 39 v := viper.New() 40 fs := hugofs.NewMem(v) 41 ps := helpers.FilePathSeparator 42 43 tests := []struct { 44 path string 45 content []byte 46 }{ 47 {"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)}, 48 {"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)}, 49 {"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)}, 50 {"은행", []byte(`T€st C은행ontent 123`)}, 51 {"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)}, 52 } 53 54 for _, test := range tests { 55 r := bytes.NewReader(test.content) 56 err := helpers.WriteToDisk(test.path, r, fs.Source) 57 if err != nil { 58 t.Error(err) 59 } 60 61 c, err := getLocal(test.path, fs.Source, v) 62 if err != nil { 63 t.Errorf("Error getting resource content: %s", err) 64 } 65 if !bytes.Equal(c, test.content) { 66 t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c)) 67 } 68 } 69 70 } 71 72 func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) { 73 testServer := httptest.NewServer(http.HandlerFunc(handler)) 74 client := &http.Client{ 75 Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) { 76 // Remove when https://github.com/golang/go/issues/13686 is fixed 77 r.Host = "gohugo.io" 78 return url.Parse(testServer.URL) 79 }}, 80 } 81 return testServer, client 82 } 83 84 func TestScpGetRemote(t *testing.T) { 85 t.Parallel() 86 fs := new(afero.MemMapFs) 87 88 tests := []struct { 89 path string 90 content []byte 91 ignore bool 92 }{ 93 {"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false}, 94 {"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`), false}, 95 {"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`), false}, 96 {"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`), true}, 97 } 98 99 for _, test := range tests { 100 msg := fmt.Sprintf("%v", test) 101 102 req, err := http.NewRequest("GET", test.path, nil) 103 require.NoError(t, err, msg) 104 105 srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) { 106 w.Write(test.content) 107 }) 108 defer func() { srv.Close() }() 109 110 cfg := viper.New() 111 112 c, err := getRemote(req, fs, cfg, cl) 113 require.NoError(t, err, msg) 114 assert.Equal(t, string(test.content), string(c)) 115 116 c, err = getCache(req.URL.String(), fs, cfg, test.ignore) 117 require.NoError(t, err, msg) 118 119 if test.ignore { 120 assert.Empty(t, c, msg) 121 } else { 122 assert.Equal(t, string(test.content), string(c)) 123 124 } 125 } 126 } 127 128 func TestScpGetRemoteParallel(t *testing.T) { 129 t.Parallel() 130 131 ns := newTestNs() 132 133 content := []byte(`T€st Content 123`) 134 srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) { 135 w.Write(content) 136 }) 137 defer func() { srv.Close() }() 138 139 url := "http://Foo.Bar/foo_Bar-Foo" 140 req, err := http.NewRequest("GET", url, nil) 141 require.NoError(t, err) 142 143 for _, ignoreCache := range []bool{false, true} { 144 cfg := viper.New() 145 cfg.Set("ignoreCache", ignoreCache) 146 147 var wg sync.WaitGroup 148 149 for i := 0; i < 50; i++ { 150 wg.Add(1) 151 go func(gor int) { 152 defer wg.Done() 153 for j := 0; j < 10; j++ { 154 c, err := getRemote(req, ns.deps.Fs.Source, ns.deps.Cfg, cl) 155 assert.NoError(t, err) 156 assert.Equal(t, string(content), string(c)) 157 158 time.Sleep(23 * time.Millisecond) 159 } 160 }(i) 161 } 162 163 wg.Wait() 164 } 165 } 166 167 func newDeps(cfg config.Provider) *deps.Deps { 168 l := langs.NewLanguage("en", cfg) 169 l.Set("i18nDir", "i18n") 170 cs, err := helpers.NewContentSpec(l) 171 if err != nil { 172 panic(err) 173 } 174 return &deps.Deps{ 175 Cfg: cfg, 176 Fs: hugofs.NewMem(l), 177 ContentSpec: cs, 178 } 179 } 180 181 func newTestNs() *Namespace { 182 v := viper.New() 183 v.Set("contentDir", "content") 184 return New(newDeps(v)) 185 }