|
| 1 | +// httpclient_api_handler.go |
| 2 | +package httpclient |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/deploymenttheory/go-api-http-client/apihandlers/graph" |
| 10 | + "github.com/deploymenttheory/go-api-http-client/apihandlers/jamfpro" |
| 11 | + "github.com/deploymenttheory/go-api-http-client/logger" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | +) |
| 15 | + |
| 16 | +// NewMockAPIHandler creates a new instance of MockAPIHandler. |
| 17 | +func NewMockAPIHandler() *MockAPIHandler { |
| 18 | + return &MockAPIHandler{} |
| 19 | +} |
| 20 | + |
| 21 | +// Implement each method of the APIHandler interface on MockAPIHandler. |
| 22 | + |
| 23 | +func (m *MockAPIHandler) ConstructAPIResourceEndpoint(instanceName string, endpointPath string, log logger.Logger) string { |
| 24 | + args := m.Called(instanceName, endpointPath, log) |
| 25 | + return args.String(0) |
| 26 | +} |
| 27 | + |
| 28 | +func (m *MockAPIHandler) ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string { |
| 29 | + args := m.Called(instanceName, endpointPath, log) |
| 30 | + return args.String(0) |
| 31 | +} |
| 32 | + |
| 33 | +func (m *MockAPIHandler) MarshalRequest(body interface{}, method string, endpoint string, log logger.Logger) ([]byte, error) { |
| 34 | + args := m.Called(body, method, endpoint, log) |
| 35 | + return args.Get(0).([]byte), args.Error(1) |
| 36 | +} |
| 37 | + |
| 38 | +func (m *MockAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error) { |
| 39 | + args := m.Called(fields, files, log) |
| 40 | + return args.Get(0).([]byte), args.String(1), args.Error(2) |
| 41 | +} |
| 42 | + |
| 43 | +func (m *MockAPIHandler) HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.Logger) error { |
| 44 | + args := m.Called(resp, out, log) |
| 45 | + return args.Error(0) |
| 46 | +} |
| 47 | + |
| 48 | +func (m *MockAPIHandler) HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error { |
| 49 | + args := m.Called(resp, out, log) |
| 50 | + return args.Error(0) |
| 51 | +} |
| 52 | + |
| 53 | +func TestConstructAPIResourceEndpoint(t *testing.T) { |
| 54 | + mockLogger := new(logger.MockLogger) |
| 55 | + mockAPIHandler := NewMockAPIHandler() |
| 56 | + instanceName := "testInstance" |
| 57 | + endpointPath := "/testEndpoint" |
| 58 | + |
| 59 | + expectedEndpoint := "https://testInstance.apiendpoint.com/testEndpoint" |
| 60 | + |
| 61 | + // Setup expectations |
| 62 | + mockAPIHandler.On("ConstructAPIResourceEndpoint", instanceName, endpointPath, mockLogger).Return(expectedEndpoint) |
| 63 | + |
| 64 | + // Call the method |
| 65 | + endpoint := mockAPIHandler.ConstructAPIResourceEndpoint(instanceName, endpointPath, mockLogger) |
| 66 | + |
| 67 | + // Assertions |
| 68 | + assert.Equal(t, expectedEndpoint, endpoint, "The constructed API resource endpoint did not match the expected value") |
| 69 | + |
| 70 | + // Verify that the expectations were met |
| 71 | + mockAPIHandler.AssertExpectations(t) |
| 72 | +} |
| 73 | + |
| 74 | +// TestLoadAPIHandler verifies the functionality of the LoadAPIHandler function in the httpclient package. |
| 75 | +// This function is designed to return the appropriate APIHandler implementation based on the provided apiType argument. |
| 76 | +// The test cases cover the following scenarios: |
| 77 | +// 1. Loading a JamfPro API handler by providing "jamfpro" as the apiType. |
| 78 | +// 2. Loading a Graph API handler by providing "graph" as the apiType. |
| 79 | +// 3. Handling an unsupported API type by providing an unknown apiType, which should result in an error. |
| 80 | +func TestLoadAPIHandler(t *testing.T) { |
| 81 | + mockLogger := new(MockLogger) |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + apiType string |
| 85 | + wantType interface{} |
| 86 | + wantErr bool |
| 87 | + }{ |
| 88 | + {"Load JamfPro Handler", "jamfpro", &jamfpro.JamfAPIHandler{}, false}, |
| 89 | + {"Load Graph Handler", "graph", &graph.GraphAPIHandler{}, false}, |
| 90 | + {"Unsupported API Type", "unknown", nil, true}, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + if tt.wantErr { |
| 96 | + mockLogger.On("Error", mock.Anything, mock.Anything).Return(errors.New("Unsupported API type")).Once() |
| 97 | + } else { |
| 98 | + mockLogger.On("Info", mock.Anything, mock.Anything).Once() |
| 99 | + } |
| 100 | + |
| 101 | + got, err := LoadAPIHandler(tt.apiType, mockLogger) |
| 102 | + if tt.wantErr { |
| 103 | + assert.Error(t, err) |
| 104 | + } else { |
| 105 | + assert.NoError(t, err) |
| 106 | + assert.IsType(t, tt.wantType, got) |
| 107 | + } |
| 108 | + |
| 109 | + mockLogger.AssertExpectations(t) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments