From af6860bc4ce13382eb9c12fd7c21a9974bceeb2f Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sun, 7 Apr 2024 19:42:01 +0800 Subject: [PATCH] Adds tests for http backend --- .mockery.yaml | 1 + backend/http_test.go | 38 +++++++ mocks/mock_Request.go | 226 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 265 insertions(+) create mode 100644 backend/http_test.go create mode 100644 mocks/mock_Request.go diff --git a/.mockery.yaml b/.mockery.yaml index 321e958..4ef845a 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -11,3 +11,4 @@ packages: Backend: RequestHandler: Connection: + Request: diff --git a/backend/http_test.go b/backend/http_test.go new file mode 100644 index 0000000..6be4918 --- /dev/null +++ b/backend/http_test.go @@ -0,0 +1,38 @@ +package backend + +import ( + "bytes" + "net/http" + "net/http/httptest" + "testing" + + "github.com/ksysoev/wasabi" + "github.com/ksysoev/wasabi/mocks" +) + +func TestHTTPBackend_Handle(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`OK`)) + })) + defer server.Close() + + mockConn := mocks.NewMockConnection(t) + mockReq := mocks.NewMockRequest(t) + + mockConn.EXPECT().Send([]byte("OK")).Return(nil) + mockReq.EXPECT().Data().Return([]byte("test request")) + + backend := NewBackend(func(req wasabi.Request) (*http.Request, error) { + bodyReader := bytes.NewBufferString(string(req.Data())) + httpReq, err := http.NewRequest("GET", server.URL, bodyReader) + if err != nil { + return nil, err + } + return httpReq, nil + }) + + err := backend.Handle(mockConn, mockReq) + if err != nil { + t.Fatal(err) + } +} diff --git a/mocks/mock_Request.go b/mocks/mock_Request.go new file mode 100644 index 0000000..8472bf5 --- /dev/null +++ b/mocks/mock_Request.go @@ -0,0 +1,226 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + +//go:build !compile + +package mocks + +import ( + context "context" + + wasabi "github.com/ksysoev/wasabi" + mock "github.com/stretchr/testify/mock" +) + +// MockRequest is an autogenerated mock type for the Request type +type MockRequest struct { + mock.Mock +} + +type MockRequest_Expecter struct { + mock *mock.Mock +} + +func (_m *MockRequest) EXPECT() *MockRequest_Expecter { + return &MockRequest_Expecter{mock: &_m.Mock} +} + +// Context provides a mock function with given fields: +func (_m *MockRequest) Context() context.Context { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Context") + } + + var r0 context.Context + if rf, ok := ret.Get(0).(func() context.Context); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(context.Context) + } + } + + return r0 +} + +// MockRequest_Context_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Context' +type MockRequest_Context_Call struct { + *mock.Call +} + +// Context is a helper method to define mock.On call +func (_e *MockRequest_Expecter) Context() *MockRequest_Context_Call { + return &MockRequest_Context_Call{Call: _e.mock.On("Context")} +} + +func (_c *MockRequest_Context_Call) Run(run func()) *MockRequest_Context_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockRequest_Context_Call) Return(_a0 context.Context) *MockRequest_Context_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockRequest_Context_Call) RunAndReturn(run func() context.Context) *MockRequest_Context_Call { + _c.Call.Return(run) + return _c +} + +// Data provides a mock function with given fields: +func (_m *MockRequest) Data() []byte { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Data") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// MockRequest_Data_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Data' +type MockRequest_Data_Call struct { + *mock.Call +} + +// Data is a helper method to define mock.On call +func (_e *MockRequest_Expecter) Data() *MockRequest_Data_Call { + return &MockRequest_Data_Call{Call: _e.mock.On("Data")} +} + +func (_c *MockRequest_Data_Call) Run(run func()) *MockRequest_Data_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockRequest_Data_Call) Return(_a0 []byte) *MockRequest_Data_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockRequest_Data_Call) RunAndReturn(run func() []byte) *MockRequest_Data_Call { + _c.Call.Return(run) + return _c +} + +// RoutingKey provides a mock function with given fields: +func (_m *MockRequest) RoutingKey() string { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for RoutingKey") + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// MockRequest_RoutingKey_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RoutingKey' +type MockRequest_RoutingKey_Call struct { + *mock.Call +} + +// RoutingKey is a helper method to define mock.On call +func (_e *MockRequest_Expecter) RoutingKey() *MockRequest_RoutingKey_Call { + return &MockRequest_RoutingKey_Call{Call: _e.mock.On("RoutingKey")} +} + +func (_c *MockRequest_RoutingKey_Call) Run(run func()) *MockRequest_RoutingKey_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockRequest_RoutingKey_Call) Return(_a0 string) *MockRequest_RoutingKey_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockRequest_RoutingKey_Call) RunAndReturn(run func() string) *MockRequest_RoutingKey_Call { + _c.Call.Return(run) + return _c +} + +// WithContext provides a mock function with given fields: ctx +func (_m *MockRequest) WithContext(ctx context.Context) wasabi.Request { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for WithContext") + } + + var r0 wasabi.Request + if rf, ok := ret.Get(0).(func(context.Context) wasabi.Request); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(wasabi.Request) + } + } + + return r0 +} + +// MockRequest_WithContext_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'WithContext' +type MockRequest_WithContext_Call struct { + *mock.Call +} + +// WithContext is a helper method to define mock.On call +// - ctx context.Context +func (_e *MockRequest_Expecter) WithContext(ctx interface{}) *MockRequest_WithContext_Call { + return &MockRequest_WithContext_Call{Call: _e.mock.On("WithContext", ctx)} +} + +func (_c *MockRequest_WithContext_Call) Run(run func(ctx context.Context)) *MockRequest_WithContext_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context)) + }) + return _c +} + +func (_c *MockRequest_WithContext_Call) Return(_a0 wasabi.Request) *MockRequest_WithContext_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockRequest_WithContext_Call) RunAndReturn(run func(context.Context) wasabi.Request) *MockRequest_WithContext_Call { + _c.Call.Return(run) + return _c +} + +// NewMockRequest creates a new instance of MockRequest. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockRequest(t interface { + mock.TestingT + Cleanup(func()) +}) *MockRequest { + mock := &MockRequest{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +}