-
Notifications
You must be signed in to change notification settings - Fork 11
/
gw2api_test.go
87 lines (76 loc) · 1.9 KB
/
gw2api_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
package gw2api
import (
"fmt"
"os"
"testing"
)
func ExampleNewGW2Api() {
api := NewGW2Api()
build, _ := api.Build()
fmt.Printf("%d\n", build)
}
func TestNewGW2Api(t *testing.T) {
api := NewGW2Api()
if ver, err := api.Build(); err != nil {
t.Error(err)
} else if ver < 1 {
t.Error("Build version is corrupted")
}
}
func ExampleNewAuthenticatedGW2Api() {
var api *GW2Api
var err error
if api, err = NewAuthenticatedGW2Api("<APIKEY>"); err != nil {
fmt.Printf("Failed to connect to the API: %s", err)
}
if api.HasPermission(PermAccount) {
var acc Account
if acc, err = api.Account(); err != nil {
fmt.Printf("API did not answer correctly: %s", err)
}
fmt.Printf("%s\n", acc.Name)
}
}
func TestNewAuthenticatedGW2Api(t *testing.T) {
var apikey string
if apikey = os.Getenv("APIKEY"); len(apikey) < 1 {
t.Skip("Cannot test without APIKEY")
}
var api *GW2Api
var err error
if api, err = NewAuthenticatedGW2Api(apikey); err != nil {
t.Error(err)
}
if !api.HasPermission(PermAccount) {
t.Error("Failed to obtain basic permission")
}
}
func TestSetAuthentication(t *testing.T) {
var apikey string
if apikey = os.Getenv("APIKEY"); len(apikey) < 1 {
t.Skip("Cannot test without APIKEY")
}
api := NewGW2Api()
if _, err := api.Account(); err == nil {
t.Error("Should be able to call authenticated endpoint")
}
if err := api.SetAuthentication("FOOBAR"); err == nil {
t.Error("Failed to catch error for incorrectly formatted APIKEY")
}
if err := api.SetAuthentication(apikey); err != nil {
t.Error("Failed to authenticate: ", err)
}
if _, err := api.Account(); err != nil {
t.Error("Authentication should be available now")
}
}
func TestAPIError(t *testing.T) {
type ProbablyError struct {
Foo int `json:"error"`
}
api := NewGW2Api()
var foo ProbablyError
if err := api.fetchEndpoint("v2", "foobar", nil, &foo); err == nil {
t.Error("Should not have been nil")
}
}