-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
70 lines (59 loc) · 1.58 KB
/
Copy pathclient_test.go
File metadata and controls
70 lines (59 loc) · 1.58 KB
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
package bobuild
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
type testUser struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func TestGetUser(t *testing.T) {
mockUser := testUser{
ID: "123",
Name: "Alice",
Email: "alice@example.com",
}
// Create a test server that returns the mock user
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/_api/users/123" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(mockUser)
}))
defer server.Close()
client := NewClient(server.URL, "test-api-key")
user, err := Get[testUser](client, "/users/123")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if user.Name != mockUser.Name {
t.Errorf("Expected name %s, got %s", mockUser.Name, user.Name)
}
}
func TestCreateUser(t *testing.T) {
inputUser := &testUser{
Name: "Bob",
Email: "bob@example.com",
}
// Create a test server that echos the request body back as the response
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" || r.URL.Path != "/_api/insert" {
http.NotFound(w, r)
return
}
r.Body.Close()
// Just echo the request as response for testing
w.Header().Set("Content-Type", "application/json")
}))
defer server.Close()
client := NewClient(server.URL, "test-api-key")
_, err := Insert(client, "/insert", inputUser)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}