aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/url_test.go
blob: d34569030e5d52edd29462b5ef6aa1fb16378153 (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
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
	}{
		{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",
		}},
		{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",
		}},
		{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",
		}},
		{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: "Err: No Root", inp: `url: "imap://user:pass@example.net:143"`, wantErr: true},
		{name: "Err: No Root: Slash", inp: `url: "imap://user:pass@example.net:143/"`, wantErr: true},
		{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",
		}},
		{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",
		}},
		{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",
		}},
	}
	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)
			}
		})
	}
}