-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathitchio_test.go
More file actions
123 lines (102 loc) · 3.16 KB
/
itchio_test.go
File metadata and controls
123 lines (102 loc) · 3.16 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
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
package itchio
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/url"
)
func testTools(code int, body string) (*httptest.Server, *Client) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
fmt.Fprintln(w, body)
}))
// Make a transport that reroutes all traffic to the example server
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
// Make a http.Client with the transport
httpClient := &http.Client{Transport: transport}
client := ClientWithKey("APIKEY")
client.HTTPClient = httpClient
client.BaseURL = server.URL
return server, client
}
func Test_ListProfileGames(t *testing.T) {
server, client := testTools(200, `{
"games": [
{"url": "https://kenney.itch.io/barb", "id": 123, "min_price": 5000},
{"url": "https://leafo.itch.io/x-moon", "id": 456, "min_price": 12000}
]
}`)
defer server.Close()
games, err := client.ListProfileGames(context.Background())
assert.NoError(t, err)
assert.EqualValues(t, len(games.Games), 2)
assert.EqualValues(t, games.Games[0].ID, 123)
assert.EqualValues(t, games.Games[0].URL, "https://kenney.itch.io/barb")
assert.EqualValues(t, games.Games[0].MinPrice, 5000)
}
func Test_ListProfileGamesError(t *testing.T) {
server, client := testTools(400, `{
"errors": [
"invalid game"
]
}`)
defer server.Close()
_, err := client.ListProfileGames(context.Background())
assert.Error(t, err)
assert.True(t, IsAPIError(err))
assert.EqualValues(t, "itch.io API error (400): /profile/games: invalid game", err.Error())
}
func Test_ExchangeOAuthCode(t *testing.T) {
server, client := testTools(200, `{
"key": {"id": 123, "userId": 456, "key": "abc123"},
"cookie": {"itchio_token": "xyz789"}
}`)
defer server.Close()
resp, err := client.ExchangeOAuthCode(context.Background(), ExchangeOAuthCodeParams{
Code: "auth_code_123",
CodeVerifier: "verifier_abc",
RedirectURI: "http://localhost:8080/callback",
})
assert.NoError(t, err)
assert.NotNil(t, resp.Key)
assert.EqualValues(t, 123, resp.Key.ID)
assert.EqualValues(t, "abc123", resp.Key.Key)
assert.EqualValues(t, "xyz789", resp.Cookie["itchioToken"])
}
func Test_ExchangeOAuthCodeError(t *testing.T) {
server, client := testTools(400, `{
"errors": ["invalid_grant"]
}`)
defer server.Close()
_, err := client.ExchangeOAuthCode(context.Background(), ExchangeOAuthCodeParams{
Code: "invalid_code",
CodeVerifier: "verifier",
RedirectURI: "http://localhost:8080/callback",
})
assert.Error(t, err)
assert.True(t, IsAPIError(err))
}
func Test_ParseSpec(t *testing.T) {
var spec *Spec
var err error
spec, err = ParseSpec("user/page:channel")
assert.NoError(t, err)
assert.Equal(t, spec.Target, "user/page")
assert.Equal(t, spec.Channel, "channel")
spec, err = ParseSpec("user/page")
assert.NoError(t, err)
assert.Equal(t, spec.Target, "user/page")
assert.Equal(t, spec.Channel, "")
err = spec.EnsureChannel()
assert.Error(t, err)
_, err = ParseSpec("a:b:c")
assert.Error(t, err)
}