github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/resources/resource_transformers/integrity/integrity_test.go (about) 1 // Copyright 2019 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 integrity 15 16 import ( 17 "context" 18 "html/template" 19 "testing" 20 21 "github.com/gohugoio/hugo/resources/resource" 22 23 qt "github.com/frankban/quicktest" 24 "github.com/gohugoio/hugo/resources/resource_transformers/htesting" 25 ) 26 27 func TestHashFromAlgo(t *testing.T) { 28 for _, algo := range []struct { 29 name string 30 bits int 31 }{ 32 {"md5", 128}, 33 {"sha256", 256}, 34 {"sha384", 384}, 35 {"sha512", 512}, 36 {"shaman", -1}, 37 } { 38 t.Run(algo.name, func(t *testing.T) { 39 c := qt.New(t) 40 h, err := newHash(algo.name) 41 if algo.bits > 0 { 42 c.Assert(err, qt.IsNil) 43 c.Assert(h.Size(), qt.Equals, algo.bits/8) 44 } else { 45 c.Assert(err, qt.Not(qt.IsNil)) 46 c.Assert(err.Error(), qt.Contains, "use either md5, sha256, sha384 or sha512") 47 } 48 }) 49 } 50 } 51 52 func TestTransform(t *testing.T) { 53 c := qt.New(t) 54 55 spec, err := htesting.NewTestResourceSpec() 56 c.Assert(err, qt.IsNil) 57 client := New(spec) 58 59 r, err := htesting.NewResourceTransformerForSpec(spec, "hugo.txt", "Hugo Rocks!") 60 c.Assert(err, qt.IsNil) 61 62 transformed, err := client.Fingerprint(r, "") 63 64 c.Assert(err, qt.IsNil) 65 c.Assert(transformed.RelPermalink(), qt.Equals, "/hugo.a5ad1c6961214a55de53c1ce6e60d27b6b761f54851fa65e33066460dfa6a0db.txt") 66 c.Assert(transformed.Data(), qt.DeepEquals, map[string]any{"Integrity": template.HTMLAttr("sha256-pa0caWEhSlXeU8HObmDSe2t2H1SFH6ZeMwZkYN+moNs=")}) 67 content, err := transformed.(resource.ContentProvider).Content(context.Background()) 68 c.Assert(err, qt.IsNil) 69 c.Assert(content, qt.Equals, "Hugo Rocks!") 70 }