From 578a9fe55a0d218269d8859c1e8e4834a5b70577 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Fri, 9 Feb 2024 11:48:51 +0100 Subject: [PATCH] chore: add integration test example --- client_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 2dfd156..d431d71 100644 --- a/client_test.go +++ b/client_test.go @@ -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) { @@ -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++ @@ -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) +}