Skip to content

Commit

Permalink
chore: add integration test example
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Feb 9, 2024
1 parent 85363c9 commit 578a9fe
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ 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) {
Expand All @@ -25,7 +30,6 @@ func TestNewClient(t *testing.T) {
require.NoError(t, err)

found := 0

for _, to := range tokens {
if strings.HasPrefix(token, to.TokenPrefix) {
found++
Expand All @@ -34,3 +38,40 @@ func TestNewClient(t *testing.T) {

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)
}

0 comments on commit 578a9fe

Please sign in to comment.