forked from seventy6/theauthapi-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojects_test.go
44 lines (38 loc) · 1.23 KB
/
projects_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
package theauthapi
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestListProjects(t *testing.T) {
// Create a mock server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/projects" && r.Method == http.MethodGet {
projects := []Project{
{ID: "1", Name: "Project 1"},
{ID: "2", Name: "Project 2"},
}
json.NewEncoder(w).Encode(projects)
} else {
http.Error(w, "not found", http.StatusNotFound)
}
}))
defer mockServer.Close()
client := &Client{BaseURL: mockServer.URL, HTTPClient: mockServer.Client()}
service := &ProjectsService{client: client}
projects, err := service.List(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(projects) != 2 {
t.Errorf("expected 2 projects, got %d", len(projects))
}
expectedNames := []string{"Project 1", "Project 2"}
for i, project := range projects {
if project.Name != expectedNames[i] {
t.Errorf("expected project name %s, got %s", expectedNames[i], project.Name)
}
}
}