github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/resource_factories/create/integration_test.go (about) 1 // Copyright 2023 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 create_test 15 16 import ( 17 "fmt" 18 "math/rand" 19 "net/http" 20 "net/http/httptest" 21 "strings" 22 "testing" 23 24 "github.com/gohugoio/hugo/hugolib" 25 ) 26 27 func TestGetRemoteHead(t *testing.T) { 28 29 files := ` 30 -- config.toml -- 31 [security] 32 [security.http] 33 methods = ['(?i)GET|POST|HEAD'] 34 urls = ['.*gohugo\.io.*'] 35 36 -- layouts/index.html -- 37 {{ $url := "https://gohugo.io/img/hugo.png" }} 38 {{ $opts := dict "method" "head" }} 39 {{ with resources.GetRemote $url $opts }} 40 {{ with .Err }} 41 {{ errorf "Unable to get remote resource: %s" . }} 42 {{ else }} 43 Head Content: {{ .Content }}. Head Data: {{ .Data }} 44 {{ end }} 45 {{ else }} 46 {{ errorf "Unable to get remote resource: %s" $url }} 47 {{ end }} 48 ` 49 50 b := hugolib.NewIntegrationTestBuilder( 51 hugolib.IntegrationTestConfig{ 52 T: t, 53 TxtarString: files, 54 }, 55 ) 56 57 b.Build() 58 59 b.AssertFileContent("public/index.html", 60 "Head Content: .", 61 "Head Data: map[ContentLength:18210 ContentType:image/png Status:200 OK StatusCode:200 TransferEncoding:[]]", 62 ) 63 64 } 65 66 func TestGetRemoteRetry(t *testing.T) { 67 t.Parallel() 68 69 temporaryHTTPCodes := []int{408, 429, 500, 502, 503, 504} 70 numPages := 20 71 72 handler := func(w http.ResponseWriter, r *http.Request) { 73 if rand.Intn(3) == 0 { 74 w.WriteHeader(temporaryHTTPCodes[rand.Intn(len(temporaryHTTPCodes))]) 75 return 76 } 77 w.Header().Add("Content-Type", "text/plain") 78 w.Write([]byte("Response for " + r.URL.Path + ".")) 79 } 80 81 srv := httptest.NewServer(http.HandlerFunc(handler)) 82 t.Cleanup(func() { srv.Close() }) 83 84 filesTemplate := ` 85 -- hugo.toml -- 86 disableKinds = ["home", "taxonomy", "term"] 87 timeout = "TIMEOUT" 88 [security] 89 [security.http] 90 urls = ['.*'] 91 mediaTypes = ['text/plain'] 92 -- layouts/_default/single.html -- 93 {{ $url := printf "%s%s" "URL" .RelPermalink}} 94 {{ $opts := dict }} 95 {{ with resources.GetRemote $url $opts }} 96 {{ with .Err }} 97 {{ errorf "Got Err: %s. Data: %v" . .Data }} 98 {{ else }} 99 Content: {{ .Content }} 100 {{ end }} 101 {{ else }} 102 {{ errorf "Unable to get remote resource: %s" $url }} 103 {{ end }} 104 ` 105 106 for i := 0; i < numPages; i++ { 107 filesTemplate += fmt.Sprintf("-- content/post/p%d.md --\n", i) 108 } 109 110 filesTemplate = strings.ReplaceAll(filesTemplate, "URL", srv.URL) 111 112 t.Run("OK", func(t *testing.T) { 113 files := strings.ReplaceAll(filesTemplate, "TIMEOUT", "60s") 114 b := hugolib.NewIntegrationTestBuilder( 115 hugolib.IntegrationTestConfig{ 116 T: t, 117 TxtarString: files, 118 }, 119 ) 120 121 b.Build() 122 123 for i := 0; i < numPages; i++ { 124 b.AssertFileContent(fmt.Sprintf("public/post/p%d/index.html", i), fmt.Sprintf("Content: Response for /post/p%d/.", i)) 125 } 126 }) 127 128 t.Run("Timeout", func(t *testing.T) { 129 files := strings.ReplaceAll(filesTemplate, "TIMEOUT", "100ms") 130 b, err := hugolib.NewIntegrationTestBuilder( 131 hugolib.IntegrationTestConfig{ 132 T: t, 133 TxtarString: files, 134 }, 135 ).BuildE() 136 137 // This is hard to get stable on GitHub Actions, it sometimes succeeds due to timing issues. 138 if err != nil { 139 b.AssertLogContains("Got Err") 140 b.AssertLogContains("Retry timeout") 141 b.AssertLogContains("ContentLength:0") 142 } 143 144 }) 145 146 }