-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsiteurl_test.go
156 lines (120 loc) · 5.77 KB
/
siteurl_test.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gocd_test
import (
_ "embed"
"net/http"
"testing"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/stretchr/testify/assert"
)
//go:embed internal/fixtures/site_url.json
var siteURLJSON string
func Test_client_GetSiteURL(t *testing.T) {
t.Run("should be able to get the site url successfully", func(t *testing.T) {
server := mockServer([]byte(siteURLJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{
SiteURL: "http://foo.com",
SecureSiteURL: "https://foo.com",
}
actual, err := client.GetSiteURL()
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("should error out while getting the site url due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(siteURLJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionTwo}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{}
actual, err := client.GetSiteURL()
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/admin/config/server/site_urls\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while getting the site url due to missing headers", func(t *testing.T) {
server := mockServer([]byte(siteURLJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{}
actual, err := client.GetSiteURL()
assert.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/admin/config/server/site_urls\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while getting site url as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte("siteURLJSON"), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{}
actual, err := client.GetSiteURL()
assert.EqualError(t, err, "invalid character 's' looking for beginning of value")
assert.Equal(t, expected, actual)
})
t.Run("should error out while getting site url as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
expected := gocd.SiteURLConfig{}
actual, err := client.GetSiteURL()
assert.EqualError(t, err, "call made to get site url errored with: "+
"Get \"http://localhost:8156/go/api/admin/config/server/site_urls\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, expected, actual)
})
}
func Test_client_CreateOrUpdateSiteURL(t *testing.T) {
t.Run("should be able to create/update the site url successfully", func(t *testing.T) {
server := mockServer([]byte(siteURLJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
site := gocd.SiteURLConfig{
SiteURL: "http://foo.com",
SecureSiteURL: "https://foo.com",
}
expected := gocd.SiteURLConfig{
SiteURL: "http://foo.com",
SecureSiteURL: "https://foo.com",
}
actual, err := client.CreateOrUpdateSiteURL(site)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("should error out while creating/updating the site url due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(siteURLJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionTwo}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{}
actual, err := client.CreateOrUpdateSiteURL(gocd.SiteURLConfig{})
assert.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/config/server/site_urls\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while creating/updating the site url due to missing headers", func(t *testing.T) {
server := mockServer([]byte(siteURLJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{}
actual, err := client.CreateOrUpdateSiteURL(gocd.SiteURLConfig{})
assert.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/config/server/site_urls\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while getting site url as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte("siteURLJSON"), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionOne}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := gocd.SiteURLConfig{}
actual, err := client.CreateOrUpdateSiteURL(gocd.SiteURLConfig{})
assert.EqualError(t, err, "invalid character 's' looking for beginning of value")
assert.Equal(t, expected, actual)
})
t.Run("should error out while getting site url as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
expected := gocd.SiteURLConfig{}
actual, err := client.CreateOrUpdateSiteURL(gocd.SiteURLConfig{})
assert.EqualError(t, err, "call made to create/update site url errored with: "+
"Post \"http://localhost:8156/go/api/admin/config/server/site_urls\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, expected, actual)
})
}