2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-12-26 22:40:10 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-12-26 22:40:10 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-26 22:40:10 -05:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
package tplimpl
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-28 01:36:06 -04:00
|
|
|
"fmt"
|
2014-12-26 22:40:10 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2017-03-31 02:54:22 -04:00
|
|
|
"sync"
|
2014-12-26 22:40:10 -05:00
|
|
|
"testing"
|
2017-03-31 02:54:22 -04:00
|
|
|
"time"
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2015-05-28 01:36:06 -04:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-12-26 22:40:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScpCache(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
2015-02-02 04:14:59 -05:00
|
|
|
ignore bool
|
2014-12-26 22:40:10 -05:00
|
|
|
}{
|
2015-02-02 04:14:59 -05:00
|
|
|
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
|
|
|
|
{"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`), false},
|
|
|
|
{"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`), false},
|
|
|
|
{"трям/трям", []byte(`T€st трям/трям Content 123`), false},
|
|
|
|
{"은행", []byte(`T€st C은행ontent 123`), false},
|
|
|
|
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`), false},
|
|
|
|
{"Банковский кассир", []byte(`Банковский кассир T€st Content 456`), true},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fs := new(afero.MemMapFs)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg := viper.New()
|
|
|
|
c, err := resGetCache(test.path, fs, cfg, test.ignore)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting cache: %s", err)
|
|
|
|
}
|
|
|
|
if c != nil {
|
|
|
|
t.Errorf("There is content where there should not be anything: %s", string(c))
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
err = resWriteCache(test.path, test.content, fs, cfg, test.ignore)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error writing cache: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
c, err = resGetCache(test.path, fs, cfg, test.ignore)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting cache after writing: %s", err)
|
|
|
|
}
|
2015-02-02 04:14:59 -05:00
|
|
|
if test.ignore {
|
|
|
|
if c != nil {
|
|
|
|
t.Errorf("Cache ignored but content is not nil: %s", string(c))
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-23 12:53:50 -05:00
|
|
|
if !bytes.Equal(c, test.content) {
|
2015-02-02 04:14:59 -05:00
|
|
|
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
|
|
|
|
}
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScpGetLocal(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
v := viper.New()
|
|
|
|
fs := hugofs.NewMem(v)
|
2014-12-26 22:40:10 -05:00
|
|
|
ps := helpers.FilePathSeparator
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2014-12-26 22:40:10 -05:00
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
}{
|
|
|
|
{"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
|
|
|
|
{"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
|
|
|
|
{"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)},
|
|
|
|
{"은행", []byte(`T€st C은행ontent 123`)},
|
|
|
|
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
r := bytes.NewReader(test.content)
|
2017-01-10 04:55:03 -05:00
|
|
|
err := helpers.WriteToDisk(test.path, r, fs.Source)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
c, err := resGetLocal(test.path, fs.Source, v)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting resource content: %s", err)
|
|
|
|
}
|
2016-11-23 12:53:50 -05:00
|
|
|
if !bytes.Equal(c, test.content) {
|
2014-12-26 22:40:10 -05:00
|
|
|
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) {
|
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(handler))
|
|
|
|
client := &http.Client{
|
2015-12-18 09:52:17 -05:00
|
|
|
Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
|
2015-12-18 17:23:37 -05:00
|
|
|
// Remove when https://github.com/golang/go/issues/13686 is fixed
|
2015-12-18 17:09:50 -05:00
|
|
|
r.Host = "gohugo.io"
|
2015-12-18 09:52:17 -05:00
|
|
|
return url.Parse(testServer.URL)
|
|
|
|
}},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
return testServer, client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScpGetRemote(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2014-12-26 22:40:10 -05:00
|
|
|
fs := new(afero.MemMapFs)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
2015-02-02 04:14:59 -05:00
|
|
|
ignore bool
|
2014-12-26 22:40:10 -05:00
|
|
|
}{
|
2015-02-02 04:14:59 -05:00
|
|
|
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
|
|
|
|
{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`), false},
|
|
|
|
{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`), false},
|
|
|
|
{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`), true},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
|
|
|
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(test.content)
|
|
|
|
})
|
|
|
|
defer func() { srv.Close() }()
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg := viper.New()
|
|
|
|
|
|
|
|
c, err := resGetRemote(test.path, fs, cfg, cl)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting resource content: %s", err)
|
|
|
|
}
|
2016-11-23 12:53:50 -05:00
|
|
|
if !bytes.Equal(c, test.content) {
|
2014-12-26 22:40:10 -05:00
|
|
|
t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c))
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
cc, cErr := resGetCache(test.path, fs, cfg, test.ignore)
|
2014-12-26 22:40:10 -05:00
|
|
|
if cErr != nil {
|
|
|
|
t.Error(cErr)
|
|
|
|
}
|
2015-02-02 04:14:59 -05:00
|
|
|
if test.ignore {
|
|
|
|
if cc != nil {
|
|
|
|
t.Errorf("Cache ignored but content is not nil: %s", string(cc))
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-23 12:53:50 -05:00
|
|
|
if !bytes.Equal(cc, test.content) {
|
2015-02-02 04:14:59 -05:00
|
|
|
t.Errorf("\nCache Expected: %s\nCache Actual: %s\n", string(test.content), string(cc))
|
|
|
|
}
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 02:54:22 -04:00
|
|
|
func TestScpGetRemoteParallel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
fs := new(afero.MemMapFs)
|
|
|
|
content := []byte(`T€st Content 123`)
|
|
|
|
url := "http://Foo.Bar/foo_Bar-Foo"
|
|
|
|
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(content)
|
|
|
|
})
|
|
|
|
defer func() { srv.Close() }()
|
|
|
|
|
|
|
|
for _, ignoreCache := range []bool{false, true} {
|
|
|
|
|
|
|
|
cfg := viper.New()
|
|
|
|
cfg.Set("ignoreCache", ignoreCache)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(gor int) {
|
|
|
|
defer wg.Done()
|
|
|
|
for j := 0; j < 10; j++ {
|
|
|
|
c, err := resGetRemote(url, fs, cfg, cl)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting resource content: %s", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(c, content) {
|
|
|
|
t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(content), string(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(23 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Done!")
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func TestParseCSV(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
csv []byte
|
|
|
|
sep string
|
|
|
|
exp string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{[]byte("a,b,c\nd,e,f\n"), "", "", true},
|
|
|
|
{[]byte("a,b,c\nd,e,f\n"), "~/", "", true},
|
|
|
|
{[]byte("a,b,c\nd,e,f"), "|", "a,b,cd,e,f", false},
|
|
|
|
{[]byte("q,w,e\nd,e,f"), ",", "qwedef", false},
|
|
|
|
{[]byte("a|b|c\nd|e|f|g"), "|", "abcdefg", true},
|
|
|
|
{[]byte("z|y|c\nd|e|f"), "|", "zycdef", false},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2015-03-11 13:34:57 -04:00
|
|
|
csv, err := parseCSV(test.csv, test.sep)
|
2014-12-26 22:40:10 -05:00
|
|
|
if test.err && err == nil {
|
|
|
|
t.Error("Expecting an error")
|
|
|
|
}
|
|
|
|
if test.err {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !test.err && err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
act := ""
|
|
|
|
for _, v := range csv {
|
|
|
|
act = act + strings.Join(v, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if act != test.exp {
|
|
|
|
t.Errorf("\nExpected: %s\nActual: %s\n%#v\n", test.exp, act, csv)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 01:36:06 -04:00
|
|
|
|
|
|
|
func TestGetJSONFailParse(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
f := newTestFuncster()
|
|
|
|
|
2015-05-28 01:36:06 -04:00
|
|
|
reqCount := 0
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if reqCount > 0 {
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
fmt.Fprintln(w, `{"gomeetup":["Sydney", "San Francisco", "Stockholm"]}`)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintln(w, `ERROR 500`)
|
|
|
|
}
|
|
|
|
reqCount++
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
url := ts.URL + "/test.json"
|
|
|
|
|
|
|
|
want := map[string]interface{}{"gomeetup": []interface{}{"Sydney", "San Francisco", "Stockholm"}}
|
2017-01-10 04:55:03 -05:00
|
|
|
have := f.getJSON(url)
|
2015-05-28 01:36:06 -04:00
|
|
|
assert.NotNil(t, have)
|
|
|
|
if have != nil {
|
|
|
|
assert.EqualValues(t, want, have)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCSVFailParseSep(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2017-01-10 04:55:03 -05:00
|
|
|
f := newTestFuncster()
|
|
|
|
|
2015-05-28 01:36:06 -04:00
|
|
|
reqCount := 0
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if reqCount > 0 {
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
fmt.Fprintln(w, `gomeetup,city`)
|
|
|
|
fmt.Fprintln(w, `yes,Sydney`)
|
|
|
|
fmt.Fprintln(w, `yes,San Francisco`)
|
|
|
|
fmt.Fprintln(w, `yes,Stockholm`)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintln(w, `ERROR 500`)
|
|
|
|
}
|
|
|
|
reqCount++
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
url := ts.URL + "/test.csv"
|
|
|
|
|
2016-02-06 06:29:13 -05:00
|
|
|
want := [][]string{{"gomeetup", "city"}, {"yes", "Sydney"}, {"yes", "San Francisco"}, {"yes", "Stockholm"}}
|
2017-01-10 04:55:03 -05:00
|
|
|
have := f.getCSV(",", url)
|
2015-05-28 01:36:06 -04:00
|
|
|
assert.NotNil(t, have)
|
|
|
|
if have != nil {
|
|
|
|
assert.EqualValues(t, want, have)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCSVFailParse(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
f := newTestFuncster()
|
|
|
|
|
2015-05-28 01:36:06 -04:00
|
|
|
reqCount := 0
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
if reqCount > 0 {
|
|
|
|
fmt.Fprintln(w, `gomeetup,city`)
|
|
|
|
fmt.Fprintln(w, `yes,Sydney`)
|
|
|
|
fmt.Fprintln(w, `yes,San Francisco`)
|
|
|
|
fmt.Fprintln(w, `yes,Stockholm`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, `gomeetup,city`)
|
|
|
|
fmt.Fprintln(w, `yes,Sydney,Bondi,`) // wrong number of fields in line
|
|
|
|
fmt.Fprintln(w, `yes,San Francisco`)
|
|
|
|
fmt.Fprintln(w, `yes,Stockholm`)
|
|
|
|
}
|
|
|
|
reqCount++
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
url := ts.URL + "/test.csv"
|
|
|
|
|
2016-02-06 06:29:13 -05:00
|
|
|
want := [][]string{{"gomeetup", "city"}, {"yes", "Sydney"}, {"yes", "San Francisco"}, {"yes", "Stockholm"}}
|
2017-01-10 04:55:03 -05:00
|
|
|
have := f.getCSV(",", url)
|
2015-05-28 01:36:06 -04:00
|
|
|
assert.NotNil(t, have)
|
|
|
|
if have != nil {
|
|
|
|
assert.EqualValues(t, want, have)
|
|
|
|
}
|
|
|
|
}
|