-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
public_profile_test.go
176 lines (141 loc) · 5.36 KB
/
public_profile_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package paymail
import (
"fmt"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestClient_GetPublicProfile will test the method GetPublicProfile()
func TestClient_GetPublicProfile(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)
t.Run("successful response", func(t *testing.T) {
client := newTestClient(t)
mockGetPublicProfile(http.StatusOK)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
require.NoError(t, err)
require.NotNil(t, profile)
assert.Equal(t, http.StatusOK, profile.StatusCode)
assert.Equal(t, testName, profile.Name)
assert.Equal(t, testAvatar, profile.Avatar)
})
t.Run("successful response - status not modified", func(t *testing.T) {
client := newTestClient(t)
mockGetPublicProfile(http.StatusNotModified)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
require.NoError(t, err)
require.NotNil(t, profile)
assert.Equal(t, http.StatusNotModified, profile.StatusCode)
assert.Equal(t, testName, profile.Name)
assert.Equal(t, testAvatar, profile.Avatar)
})
t.Run("missing url", func(t *testing.T) {
client := newTestClient(t)
mockGetPublicProfile(http.StatusOK)
profile, err := client.GetPublicProfile("invalid-url", testAlias, testDomain)
require.Error(t, err)
require.Nil(t, profile)
})
t.Run("missing alias", func(t *testing.T) {
client := newTestClient(t)
mockGetPublicProfile(http.StatusOK)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", "", testDomain)
require.Error(t, err)
require.Nil(t, profile)
})
t.Run("missing domain", func(t *testing.T) {
client := newTestClient(t)
mockGetPublicProfile(http.StatusOK)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, "")
require.Error(t, err)
require.Nil(t, profile)
})
t.Run("bad request", func(t *testing.T) {
client := newTestClient(t)
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, testServerURL+"public-profile/"+testAlias+"@"+testDomain,
httpmock.NewStringResponder(
http.StatusBadRequest,
`{"message": "request failed"}`,
),
)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
require.Error(t, err)
require.NotNil(t, profile)
assert.Equal(t, http.StatusBadRequest, profile.StatusCode)
})
t.Run("http error", func(t *testing.T) {
client := newTestClient(t)
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, testServerURL+"public-profile/"+testAlias+"@"+testDomain,
httpmock.NewErrorResponder(fmt.Errorf("error in request")),
)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
require.Error(t, err)
require.Nil(t, profile)
})
t.Run("error occurred", func(t *testing.T) {
client := newTestClient(t)
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, testServerURL+"public-profile/"+testAlias+"@"+testDomain,
httpmock.NewStringResponder(
http.StatusBadRequest,
`{"message": request failed}`,
),
)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
require.Error(t, err)
require.NotNil(t, profile)
assert.Equal(t, http.StatusBadRequest, profile.StatusCode)
})
t.Run("invalid json", func(t *testing.T) {
client := newTestClient(t)
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, testServerURL+"public-profile/"+testAlias+"@"+testDomain,
httpmock.NewStringResponder(
http.StatusOK,
`{"name": MrZ,avatar: https://www.gravatar.com/avatar/372bc0ab9b8a8930d4a86b2c5b11f11e?d=identicon"}`,
),
)
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
require.Error(t, err)
require.NotNil(t, profile)
assert.Equal(t, http.StatusOK, profile.StatusCode)
assert.Equal(t, "", profile.Name)
assert.Equal(t, "", profile.Avatar)
})
}
// mockGetPublicProfile is used for mocking the response
func mockGetPublicProfile(statusCode int) {
httpmock.Reset()
httpmock.RegisterResponder(http.MethodGet, testServerURL+"public-profile/"+testAlias+"@"+testDomain,
httpmock.NewStringResponder(
statusCode, `{"name": "`+testName+`","avatar": "`+testAvatar+`"}`,
),
)
}
// ExampleClient_GetPublicProfile example using GetPublicProfile()
//
// See more examples in /examples/
func ExampleClient_GetPublicProfile() {
// Load the client
client := newTestClient(nil)
mockGetPublicProfile(http.StatusOK)
// Get profile
profile, err := client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
if err != nil {
fmt.Printf("error getting profile: " + err.Error())
return
}
fmt.Printf("found profile for: %s", profile.Name)
// Output:found profile for: MrZ
}
// BenchmarkClient_GetPublicProfile benchmarks the method GetPublicProfile()
func BenchmarkClient_GetPublicProfile(b *testing.B) {
client := newTestClient(nil)
mockGetPublicProfile(http.StatusOK)
for i := 0; i < b.N; i++ {
_, _ = client.GetPublicProfile(testServerURL+"public-profile/{alias}@{domain.tld}", testAlias, testDomain)
}
}