aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/url_test.go
blob: 65797cbc4dab65ec76b2cc731a5913a8a2b7943e (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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package config

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"gopkg.in/yaml.v3"
)

func TestUrl_Unmarshal(t *testing.T) {

	tests := []struct {
		name    string
		inp     string
		url     Url
		wantErr bool
		str     string
	}{
		{name: "Empty", inp: `url: ""`, wantErr: true},
		{name: "Simple String", inp: `url: "imap://user:pass@example.net:143/INBOX"`, url: Url{
			Scheme:   "imap",
			User:     "user",
			Password: "pass",
			Host:     "example.net",
			Port:     "143",
			Root:     "/INBOX",
		}, str: "imap://user:******@example.net:143/INBOX"},
		{name: "Simple String with @", inp: `url: "imaps://user@example:pass@example.net:143/INBOX"`, url: Url{
			Scheme:   "imaps",
			User:     "user@example",
			Password: "pass",
			Host:     "example.net",
			Port:     "143",
			Root:     "/INBOX",
		}, str: "imaps://user@example:******@example.net:143/INBOX"},
		{name: "Simple String with %40", inp: `url: "imap://user%40example:pass@example.net:4711/INBOX"`, url: Url{
			Scheme:   "imap",
			User:     "user@example",
			Password: "pass",
			Host:     "example.net",
			Port:     "4711",
			Root:     "/INBOX",
		}, str: "imap://user@example:******@example.net:4711/INBOX"},
		{name: "Simple String without user", inp: `url: "imap://example.net:143/INBOX"`, url: Url{
			Scheme:   "imap",
			User:     "",
			Password: "",
			Host:     "example.net",
			Port:     "143",
			Root:     "/INBOX",
		}, str: "imap://example.net:143/INBOX"},
		{name: "Err: Inv scheme", inp: `url: "smtp://user%40example:pass@example.net:4711/INBOX"`, wantErr: true},
		{name: "Err: No Host", inp: `url: "imap://user%40example:pass/INBOX"`, wantErr: true},
		{name: "Err: Scheme Only", inp: `url: "imap://"`, wantErr: true},
		{name: "No Root", inp: `url: "imap://user:pass@example.net:143"`, url: Url{
			Scheme:   "imap",
			User:     "user",
			Password: "pass",
			Host:     "example.net",
			Port:     "143",
			Root:     "",
		}, str: "imap://user:******@example.net:143"},
		{name: "No Root: Slash", inp: `url: "imap://user:pass@example.net:143/"`, url: Url{
			Scheme:   "imap",
			User:     "user",
			Password: "pass",
			Host:     "example.net",
			Port:     "143",
			Root:     "/",
		}, str: "imap://user:******@example.net:143/"},
		{name: "Full", inp: `url:
  scheme: imap
  host: example.net
  user: user
  password: p4ss
  port: 143
  root: INBOX
`, url: Url{
			Scheme:   "imap",
			User:     "user",
			Password: "p4ss",
			Host:     "example.net",
			Port:     "143",
			Root:     "INBOX",
		}, str: "imap://user:******@example.net:143/INBOX"},
		{name: "Default Port", inp: `url:
  scheme: imap
  host: example.net
  user: user
  password: p4ss
  root: INBOX
`, url: Url{
			Scheme:   "imap",
			User:     "user",
			Password: "p4ss",
			Host:     "example.net",
			Port:     "143",
			Root:     "INBOX",
		}, str: "imap://user:******@example.net:143/INBOX"},
		{name: "Default Scheme", inp: `url:
  host: example.net
  user: user
  password: p4ss
  port: 993
  root: INBOX
`, url: Url{
			Scheme:   "imaps",
			User:     "user",
			Password: "p4ss",
			Host:     "example.net",
			Port:     "993",
			Root:     "INBOX",
		}, str: "imaps://user:******@example.net:993/INBOX"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var u struct {
				Url Url `yaml:"url"`
			}
			err := yaml.Unmarshal([]byte(tt.inp), &u)
			if (err != nil) != tt.wantErr {
				t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			if diff := cmp.Diff(u.Url, tt.url); err == nil && diff != "" {
				t.Error(diff)
			}

			if diff := cmp.Diff(u.Url.String(), tt.str); err == nil && diff != "" {
				t.Error(diff)
			}
		})
	}
}