|
| 1 | +package hubspot |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestNewClient(t *testing.T) { |
| 10 | + apiKey := "test-api-key" |
| 11 | + client := NewClient(apiKey) |
| 12 | + |
| 13 | + if client == nil { |
| 14 | + t.Fatal("NewClient returned nil") |
| 15 | + } |
| 16 | + |
| 17 | + if client.apiKey != apiKey { |
| 18 | + t.Errorf("Expected API key %s, got %s", apiKey, client.apiKey) |
| 19 | + } |
| 20 | + |
| 21 | + if client.baseURL != baseURL { |
| 22 | + t.Errorf("Expected base URL %s, got %s", baseURL, client.baseURL) |
| 23 | + } |
| 24 | + |
| 25 | + if client.client == nil { |
| 26 | + t.Fatal("HTTP client is nil") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestClient_doRequest(t *testing.T) { |
| 31 | + // Create a test server |
| 32 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | + if r.Header.Get("Authorization") != "Bearer test-api-key" { |
| 34 | + t.Error("Missing or incorrect Authorization header") |
| 35 | + } |
| 36 | + w.WriteHeader(http.StatusOK) |
| 37 | + w.Write([]byte(`{"test": "data"}`)) |
| 38 | + })) |
| 39 | + defer server.Close() |
| 40 | + |
| 41 | + client := NewClient("test-api-key") |
| 42 | + client.baseURL = server.URL |
| 43 | + |
| 44 | + _, err := client.doRequest("GET", "/test", nil) |
| 45 | + if err != nil { |
| 46 | + t.Errorf("doRequest failed: %v", err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestContact_String(t *testing.T) { |
| 51 | + contact := Contact{ |
| 52 | + ID: "123", |
| 53 | + Properties: map[string]interface{}{ |
| 54 | + "email": "test@example.com", |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + if contact.ID != "123" { |
| 59 | + t.Errorf("Expected ID 123, got %s", contact.ID) |
| 60 | + } |
| 61 | + |
| 62 | + email, ok := contact.Properties["email"].(string) |
| 63 | + if !ok || email != "test@example.com" { |
| 64 | + t.Errorf("Expected email test@example.com, got %v", contact.Properties["email"]) |
| 65 | + } |
| 66 | +} |
| 67 | + |
0 commit comments