github.com/neohugo/neohugo@v0.123.8/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 func TestOptionsNewRequest(t *testing.T) { 88 t.Parallel() 89 90 c := qt.New(t) 91 92 opts := fromRemoteOptions{ 93 Method: "GET", 94 Body: []byte("foo"), 95 } 96 97 req, err := opts.NewRequest("https://example.com/api") 98 99 c.Assert(err, qt.IsNil) 100 c.Assert(req.Method, qt.Equals, "GET") 101 c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"Hugo Static Site Generator"}) 102 103 opts = fromRemoteOptions{ 104 Method: "GET", 105 Body: []byte("foo"), 106 Headers: map[string]any{ 107 "User-Agent": "foo", 108 }, 109 } 110 111 req, err = opts.NewRequest("https://example.com/api") 112 113 c.Assert(err, qt.IsNil) 114 c.Assert(req.Method, qt.Equals, "GET") 115 c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"foo"}) 116 } 117 118 func TestCalculateResourceID(t *testing.T) { 119 t.Parallel() 120 121 c := qt.New(t) 122 123 c.Assert(calculateResourceID("foo", nil), qt.Equals, "5917621528921068675") 124 c.Assert(calculateResourceID("foo", map[string]any{"bar": "baz"}), qt.Equals, "7294498335241413323") 125 126 c.Assert(calculateResourceID("foo", map[string]any{"key": "1234", "bar": "baz"}), qt.Equals, "14904296279238663669") 127 c.Assert(calculateResourceID("asdf", map[string]any{"key": "1234", "bar": "asdf"}), qt.Equals, "14904296279238663669") 128 c.Assert(calculateResourceID("asdf", map[string]any{"key": "12345", "bar": "asdf"}), qt.Equals, "12191037851845371770") 129 }