-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_test.go
81 lines (69 loc) · 2.15 KB
/
client_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
package rawg
import (
"context"
"fmt"
"github.com/jarcoal/httpmock"
"net/http"
)
func (suite *RAWGTestSuite) TestNewClient() {
config := Config{
ApiKey: "anykey",
Language: "en",
Rps: 3,
}
client := NewClient(http.DefaultClient, &config)
suite.Equal("https://api.rawg.io/api", client.baseUrl)
suite.Equal(config.ApiKey, client.config.ApiKey)
suite.Equal(config.Rps, client.config.Rps)
}
func (suite *RAWGTestSuite) TestNewClientWithoutDefaultParams() {
config := Config{
ApiKey: "anykey",
}
client := NewClient(http.DefaultClient, &config)
suite.Equal(5, client.config.Rps)
suite.Equal("en", client.config.Language)
}
func (suite *RAWGTestSuite) TestHttpError() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
badStatusCode := http.StatusInternalServerError
config := Config{
Language: "ru",
ApiKey: "anykey",
}
client := NewClient(http.DefaultClient, &config)
httpmock.RegisterResponder(http.MethodGet, fmt.Sprintf("%s/test?key=%s&lang=%s", apiBaseUrl, config.ApiKey, config.Language),
httpmock.NewStringResponder(badStatusCode, ""),
)
err := client.get(context.Background(), "/test", nil, nil)
suite.Error(err)
_, isResponseError := err.(*RawgError)
suite.True(isResponseError)
suite.Equal(badStatusCode, err.(*RawgError).HttpCode)
}
func (suite *RAWGTestSuite) TestInvalidJsonError() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
config := Config{
Language: "ru",
ApiKey: "anykey",
}
client := NewClient(http.DefaultClient, &config)
httpmock.RegisterResponder(http.MethodGet, fmt.Sprintf("%s/test?key=%s&lang=%s", apiBaseUrl, config.ApiKey, config.Language),
httpmock.NewStringResponder(http.StatusOK, ""),
)
err := client.get(context.Background(), "/test", nil, nil)
suite.Error(err)
_, isResponseError := err.(*RawgError)
suite.False(isResponseError)
}
func (suite *RAWGTestSuite) TestRawgError() {
err := RawgError{
HttpCode: http.StatusInternalServerError,
Url: "/test",
Body: "Something went wrong",
Message: "Internal server error",
}
suite.Equal(fmt.Sprintf("Http code: %d, url: %s, body: %s, message: %s", err.HttpCode, err.Url, err.Body, err.Message), err.Error())
}