|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/feedloop/qhronos/internal/models" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | +) |
| 15 | + |
| 16 | +type MockHTTPClient struct { |
| 17 | + mock.Mock |
| 18 | +} |
| 19 | + |
| 20 | +func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) { |
| 21 | + args := m.Called(req) |
| 22 | + resp, _ := args.Get(0).(*http.Response) |
| 23 | + return resp, args.Error(1) |
| 24 | +} |
| 25 | + |
| 26 | +func TestAPICallExecutor_Success(t *testing.T) { |
| 27 | + mockHTTP := new(MockHTTPClient) |
| 28 | + executor := NewAPICallExecutor(mockHTTP) |
| 29 | + params := models.ApicallActionParams{ |
| 30 | + Method: "POST", |
| 31 | + URL: "https://api.example.com/endpoint", |
| 32 | + Headers: map[string]string{"Authorization": "Bearer token", "Content-Type": "application/json"}, |
| 33 | + Body: json.RawMessage(`{"foo":"bar"}`), |
| 34 | + } |
| 35 | + paramsBytes, _ := json.Marshal(params) |
| 36 | + mockHTTP.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 37 | + StatusCode: 200, |
| 38 | + Body: ioutil.NopCloser(nil), |
| 39 | + }, nil) |
| 40 | + event := &models.Event{ID: [16]byte{1}, Name: "API Call Event"} |
| 41 | + err := executor(context.Background(), event, paramsBytes) |
| 42 | + assert.NoError(t, err) |
| 43 | + mockHTTP.AssertCalled(t, "Do", mock.AnythingOfType("*http.Request")) |
| 44 | +} |
| 45 | + |
| 46 | +func TestAPICallExecutor_Non2xx(t *testing.T) { |
| 47 | + mockHTTP := new(MockHTTPClient) |
| 48 | + executor := NewAPICallExecutor(mockHTTP) |
| 49 | + params := models.ApicallActionParams{ |
| 50 | + Method: "POST", |
| 51 | + URL: "https://api.example.com/endpoint", |
| 52 | + Headers: map[string]string{}, |
| 53 | + Body: json.RawMessage(`{"foo":"bar"}`), |
| 54 | + } |
| 55 | + paramsBytes, _ := json.Marshal(params) |
| 56 | + mockHTTP.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{ |
| 57 | + StatusCode: 500, |
| 58 | + Body: ioutil.NopCloser(nil), |
| 59 | + }, nil) |
| 60 | + event := &models.Event{ID: [16]byte{1}, Name: "API Call Event"} |
| 61 | + err := executor(context.Background(), event, paramsBytes) |
| 62 | + assert.Error(t, err) |
| 63 | + assert.Contains(t, err.Error(), "non-2xx") |
| 64 | +} |
| 65 | + |
| 66 | +func TestAPICallExecutor_NetworkError(t *testing.T) { |
| 67 | + mockHTTP := new(MockHTTPClient) |
| 68 | + executor := NewAPICallExecutor(mockHTTP) |
| 69 | + params := models.ApicallActionParams{ |
| 70 | + Method: "POST", |
| 71 | + URL: "https://api.example.com/endpoint", |
| 72 | + Headers: map[string]string{}, |
| 73 | + Body: json.RawMessage(`{"foo":"bar"}`), |
| 74 | + } |
| 75 | + paramsBytes, _ := json.Marshal(params) |
| 76 | + mockHTTP.On("Do", mock.AnythingOfType("*http.Request")).Return((*http.Response)(nil), errors.New("network error")) |
| 77 | + event := &models.Event{ID: [16]byte{1}, Name: "API Call Event"} |
| 78 | + err := executor(context.Background(), event, paramsBytes) |
| 79 | + assert.Error(t, err) |
| 80 | + assert.Contains(t, err.Error(), "network error") |
| 81 | +} |
| 82 | + |
| 83 | +func TestAPICallExecutor_MissingParams(t *testing.T) { |
| 84 | + mockHTTP := new(MockHTTPClient) |
| 85 | + executor := NewAPICallExecutor(mockHTTP) |
| 86 | + params := models.ApicallActionParams{ |
| 87 | + Method: "", |
| 88 | + URL: "", |
| 89 | + Headers: map[string]string{}, |
| 90 | + Body: json.RawMessage(`{"foo":"bar"}`), |
| 91 | + } |
| 92 | + paramsBytes, _ := json.Marshal(params) |
| 93 | + event := &models.Event{ID: [16]byte{1}, Name: "API Call Event"} |
| 94 | + err := executor(context.Background(), event, paramsBytes) |
| 95 | + assert.Error(t, err) |
| 96 | + assert.Contains(t, err.Error(), "requires both url and method") |
| 97 | +} |
0 commit comments