diff --git a/pkg/gofr/container/mock_container.go b/pkg/gofr/container/mock_container.go index 368ed9e30..104f927d7 100644 --- a/pkg/gofr/container/mock_container.go +++ b/pkg/gofr/container/mock_container.go @@ -13,19 +13,35 @@ import ( "gofr.dev/pkg/gofr/datasource/pubsub" "gofr.dev/pkg/gofr/datasource/sql" "gofr.dev/pkg/gofr/logging" + "gofr.dev/pkg/gofr/service" ) type Mocks struct { - Redis *MockRedis - SQL *mockSQL - Clickhouse *MockClickhouse - Cassandra *MockCassandra - Mongo *MockMongo - KVStore *MockKVStore - File *file.MockFileSystemProvider + Redis *MockRedis + SQL *mockSQL + Clickhouse *MockClickhouse + Cassandra *MockCassandra + Mongo *MockMongo + KVStore *MockKVStore + File *file.MockFileSystemProvider + HTTPService *service.MockHTTP } -func NewMockContainer(t *testing.T) (*Container, Mocks) { +type options func(c *Container, ctrl *gomock.Controller) any + +//nolint:revive //Because user should not access the options, and we might change it to an interface in the future. +func WithMockHTTPService(httpServiceNames ...string) options { + return func(c *Container, ctrl *gomock.Controller) any { + mockservice := service.NewMockHTTP(ctrl) + for _, s := range httpServiceNames { + c.Services[s] = mockservice + } + + return mockservice + } +} + +func NewMockContainer(t *testing.T, options ...options) (*Container, Mocks) { t.Helper() container := &Container{} @@ -62,14 +78,28 @@ func NewMockContainer(t *testing.T) (*Container, Mocks) { fileStoreMock := file.NewMockFileSystemProvider(ctrl) container.File = fileStoreMock + var httpMock *service.MockHTTP + + container.Services = make(map[string]service.HTTP) + + for _, option := range options { + optionsAdded := option(container, ctrl) + + val, ok := optionsAdded.(*service.MockHTTP) + if ok { + httpMock = val + } + } + mocks := Mocks{ - Redis: redisMock, - SQL: sqlMockWrapper, - Clickhouse: clickhouseMock, - Cassandra: cassandraMock, - Mongo: mongoMock, - KVStore: kvStoreMock, - File: fileStoreMock, + Redis: redisMock, + SQL: sqlMockWrapper, + Clickhouse: clickhouseMock, + Cassandra: cassandraMock, + Mongo: mongoMock, + KVStore: kvStoreMock, + File: fileStoreMock, + HTTPService: httpMock, } redisMock.EXPECT().Close().AnyTimes() diff --git a/pkg/gofr/container/mockcontainer_test.go b/pkg/gofr/container/mockcontainer_test.go index 79b17be46..21a57680c 100644 --- a/pkg/gofr/container/mockcontainer_test.go +++ b/pkg/gofr/container/mockcontainer_test.go @@ -1,14 +1,59 @@ package container import ( + "bytes" "context" + "net/http/httptest" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gofr.dev/pkg/gofr/datasource" ) +func Test_HttpServiceMock(t *testing.T) { + test := struct { + desc string + path string + statusCode int + expectedRes string + }{ + + desc: "simple service handler", + path: "/fact", + expectedRes: `{"data":{"fact":"Cats have 3 eyelids.","length":20}}` + "\n", + statusCode: 200, + } + + httpservices := []string{"cat-facts", "cat-facts1", "cat-facts2"} + + _, mock := NewMockContainer(t, WithMockHTTPService(httpservices...)) + + res := httptest.NewRecorder() + res.Body = bytes.NewBufferString(`{"fact":"Cats have 3 eyelids.","length":20}` + "\n") + res.Code = test.statusCode + result := res.Result() + + // Setting mock expectations + mock.HTTPService.EXPECT().Get(context.Background(), "fact", map[string]interface{}{ + "max_length": 20, + }).Return(result, nil) + + resp, err := mock.HTTPService.Get(context.Background(), "fact", map[string]interface{}{ + "max_length": 20, + }) + + require.NoError(t, err) + assert.Equal(t, resp, result) + + err = result.Body.Close() + require.NoError(t, err) + + err = resp.Body.Close() + require.NoError(t, err) +} + // TestMockSQL_Select tests the successful operation of SQL mocking for SELECT statements. // It checks that the mock expectations are correctly set and that the SQL database function // is called as expected. diff --git a/pkg/gofr/service/mock_http_service.go b/pkg/gofr/service/mock_http_service.go new file mode 100644 index 000000000..90f2fd83e --- /dev/null +++ b/pkg/gofr/service/mock_http_service.go @@ -0,0 +1,392 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ../service/new.go +// +// Generated by this command: +// +// mockgen -source=../service/new.go -destination=../service/mock_http_service.go -package=service HTTP +// + +// Package service is a generated GoMock package. +package service + +import ( + context "context" + http "net/http" + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockHTTP is a mock of HTTP interface. +type MockHTTP struct { + ctrl *gomock.Controller + recorder *MockHTTPMockRecorder +} + +// MockHTTPMockRecorder is the mock recorder for MockHTTP. +type MockHTTPMockRecorder struct { + mock *MockHTTP +} + +// NewMockHTTP creates a new mock instance. +func NewMockHTTP(ctrl *gomock.Controller) *MockHTTP { + mock := &MockHTTP{ctrl: ctrl} + mock.recorder = &MockHTTPMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockHTTP) EXPECT() *MockHTTPMockRecorder { + return m.recorder +} + +// Delete mocks base method. +func (m *MockHTTP) Delete(ctx context.Context, api string, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", ctx, api, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Delete indicates an expected call of Delete. +func (mr *MockHTTPMockRecorder) Delete(ctx, api, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockHTTP)(nil).Delete), ctx, api, body) +} + +// DeleteWithHeaders mocks base method. +func (m *MockHTTP) DeleteWithHeaders(ctx context.Context, api string, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteWithHeaders", ctx, api, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DeleteWithHeaders indicates an expected call of DeleteWithHeaders. +func (mr *MockHTTPMockRecorder) DeleteWithHeaders(ctx, api, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteWithHeaders", reflect.TypeOf((*MockHTTP)(nil).DeleteWithHeaders), ctx, api, body, headers) +} + +// Get mocks base method. +func (m *MockHTTP) Get(ctx context.Context, api string, queryParams map[string]any) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Get", ctx, api, queryParams) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Get indicates an expected call of Get. +func (mr *MockHTTPMockRecorder) Get(ctx, api, queryParams any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockHTTP)(nil).Get), ctx, api, queryParams) +} + +// GetWithHeaders mocks base method. +func (m *MockHTTP) GetWithHeaders(ctx context.Context, path string, queryParams map[string]any, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetWithHeaders", ctx, path, queryParams, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetWithHeaders indicates an expected call of GetWithHeaders. +func (mr *MockHTTPMockRecorder) GetWithHeaders(ctx, path, queryParams, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetWithHeaders", reflect.TypeOf((*MockHTTP)(nil).GetWithHeaders), ctx, path, queryParams, headers) +} + +// HealthCheck mocks base method. +func (m *MockHTTP) HealthCheck(ctx context.Context) *Health { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HealthCheck", ctx) + ret0, _ := ret[0].(*Health) + return ret0 +} + +// HealthCheck indicates an expected call of HealthCheck. +func (mr *MockHTTPMockRecorder) HealthCheck(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HealthCheck", reflect.TypeOf((*MockHTTP)(nil).HealthCheck), ctx) +} + +// Patch mocks base method. +func (m *MockHTTP) Patch(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Patch", ctx, api, queryParams, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Patch indicates an expected call of Patch. +func (mr *MockHTTPMockRecorder) Patch(ctx, api, queryParams, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Patch", reflect.TypeOf((*MockHTTP)(nil).Patch), ctx, api, queryParams, body) +} + +// PatchWithHeaders mocks base method. +func (m *MockHTTP) PatchWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PatchWithHeaders", ctx, api, queryParams, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PatchWithHeaders indicates an expected call of PatchWithHeaders. +func (mr *MockHTTPMockRecorder) PatchWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PatchWithHeaders", reflect.TypeOf((*MockHTTP)(nil).PatchWithHeaders), ctx, api, queryParams, body, headers) +} + +// Post mocks base method. +func (m *MockHTTP) Post(ctx context.Context, path string, queryParams map[string]any, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Post", ctx, path, queryParams, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Post indicates an expected call of Post. +func (mr *MockHTTPMockRecorder) Post(ctx, path, queryParams, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Post", reflect.TypeOf((*MockHTTP)(nil).Post), ctx, path, queryParams, body) +} + +// PostWithHeaders mocks base method. +func (m *MockHTTP) PostWithHeaders(ctx context.Context, path string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PostWithHeaders", ctx, path, queryParams, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PostWithHeaders indicates an expected call of PostWithHeaders. +func (mr *MockHTTPMockRecorder) PostWithHeaders(ctx, path, queryParams, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PostWithHeaders", reflect.TypeOf((*MockHTTP)(nil).PostWithHeaders), ctx, path, queryParams, body, headers) +} + +// Put mocks base method. +func (m *MockHTTP) Put(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Put", ctx, api, queryParams, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Put indicates an expected call of Put. +func (mr *MockHTTPMockRecorder) Put(ctx, api, queryParams, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Put", reflect.TypeOf((*MockHTTP)(nil).Put), ctx, api, queryParams, body) +} + +// PutWithHeaders mocks base method. +func (m *MockHTTP) PutWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PutWithHeaders", ctx, api, queryParams, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PutWithHeaders indicates an expected call of PutWithHeaders. +func (mr *MockHTTPMockRecorder) PutWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PutWithHeaders", reflect.TypeOf((*MockHTTP)(nil).PutWithHeaders), ctx, api, queryParams, body, headers) +} + +// getHealthResponseForEndpoint mocks base method. +func (m *MockHTTP) getHealthResponseForEndpoint(ctx context.Context, endpoint string, timeout int) *Health { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "getHealthResponseForEndpoint", ctx, endpoint, timeout) + ret0, _ := ret[0].(*Health) + return ret0 +} + +// getHealthResponseForEndpoint indicates an expected call of getHealthResponseForEndpoint. +func (mr *MockHTTPMockRecorder) getHealthResponseForEndpoint(ctx, endpoint, timeout any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "getHealthResponseForEndpoint", reflect.TypeOf((*MockHTTP)(nil).getHealthResponseForEndpoint), ctx, endpoint, timeout) +} + +// MockhttpClient is a mock of httpClient interface. +type MockhttpClient struct { + ctrl *gomock.Controller + recorder *MockhttpClientMockRecorder +} + +// MockhttpClientMockRecorder is the mock recorder for MockhttpClient. +type MockhttpClientMockRecorder struct { + mock *MockhttpClient +} + +// NewMockhttpClient creates a new mock instance. +func NewMockhttpClient(ctrl *gomock.Controller) *MockhttpClient { + mock := &MockhttpClient{ctrl: ctrl} + mock.recorder = &MockhttpClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockhttpClient) EXPECT() *MockhttpClientMockRecorder { + return m.recorder +} + +// Delete mocks base method. +func (m *MockhttpClient) Delete(ctx context.Context, api string, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", ctx, api, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Delete indicates an expected call of Delete. +func (mr *MockhttpClientMockRecorder) Delete(ctx, api, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockhttpClient)(nil).Delete), ctx, api, body) +} + +// DeleteWithHeaders mocks base method. +func (m *MockhttpClient) DeleteWithHeaders(ctx context.Context, api string, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteWithHeaders", ctx, api, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DeleteWithHeaders indicates an expected call of DeleteWithHeaders. +func (mr *MockhttpClientMockRecorder) DeleteWithHeaders(ctx, api, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteWithHeaders", reflect.TypeOf((*MockhttpClient)(nil).DeleteWithHeaders), ctx, api, body, headers) +} + +// Get mocks base method. +func (m *MockhttpClient) Get(ctx context.Context, api string, queryParams map[string]any) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Get", ctx, api, queryParams) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Get indicates an expected call of Get. +func (mr *MockhttpClientMockRecorder) Get(ctx, api, queryParams any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockhttpClient)(nil).Get), ctx, api, queryParams) +} + +// GetWithHeaders mocks base method. +func (m *MockhttpClient) GetWithHeaders(ctx context.Context, path string, queryParams map[string]any, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetWithHeaders", ctx, path, queryParams, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetWithHeaders indicates an expected call of GetWithHeaders. +func (mr *MockhttpClientMockRecorder) GetWithHeaders(ctx, path, queryParams, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetWithHeaders", reflect.TypeOf((*MockhttpClient)(nil).GetWithHeaders), ctx, path, queryParams, headers) +} + +// Patch mocks base method. +func (m *MockhttpClient) Patch(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Patch", ctx, api, queryParams, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Patch indicates an expected call of Patch. +func (mr *MockhttpClientMockRecorder) Patch(ctx, api, queryParams, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Patch", reflect.TypeOf((*MockhttpClient)(nil).Patch), ctx, api, queryParams, body) +} + +// PatchWithHeaders mocks base method. +func (m *MockhttpClient) PatchWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PatchWithHeaders", ctx, api, queryParams, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PatchWithHeaders indicates an expected call of PatchWithHeaders. +func (mr *MockhttpClientMockRecorder) PatchWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PatchWithHeaders", reflect.TypeOf((*MockhttpClient)(nil).PatchWithHeaders), ctx, api, queryParams, body, headers) +} + +// Post mocks base method. +func (m *MockhttpClient) Post(ctx context.Context, path string, queryParams map[string]any, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Post", ctx, path, queryParams, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Post indicates an expected call of Post. +func (mr *MockhttpClientMockRecorder) Post(ctx, path, queryParams, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Post", reflect.TypeOf((*MockhttpClient)(nil).Post), ctx, path, queryParams, body) +} + +// PostWithHeaders mocks base method. +func (m *MockhttpClient) PostWithHeaders(ctx context.Context, path string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PostWithHeaders", ctx, path, queryParams, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PostWithHeaders indicates an expected call of PostWithHeaders. +func (mr *MockhttpClientMockRecorder) PostWithHeaders(ctx, path, queryParams, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PostWithHeaders", reflect.TypeOf((*MockhttpClient)(nil).PostWithHeaders), ctx, path, queryParams, body, headers) +} + +// Put mocks base method. +func (m *MockhttpClient) Put(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Put", ctx, api, queryParams, body) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Put indicates an expected call of Put. +func (mr *MockhttpClientMockRecorder) Put(ctx, api, queryParams, body any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Put", reflect.TypeOf((*MockhttpClient)(nil).Put), ctx, api, queryParams, body) +} + +// PutWithHeaders mocks base method. +func (m *MockhttpClient) PutWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PutWithHeaders", ctx, api, queryParams, body, headers) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PutWithHeaders indicates an expected call of PutWithHeaders. +func (mr *MockhttpClientMockRecorder) PutWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PutWithHeaders", reflect.TypeOf((*MockhttpClient)(nil).PutWithHeaders), ctx, api, queryParams, body, headers) +}