Debian Codegraph @

resources/resource_factories/create/remote_test.go

raw
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 TestRemoteResourceKeys(t *testing.T) {
119 t.Parallel()
120
121 c := qt.New(t)
122
123 check := func(uri string, optionsm map[string]any, expect1, expect2 string) {
124 c.Helper()
125 got1, got2 := remoteResourceKeys(uri, optionsm)
126 c.Assert(got1, qt.Equals, expect1)
127 c.Assert(got2, qt.Equals, expect2)
128 }
129
130 check("foo", nil, "7763396052142361238", "7763396052142361238")
131 check("foo", map[string]any{"bar": "baz"}, "5783339285578751849", "5783339285578751849")
132 check("foo", map[string]any{"key": "1234", "bar": "baz"}, "15578353952571222948", "5783339285578751849")
133 check("foo", map[string]any{"key": "12345", "bar": "baz"}, "14335752410685132726", "5783339285578751849")
134 check("asdf", map[string]any{"key": "1234", "bar": "asdf"}, "15578353952571222948", "15615023578599429261")
135 check("asdf", map[string]any{"key": "12345", "bar": "asdf"}, "14335752410685132726", "15615023578599429261")
136 }