File: yaml_test.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (70 lines) | stat: -rw-r--r-- 1,654 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package toolbox

import (
	"encoding/json"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	yaml "gopkg.in/yaml.v2"
)

func TestNormalizeKVPairs(t *testing.T) {

	{ //yaml case
		YAML := `- Requests:
    - URL: http://localhost:5000
      Method: GET
      Header:
        aHeader:
          - "v1"
          - "v2"

        someOtherHeader:

          - "CP=RTO"

      Body: "hey there"
      Cookies:
        - Name: aHeader
          Value: a-value
          DYAMLomain: "localhost"
          Expires: "2023-12-16T20:17:38Z"
          RawExpires: Sat, 16 Dec 2023 20:17:38 GMT`

		var data interface{}
		err := yaml.NewDecoder(strings.NewReader(YAML)).Decode(&data)
		assert.Nil(t, err)
		normalized, err := NormalizeKVPairs(data)
		assert.Nil(t, err)
		requests := AsMap(AsSlice(normalized)[0])["Requests"]
		request := AsMap(AsSlice(requests)[0])
		assert.Equal(t, "http://localhost:5000", request["URL"])
		header := AsMap(request["Header"])
		assert.Equal(t, []interface{}{"v1", "v2"}, header["aHeader"])
	}
	{
		JSON := `[
{"Key":"k1", "Value":"v1"},
{"Key":"k2", "Value":"v2"},
{"Key":"k3", "Value":[
	{"Key":"k1", "Value":"v1", "Attr":2}
]}]`

		var data interface{}
		err := json.NewDecoder(strings.NewReader(JSON)).Decode(&data)
		assert.Nil(t, err)
		normalized, err := NormalizeKVPairs(data)
		assert.Nil(t, err)
		aMap := AsMap(normalized)
		assert.Equal(t, "v1", aMap["k1"])
		assert.Equal(t, "v2", aMap["k2"])
		aSlice := AsSlice(aMap["k3"])
		assert.NotNil(t, aSlice)
		anItem := AsMap(aSlice[0])
		assert.Equal(t, "k1", anItem["Key"])
		assert.Equal(t, "v1", anItem["Value"])
		assert.Equal(t, 2.0, anItem["Attr"])
	}

}