github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/resource_factories/create/remote_test.go (about) 1 // Copyright 2021 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 15 16 import ( 17 "testing" 18 19 qt "github.com/frankban/quicktest" 20 ) 21 22 func TestDecodeRemoteOptions(t *testing.T) { 23 t.Parallel() 24 25 c := qt.New(t) 26 27 for _, test := range []struct { 28 name string 29 args map[string]any 30 want fromRemoteOptions 31 wantErr bool 32 }{ 33 { 34 "POST", 35 map[string]any{ 36 "meThod": "PoST", 37 "headers": map[string]any{ 38 "foo": "bar", 39 }, 40 }, 41 fromRemoteOptions{ 42 Method: "POST", 43 Headers: map[string]any{ 44 "foo": "bar", 45 }, 46 }, 47 false, 48 }, 49 { 50 "Body", 51 map[string]any{ 52 "meThod": "POST", 53 "body": []byte("foo"), 54 }, 55 fromRemoteOptions{ 56 Method: "POST", 57 Body: []byte("foo"), 58 }, 59 false, 60 }, 61 { 62 "Body, string", 63 map[string]any{ 64 "meThod": "POST", 65 "body": "foo", 66 }, 67 fromRemoteOptions{ 68 Method: "POST", 69 Body: []byte("foo"), 70 }, 71 false, 72 }, 73 } { 74 c.Run(test.name, func(c *qt.C) { 75 got, err := decodeRemoteOptions(test.args) 76 isErr := qt.IsNil 77 if test.wantErr { 78 isErr = qt.IsNotNil 79 } 80 81 c.Assert(err, isErr) 82 c.Assert(got, qt.DeepEquals, test.want) 83 }) 84 85 } 86 } 87 88 func TestOptionsNewRequest(t *testing.T) { 89 t.Parallel() 90 91 c := qt.New(t) 92 93 opts := fromRemoteOptions{ 94 Method: "GET", 95 Body: []byte("foo"), 96 } 97 98 req, err := opts.NewRequest("https://example.com/api") 99 100 c.Assert(err, qt.IsNil) 101 c.Assert(req.Method, qt.Equals, "GET") 102 c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"Hugo Static Site Generator"}) 103 104 opts = fromRemoteOptions{ 105 Method: "GET", 106 Body: []byte("foo"), 107 Headers: map[string]any{ 108 "User-Agent": "foo", 109 }, 110 } 111 112 req, err = opts.NewRequest("https://example.com/api") 113 114 c.Assert(err, qt.IsNil) 115 c.Assert(req.Method, qt.Equals, "GET") 116 c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"foo"}) 117 118 } 119 120 func TestCalculateResourceID(t *testing.T) { 121 t.Parallel() 122 123 c := qt.New(t) 124 125 c.Assert(calculateResourceID("foo", nil), qt.Equals, "5917621528921068675") 126 c.Assert(calculateResourceID("foo", map[string]any{"bar": "baz"}), qt.Equals, "7294498335241413323") 127 128 c.Assert(calculateResourceID("foo", map[string]any{"key": "1234", "bar": "baz"}), qt.Equals, "14904296279238663669") 129 c.Assert(calculateResourceID("asdf", map[string]any{"key": "1234", "bar": "asdf"}), qt.Equals, "14904296279238663669") 130 c.Assert(calculateResourceID("asdf", map[string]any{"key": "12345", "bar": "asdf"}), qt.Equals, "12191037851845371770") 131 }