-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
77 lines (64 loc) · 1.84 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
// Package aiven provides a client for interacting with the Aiven API.
package aiven
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aiven/go-client-codegen/handler/service"
)
func TestNewClient(t *testing.T) {
token := os.Getenv("AIVEN_TOKEN")
if token == "" {
t.Skip("token is required for the test")
}
c, err := NewClient(DebugOpt(true))
require.NoError(t, err)
ctx := context.Background()
tokens, err := c.AccessTokenList(ctx)
require.NoError(t, err)
found := 0
for _, to := range tokens {
if strings.HasPrefix(token, to.TokenPrefix) {
found++
}
}
assert.Equal(t, 1, found)
}
func TestServiceCreate(t *testing.T) {
// Creates a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/project/foo/service", r.URL.Path)
// Validates request
expectIn := new(service.ServiceCreateIn)
err := json.NewDecoder(r.Body).Decode(expectIn)
assert.NoError(t, err)
assert.Equal(t, "foo", expectIn.ServiceName)
assert.Equal(t, "kafka", expectIn.ServiceType)
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`{"service": {"plan": "wow", "state": "RUNNING"}}`))
require.NoError(t, err)
}))
defer server.Close()
// Points a new client to the server url
c, err := NewClient(TokenOpt("token"), HostOpt(server.URL))
require.NotNil(t, c)
require.NoError(t, err)
// Makes create request
in := &service.ServiceCreateIn{
ServiceName: "foo",
ServiceType: "kafka",
}
out, err := c.ServiceCreate(context.Background(), "foo", in)
require.NoError(t, err)
require.NotNil(t, out)
assert.Equal(t, "wow", out.Plan)
assert.Equal(t, "RUNNING", out.State)
}