-
Notifications
You must be signed in to change notification settings - Fork 6
/
credits_test.go
68 lines (50 loc) · 1.29 KB
/
credits_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
package atlas
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"testing"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_GetCredits_InvalidKey(t *testing.T) {
defer gock.Off()
myurl, _ := url.Parse(apiEndpoint)
gock.New(apiEndpoint).
Get("credits").
MatchParam("key", "foobar").
MatchHeaders(map[string]string{
"host": myurl.Host,
"user-agent": fmt.Sprintf("ripe-atlas/%s", ourVersion),
}).
Reply(403).
BodyString(`{"error":{"status":403,"code":104,"detail":"The provided API key does not exist","title":"Forbidden"}}`)
c := Before(t)
gock.InterceptClient(c.client)
defer gock.RestoreClient(c.client)
rp, err := c.GetCredits()
assert.Error(t, err)
assert.Empty(t, rp)
}
func TestClient_GetCredits(t *testing.T) {
defer gock.Off()
ft, err := ioutil.ReadFile("testdata/credits.json")
assert.NoError(t, err)
gock.New(apiEndpoint).
Get("credits").
MatchParam("key", "foobar").
Reply(200).
BodyString(string(ft))
c := Before(t)
gock.InterceptClient(c.client)
defer gock.RestoreClient(c.client)
rp, err := c.GetCredits()
var jft Credits
err = json.Unmarshal(ft, &jft)
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, rp)
assert.EqualValues(t, &jft, rp)
}