-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpublic_key_test.go
More file actions
95 lines (75 loc) · 3.1 KB
/
public_key_test.go
File metadata and controls
95 lines (75 loc) · 3.1 KB
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
package gokick_test
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/scorfly/gokick"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetPublicKeyError(t *testing.T) {
t.Run("on new request", func(t *testing.T) {
kickClient, err := gokick.NewClient(&gokick.ClientOptions{UserAccessToken: "access-token"})
require.NoError(t, err)
var ctx context.Context
_, err = kickClient.GetPublicKey(ctx)
require.EqualError(t, err, "failed to create request: net/http: nil Context")
})
t.Run("timeout", func(t *testing.T) {
kickClient := setupTimeoutMockClient(t)
_, err := kickClient.GetPublicKey(context.Background())
require.EqualError(t, err, `failed to make request: Get "https://api.kick.com/public/v1/public-key": context deadline exceeded `+
`(Client.Timeout exceeded while awaiting headers)`)
})
t.Run("unmarshal error response", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `117`)
})
_, err := kickClient.GetPublicKey(context.Background())
assert.EqualError(t, err, `failed to unmarshal error response (KICK status code: 500 and body "117"): json: cannot unmarshal `+
`number into Go value of type gokick.errorResponse`)
})
t.Run("unmarshal categories response", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "117")
})
_, err := kickClient.GetPublicKey(context.Background())
assert.EqualError(t, err, `failed to unmarshal response body (KICK status code 200 and body "117"): json: cannot unmarshal `+
`number into Go value of type gokick.successResponse[github.com/scorfly/gokick.PublicKeyResponse]`)
})
t.Run("reader failure", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "10")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "")
})
_, err := kickClient.GetPublicKey(context.Background())
assert.EqualError(t, err, `failed to read response body (KICK status code 500): unexpected EOF`)
})
t.Run("with internal server error", func(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"message":"internal server error", "data":null}`)
})
_, err := kickClient.GetPublicKey(context.Background())
var kickError gokick.Error
require.ErrorAs(t, err, &kickError)
assert.Equal(t, http.StatusInternalServerError, kickError.Code())
assert.Equal(t, "internal server error", kickError.Message())
})
}
func TestGetPublicKeySuccess(t *testing.T) {
kickClient := setupMockClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{
"message":"success",
"data":{"public_key": "public key"}
}`)
})
response, err := kickClient.GetPublicKey(context.Background())
require.NoError(t, err)
assert.Equal(t, "public key", response.Result.PublicKey)
}