-
Notifications
You must be signed in to change notification settings - Fork 6
/
itchio_test.go
89 lines (72 loc) · 2.17 KB
/
itchio_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
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_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)
}