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