summaryrefslogtreecommitdiff
path: root/cmd/podman/formats/formats_test.go
blob: c75109d650e32780595469fc998ab0ff67f0876d (plain)
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
package formats

import (
	"bytes"
	"strings"
	"testing"

	"github.com/containers/libpod/pkg/inspect"
)

func TestSetJSONFormatEncoder(t *testing.T) {
	tt := []struct {
		name       string
		imageData  *inspect.ImageData
		expected   string
		isTerminal bool
	}{
		{
			name:       "HTML tags are not escaped",
			imageData:  &inspect.ImageData{Author: "dave <dave@corp.io>"},
			expected:   `"Author": "dave <dave@corp.io>"`,
			isTerminal: true,
		},
		{
			name:       "HTML tags are escaped",
			imageData:  &inspect.ImageData{Author: "dave <dave@corp.io>"},
			expected:   `"Author": "dave \u003cdave@corp.io\u003e"`,
			isTerminal: false,
		},
	}

	for _, tc := range tt {
		buf := bytes.NewBuffer(nil)
		enc := setJSONFormatEncoder(tc.isTerminal, buf)
		if err := enc.Encode(tc.imageData); err != nil {
			t.Errorf("test %#v failed encoding: %s", tc.name, err)
		}
		if !strings.Contains(buf.String(), tc.expected) {
			t.Errorf("test %#v expected output to contain %#v. Output:\n%v\n", tc.name, tc.expected, buf.String())
		}
	}
}