Skip to content

Commit

Permalink
test: add a test to SDK GetGroup using a mock http server
Browse files Browse the repository at this point in the history
  • Loading branch information
mariatsji committed Jan 8, 2025
1 parent b0f9874 commit ea0f35e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions internal/sdk/cloudian/sdk_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
package cloudian

import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"testing/quick"

"github.com/google/go-cmp/cmp"
)

func TestGetGroup(t *testing.T) {
expected := Group{
GroupID: "QA",
}

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"groupId": "QA"}`))
}))
defer mockServer.Close()

mockClient := &http.Client{}

_, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, mockServer.URL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}

cloudianClient := Client{
baseURL: mockServer.URL,
httpClient: mockClient,
authHeader: "",
}

group, err := cloudianClient.GetGroup(context.TODO(), "QA")

if err != nil {
t.Errorf("Error getting group: %v", err)
}

if !cmp.Equal(*group, expected) {
t.Errorf("Expected QA, got %v", group.GroupID)
}
}

func TestRealisticGroupSerialization(t *testing.T) {
jsonString := `{
"active": "true",
Expand Down

0 comments on commit ea0f35e

Please sign in to comment.