diff --git a/internal/handler/mock/middleware.go b/internal/handler/mock/handler.go similarity index 61% rename from internal/handler/mock/middleware.go rename to internal/handler/mock/handler.go index 56eac0bc..ec60cf5e 100644 --- a/internal/handler/mock/middleware.go +++ b/internal/handler/mock/handler.go @@ -10,29 +10,29 @@ import ( "github.com/spf13/afero" ) -type Middleware struct { +type Handler struct { response config.Response logger contracts.Logger fs afero.Fs after func(duration time.Duration) <-chan time.Time } -func NewMockMiddleware(options ...MiddlewareOption) *Middleware { - middleware := &Middleware{} +func NewMockHandler(options ...HandlerOption) *Handler { + handler := &Handler{} for _, option := range options { - option(middleware) + option(handler) } - return middleware + return handler } -func (m *Middleware) ServeHTTP(writer *contracts.ResponseWriter, request *contracts.Request) { - response := m.response +func (h *Handler) ServeHTTP(writer *contracts.ResponseWriter, request *contracts.Request) { + response := h.response header := writer.Header() if response.Delay > 0 { - m.logger.Debugf("Delay %s for %s", response.Delay, request.URL.RequestURI()) + h.logger.Debugf("Delay %s for %s", response.Delay, request.URL.RequestURI()) ctx := request.Context() url := request.URL.RequestURI() @@ -41,11 +41,11 @@ func (m *Middleware) ServeHTTP(writer *contracts.ResponseWriter, request *contra select { case <-ctx.Done(): writer.WriteHeader(http.StatusServiceUnavailable) - m.logger.Debugf("Delay is canceled (url: %s)", url) + h.logger.Debugf("Delay is canceled (url: %s)", url) return - case <-m.after(response.Delay): - m.logger.Debugf("Delay is complete (url: %s)", url) + case <-h.after(response.Delay): + h.logger.Debugf("Delay is complete (url: %s)", url) break waitingLoop } @@ -57,18 +57,18 @@ func (m *Middleware) ServeHTTP(writer *contracts.ResponseWriter, request *contra header.Set(key, value) } - if len(m.response.File) > 0 { - err := m.serveFileContent(writer, request) + if len(h.response.File) > 0 { + err := h.serveFileContent(writer, request) if err != nil { infra.HTTPError(writer, err) return } } else { - m.serveRawContent(writer) + h.serveRawContent(writer) } - m.logger.PrintResponse(&http.Response{ + h.logger.PrintResponse(&http.Response{ Request: request, StatusCode: response.Code, }) diff --git a/internal/handler/mock/handler_options.go b/internal/handler/mock/handler_options.go new file mode 100644 index 00000000..61e71f57 --- /dev/null +++ b/internal/handler/mock/handler_options.go @@ -0,0 +1,35 @@ +package mock + +import ( + "time" + + "github.com/evg4b/uncors/internal/config" + "github.com/evg4b/uncors/internal/contracts" + "github.com/spf13/afero" +) + +type HandlerOption = func(*Handler) + +func WithLogger(logger contracts.Logger) HandlerOption { + return func(h *Handler) { + h.logger = logger + } +} + +func WithResponse(response config.Response) HandlerOption { + return func(h *Handler) { + h.response = response + } +} + +func WithFileSystem(fs afero.Fs) HandlerOption { + return func(h *Handler) { + h.fs = fs + } +} + +func WithAfter(after func(duration time.Duration) <-chan time.Time) HandlerOption { + return func(h *Handler) { + h.after = after + } +} diff --git a/internal/handler/mock/middleware_test.go b/internal/handler/mock/handler_test.go similarity index 97% rename from internal/handler/mock/middleware_test.go rename to internal/handler/mock/handler_test.go index 17a7b63d..1c2f134b 100644 --- a/internal/handler/mock/middleware_test.go +++ b/internal/handler/mock/handler_test.go @@ -64,7 +64,7 @@ func TestHandler(t *testing.T) { } for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { - handler := mock.NewMockMiddleware( + handler := mock.NewMockHandler( mock.WithLogger(mocks.NewNoopLogger(t)), mock.WithResponse(testCase.response), mock.WithFileSystem(fileSystem), @@ -132,7 +132,7 @@ func TestHandler(t *testing.T) { } for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { - handler := mock.NewMockMiddleware( + handler := mock.NewMockHandler( mock.WithLogger(mocks.NewNoopLogger(t)), mock.WithResponse(testCase.response), mock.WithFileSystem(fileSystem), @@ -221,7 +221,7 @@ func TestHandler(t *testing.T) { } for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { - handler := mock.NewMockMiddleware( + handler := mock.NewMockHandler( mock.WithLogger(mocks.NewNoopLogger(t)), mock.WithResponse(testCase.response), mock.WithFileSystem(fileSystem), @@ -269,7 +269,7 @@ func TestHandler(t *testing.T) { } for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { - handler := mock.NewMockMiddleware( + handler := mock.NewMockHandler( mock.WithLogger(mocks.NewNoopLogger(t)), mock.WithResponse(testCase.response), mock.WithFileSystem(fileSystem), @@ -341,7 +341,7 @@ func TestHandler(t *testing.T) { for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { called := false - handler := mock.NewMockMiddleware( + handler := mock.NewMockHandler( mock.WithLogger(mocks.NewNoopLogger(t)), mock.WithResponse(testCase.response), mock.WithFileSystem(fileSystem), @@ -364,7 +364,7 @@ func TestHandler(t *testing.T) { }) t.Run("correctly cancel delay", func(t *testing.T) { - handler := mock.NewMockMiddleware( + handler := mock.NewMockHandler( mock.WithLogger(mocks.NewNoopLogger(t)), mock.WithResponse(config.Response{ Code: http.StatusOK, diff --git a/internal/handler/mock/middleware_options.go b/internal/handler/mock/middleware_options.go deleted file mode 100644 index 688e2328..00000000 --- a/internal/handler/mock/middleware_options.go +++ /dev/null @@ -1,35 +0,0 @@ -package mock - -import ( - "time" - - "github.com/evg4b/uncors/internal/config" - "github.com/evg4b/uncors/internal/contracts" - "github.com/spf13/afero" -) - -type MiddlewareOption = func(*Middleware) - -func WithLogger(logger contracts.Logger) MiddlewareOption { - return func(m *Middleware) { - m.logger = logger - } -} - -func WithResponse(response config.Response) MiddlewareOption { - return func(m *Middleware) { - m.response = response - } -} - -func WithFileSystem(fs afero.Fs) MiddlewareOption { - return func(m *Middleware) { - m.fs = fs - } -} - -func WithAfter(after func(duration time.Duration) <-chan time.Time) MiddlewareOption { - return func(m *Middleware) { - m.after = after - } -} diff --git a/internal/handler/mock/serve_file_content.go b/internal/handler/mock/serve_file_content.go index eb5377f6..1c85f20e 100644 --- a/internal/handler/mock/serve_file_content.go +++ b/internal/handler/mock/serve_file_content.go @@ -6,9 +6,9 @@ import ( "os" ) -func (m *Middleware) serveFileContent(writer http.ResponseWriter, request *http.Request) error { - fileName := m.response.File - file, err := m.fs.OpenFile(fileName, os.O_RDONLY, os.ModePerm) +func (h *Handler) serveFileContent(writer http.ResponseWriter, request *http.Request) error { + fileName := h.response.File + file, err := h.fs.OpenFile(fileName, os.O_RDONLY, os.ModePerm) if err != nil { return fmt.Errorf("filed to opent file %s: %w", fileName, err) } diff --git a/internal/handler/mock/serve_raw_content.go b/internal/handler/mock/serve_raw_content.go index c0db4cbb..499673f6 100644 --- a/internal/handler/mock/serve_raw_content.go +++ b/internal/handler/mock/serve_raw_content.go @@ -7,8 +7,8 @@ import ( "github.com/go-http-utils/headers" ) -func (m *Middleware) serveRawContent(writer http.ResponseWriter) { - response := m.response +func (h *Handler) serveRawContent(writer http.ResponseWriter) { + response := h.response header := writer.Header() if len(header.Get(headers.ContentType)) == 0 { contentType := http.DetectContentType([]byte(response.Raw)) diff --git a/internal/handler/mocked_routes.go b/internal/handler/mocked_routes.go index 4e5c4b39..d8f72004 100644 --- a/internal/handler/mocked_routes.go +++ b/internal/handler/mocked_routes.go @@ -5,7 +5,7 @@ import ( "github.com/gorilla/mux" ) -func (m *RequestHandler) makeMockedRoutes(router *mux.Router, mocks config.Mocks) { +func (h *RequestHandler) makeMockedRoutes(router *mux.Router, mocks config.Mocks) { var defaultMocks config.Mocks for _, mockDef := range mocks { @@ -15,7 +15,7 @@ func (m *RequestHandler) makeMockedRoutes(router *mux.Router, mocks config.Mocks setMethod(route, mockDef.Method) setQueries(route, mockDef.Queries) setHeaders(route, mockDef.Headers) - route.Handler(m.createHandler(mockDef.Response)) + route.Handler(h.createHandler(mockDef.Response)) } else { defaultMocks = append(defaultMocks, mockDef) } @@ -24,6 +24,6 @@ func (m *RequestHandler) makeMockedRoutes(router *mux.Router, mocks config.Mocks for _, mockDef := range defaultMocks { route := router.NewRoute() setPath(route, mockDef.Path) - route.Handler(m.createHandler(mockDef.Response)) + route.Handler(h.createHandler(mockDef.Response)) } } diff --git a/internal/handler/proxy/middleware.go b/internal/handler/proxy/handler.go similarity index 76% rename from internal/handler/proxy/middleware.go rename to internal/handler/proxy/handler.go index f4afdcd6..7cb0762f 100644 --- a/internal/handler/proxy/middleware.go +++ b/internal/handler/proxy/handler.go @@ -31,35 +31,35 @@ func NewProxyHandler(options ...HandlerOption) *Handler { return middleware } -func (m *Handler) ServeHTTP(response *contracts.ResponseWriter, request *contracts.Request) { - if err := m.handle(response, request); err != nil { +func (h *Handler) ServeHTTP(response *contracts.ResponseWriter, request *contracts.Request) { + if err := h.handle(response, request); err != nil { infra.HTTPError(response, err) } } -func (m *Handler) handle(resp http.ResponseWriter, req *http.Request) error { +func (h *Handler) handle(resp http.ResponseWriter, req *http.Request) error { if strings.EqualFold(req.Method, http.MethodOptions) { - return m.makeOptionsResponse(resp, req) + return h.makeOptionsResponse(resp, req) } - targetReplacer, sourceReplacer, err := m.replacers.Make(req.URL) + targetReplacer, sourceReplacer, err := h.replacers.Make(req.URL) if err != nil { return fmt.Errorf("failed to transform general url: %w", err) } - originalRequest, err := m.makeOriginalRequest(req, targetReplacer) + originalRequest, err := h.makeOriginalRequest(req, targetReplacer) if err != nil { return fmt.Errorf("failed to create reuest to original source: %w", err) } - originalResponse, err := m.executeQuery(originalRequest) + originalResponse, err := h.executeQuery(originalRequest) if err != nil { return err } defer helpers.CloseSafe(originalResponse.Body) - err = m.makeUncorsResponse(originalResponse, resp, sourceReplacer) + err = h.makeUncorsResponse(originalResponse, resp, sourceReplacer) if err != nil { return fmt.Errorf("failed to make uncors response: %w", err) } @@ -67,12 +67,12 @@ func (m *Handler) handle(resp http.ResponseWriter, req *http.Request) error { return nil } -func (m *Handler) executeQuery(request *http.Request) (*http.Response, error) { - originalResponse, err := m.http.Do(request) +func (h *Handler) executeQuery(request *http.Request) (*http.Response, error) { + originalResponse, err := h.http.Do(request) if err != nil { return nil, fmt.Errorf("failed to do reuest: %w", err) } - m.logger.PrintResponse(originalResponse) + h.logger.PrintResponse(originalResponse) return originalResponse, nil } diff --git a/internal/handler/proxy/middleware_test.go b/internal/handler/proxy/handler_test.go similarity index 93% rename from internal/handler/proxy/middleware_test.go rename to internal/handler/proxy/handler_test.go index def0bf7c..9e38ac47 100644 --- a/internal/handler/proxy/middleware_test.go +++ b/internal/handler/proxy/handler_test.go @@ -21,7 +21,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestProxyMiddleware(t *testing.T) { +func TestProxyHandler(t *testing.T) { replacerFactory, err := urlreplacer.NewURLReplacerFactory(config.Mappings{ {From: "http://premium.local.com", To: "https://premium.api.com"}, }) @@ -66,7 +66,7 @@ func TestProxyMiddleware(t *testing.T) { } }) - proc := proxy.NewProxyHandler( + handler := proxy.NewProxyHandler( proxy.WithHTTPClient(httpClient), proxy.WithURLReplacerFactory(replacerFactory), proxy.WithLogger(mocks.NewNoopLogger(t)), @@ -81,7 +81,7 @@ func TestProxyMiddleware(t *testing.T) { req.Header.Add(testCase.headerKey, testCase.URL) - proc.ServeHTTP(contracts.WrapResponseWriter(httptest.NewRecorder()), req) + handler.ServeHTTP(contracts.WrapResponseWriter(httptest.NewRecorder()), req) }) } }) @@ -119,7 +119,7 @@ func TestProxyMiddleware(t *testing.T) { } }) - proc := proxy.NewProxyHandler( + handler := proxy.NewProxyHandler( proxy.WithHTTPClient(httpClient), proxy.WithURLReplacerFactory(replacerFactory), proxy.WithLogger(mocks.NewNoopLogger(t)), @@ -134,7 +134,7 @@ func TestProxyMiddleware(t *testing.T) { recorder := httptest.NewRecorder() - proc.ServeHTTP(contracts.WrapResponseWriter(recorder), req) + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), req) assert.Equal(t, testCase.expectedURL, recorder.Header().Get(testCase.headerKey)) }) @@ -153,7 +153,7 @@ func TestProxyMiddleware(t *testing.T) { } }) - proc := proxy.NewProxyHandler( + handler := proxy.NewProxyHandler( proxy.WithHTTPClient(httpClient), proxy.WithURLReplacerFactory(replacerFactory), proxy.WithLogger(mocks.NewNoopLogger(t)), @@ -167,7 +167,7 @@ func TestProxyMiddleware(t *testing.T) { recorder := httptest.NewRecorder() - proc.ServeHTTP(contracts.WrapResponseWriter(recorder), req) + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), req) header := recorder.Header() assert.Equal(t, "*", header.Get(headers.AccessControlAllowOrigin)) @@ -180,7 +180,7 @@ func TestProxyMiddleware(t *testing.T) { }) t.Run("OPTIONS request handling", func(t *testing.T) { - middleware := proxy.NewProxyHandler( + handler := proxy.NewProxyHandler( proxy.WithHTTPClient(http.DefaultClient), proxy.WithURLReplacerFactory(replacerFactory), proxy.WithLogger(mocks.NewNoopLogger(t)), @@ -241,7 +241,7 @@ func TestProxyMiddleware(t *testing.T) { req, err := http.NewRequestWithContext(context.TODO(), http.MethodOptions, "/", nil) testutils.CheckNoError(t, err) - middleware.ServeHTTP(contracts.WrapResponseWriter(recorder), req) + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), req) assert.Equal(t, http.StatusOK, recorder.Code) assert.Equal(t, testCase.expected, recorder.Header()) diff --git a/internal/handler/proxy/option.go b/internal/handler/proxy/option.go index 87c55677..a7f08e11 100644 --- a/internal/handler/proxy/option.go +++ b/internal/handler/proxy/option.go @@ -6,9 +6,9 @@ import ( "github.com/evg4b/uncors/internal/infra" ) -func (m *Handler) makeOptionsResponse(writer http.ResponseWriter, req *http.Request) error { +func (h *Handler) makeOptionsResponse(writer http.ResponseWriter, req *http.Request) error { infra.WriteCorsHeaders(writer.Header()) - m.logger.PrintResponse(&http.Response{ + h.logger.PrintResponse(&http.Response{ StatusCode: http.StatusOK, Request: req, }) diff --git a/internal/handler/proxy/request.go b/internal/handler/proxy/request.go index d585e838..eee14160 100644 --- a/internal/handler/proxy/request.go +++ b/internal/handler/proxy/request.go @@ -8,7 +8,7 @@ import ( "github.com/go-http-utils/headers" ) -func (m *Handler) makeOriginalRequest( +func (h *Handler) makeOriginalRequest( req *http.Request, replacer *urlreplacer.Replacer, ) (*http.Request, error) { diff --git a/internal/handler/proxy/responce.go b/internal/handler/proxy/responce.go index a87a2b3d..de2059e4 100644 --- a/internal/handler/proxy/responce.go +++ b/internal/handler/proxy/responce.go @@ -10,7 +10,7 @@ import ( "github.com/go-http-utils/headers" ) -func (m *Handler) makeUncorsResponse( +func (h *Handler) makeUncorsResponse( original *http.Response, target http.ResponseWriter, replacer *urlreplacer.Replacer, diff --git a/internal/handler/static/file.go b/internal/handler/static/file.go index 863cd763..229a911b 100644 --- a/internal/handler/static/file.go +++ b/internal/handler/static/file.go @@ -11,14 +11,14 @@ import ( var errNorHandled = errors.New("request is not handled") -func (m *Middleware) openFile(filePath string) (afero.File, os.FileInfo, error) { - file, err := m.fs.Open(filePath) +func (h *Handler) openFile(filePath string) (afero.File, os.FileInfo, error) { + file, err := h.fs.Open(filePath) if err != nil { if !errors.Is(err, fs.ErrNotExist) { return nil, nil, fmt.Errorf("filed to open file: %w", err) } - indexFile, err := m.openIndexFile() + indexFile, err := h.openIndexFile() if err != nil { return nil, nil, err } @@ -32,7 +32,7 @@ func (m *Middleware) openFile(filePath string) (afero.File, os.FileInfo, error) } if stat.IsDir() { - indexFile, err := m.openIndexFile() + indexFile, err := h.openIndexFile() if err != nil { return file, stat, err } @@ -49,12 +49,12 @@ func (m *Middleware) openFile(filePath string) (afero.File, os.FileInfo, error) return file, stat, nil } -func (m *Middleware) openIndexFile() (afero.File, error) { - if len(m.index) == 0 { +func (h *Handler) openIndexFile() (afero.File, error) { + if len(h.index) == 0 { return nil, errNorHandled } - file, err := m.fs.Open(m.index) + file, err := h.fs.Open(h.index) if err != nil { return nil, fmt.Errorf("filed to opend index file: %w", err) } diff --git a/internal/handler/static/middleware.go b/internal/handler/static/middleware.go index b7fcfdf6..afbf01a0 100644 --- a/internal/handler/static/middleware.go +++ b/internal/handler/static/middleware.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/afero" ) -type Middleware struct { +type Handler struct { fs afero.Fs next contracts.Handler index string @@ -20,26 +20,26 @@ type Middleware struct { prefix string } -func NewStaticMiddleware(options ...MiddlewareOption) *Middleware { - middleware := &Middleware{} +func NewStaticHandler(options ...HandlerOption) *Handler { + handler := &Handler{} for _, option := range options { - option(middleware) + option(handler) } - return middleware + return handler } -func (m *Middleware) ServeHTTP(writer *contracts.ResponseWriter, request *contracts.Request) { +func (h *Handler) ServeHTTP(writer *contracts.ResponseWriter, request *contracts.Request) { response := contracts.WrapResponseWriter(writer) - filePath := m.extractFilePath(request) - file, stat, err := m.openFile(filePath) + filePath := h.extractFilePath(request) + file, stat, err := h.openFile(filePath) defer helpers.CloseSafe(file) if err != nil { if errors.Is(err, errNorHandled) { - m.next.ServeHTTP(response, request) + h.next.ServeHTTP(response, request) } else { infra.HTTPError(response, err) } @@ -48,14 +48,14 @@ func (m *Middleware) ServeHTTP(writer *contracts.ResponseWriter, request *contra } http.ServeContent(response, request, stat.Name(), stat.ModTime(), file) - m.logger.PrintResponse(&http.Response{ + h.logger.PrintResponse(&http.Response{ StatusCode: response.StatusCode, Request: request, }) } -func (m *Middleware) extractFilePath(request *http.Request) string { - filePath := strings.TrimPrefix(request.URL.Path, m.prefix) +func (h *Handler) extractFilePath(request *http.Request) string { + filePath := strings.TrimPrefix(request.URL.Path, h.prefix) if !strings.HasPrefix(filePath, "/") { filePath = "/" + filePath } diff --git a/internal/handler/static/middleware_options.go b/internal/handler/static/middleware_options.go index 99914b27..5e4833a9 100644 --- a/internal/handler/static/middleware_options.go +++ b/internal/handler/static/middleware_options.go @@ -5,34 +5,34 @@ import ( "github.com/spf13/afero" ) -type MiddlewareOption = func(*Middleware) +type HandlerOption = func(*Handler) -func WithFileSystem(fs afero.Fs) MiddlewareOption { - return func(m *Middleware) { - m.fs = fs +func WithFileSystem(fs afero.Fs) HandlerOption { + return func(h *Handler) { + h.fs = fs } } -func WithIndex(index string) MiddlewareOption { - return func(m *Middleware) { - m.index = index +func WithIndex(index string) HandlerOption { + return func(h *Handler) { + h.index = index } } -func WithNext(next contracts.Handler) MiddlewareOption { - return func(m *Middleware) { - m.next = next +func WithNext(next contracts.Handler) HandlerOption { + return func(h *Handler) { + h.next = next } } -func WithLogger(logger contracts.Logger) MiddlewareOption { - return func(m *Middleware) { - m.logger = logger +func WithLogger(logger contracts.Logger) HandlerOption { + return func(h *Handler) { + h.logger = logger } } -func WithPrefix(prefix string) MiddlewareOption { - return func(m *Middleware) { - m.prefix = prefix +func WithPrefix(prefix string) HandlerOption { + return func(h *Handler) { + h.prefix = prefix } } diff --git a/internal/handler/static/middleware_test.go b/internal/handler/static/middleware_test.go index daa551dc..bbf7600a 100644 --- a/internal/handler/static/middleware_test.go +++ b/internal/handler/static/middleware_test.go @@ -99,7 +99,7 @@ func TestMiddleware(t *testing.T) { t.Run("index file is not configured", func(t *testing.T) { const testHTTPStatusCode = 999 const testHTTPBody = "this is tests response" - middleware := static.NewStaticMiddleware( + handler := static.NewStaticHandler( static.WithFileSystem(fs), static.WithLogger(loggerMock), static.WithNext(contracts.HandlerFunc(func(writer *contracts.ResponseWriter, _ *contracts.Request) { @@ -115,7 +115,7 @@ func TestMiddleware(t *testing.T) { requestURI, err := url.Parse(testCase.path) testutils.CheckNoError(t, err) - middleware.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ Method: http.MethodGet, URL: requestURI, }) @@ -133,7 +133,7 @@ func TestMiddleware(t *testing.T) { requestURI, err := url.Parse(testCase.path) testutils.CheckNoError(t, err) - middleware.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ Method: http.MethodGet, URL: requestURI, }) @@ -146,7 +146,7 @@ func TestMiddleware(t *testing.T) { }) t.Run("index file is configured", func(t *testing.T) { - middleware := static.NewStaticMiddleware( + handler := static.NewStaticHandler( static.WithFileSystem(fs), static.WithLogger(loggerMock), static.WithIndex(indexHTML), @@ -160,7 +160,7 @@ func TestMiddleware(t *testing.T) { requestURI, err := url.Parse(testCase.path) testutils.CheckNoError(t, err) - middleware.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ Method: http.MethodGet, URL: requestURI, }) @@ -178,7 +178,7 @@ func TestMiddleware(t *testing.T) { requestURI, err := url.Parse(testCase.path) testutils.CheckNoError(t, err) - middleware.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ Method: http.MethodGet, URL: requestURI, }) @@ -190,7 +190,7 @@ func TestMiddleware(t *testing.T) { }) t.Run("index file doesn't exists", func(t *testing.T) { - middleware := static.NewStaticMiddleware( + handler := static.NewStaticHandler( static.WithFileSystem(fs), static.WithLogger(loggerMock), static.WithIndex("/not-exists.html"), @@ -201,7 +201,7 @@ func TestMiddleware(t *testing.T) { requestURI, err := url.Parse("/options/") testutils.CheckNoError(t, err) - middleware.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ + handler.ServeHTTP(contracts.WrapResponseWriter(recorder), &http.Request{ Method: http.MethodGet, URL: requestURI, }) diff --git a/internal/handler/static_routes.go b/internal/handler/static_routes.go index e2b98957..9658c25a 100644 --- a/internal/handler/static_routes.go +++ b/internal/handler/static_routes.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/afero" ) -func (m *RequestHandler) makeStaticRoutes( +func (h *RequestHandler) makeStaticRoutes( router *mux.Router, statics config.StaticDirectories, next contracts.Handler, @@ -26,8 +26,8 @@ func (m *RequestHandler) makeStaticRoutes( Handler(http.RedirectHandler(path, http.StatusTemporaryRedirect)) route := router.NewRoute() - handler := static.NewStaticMiddleware( - static.WithFileSystem(afero.NewBasePathFs(m.fs, staticDir.Dir)), + handler := static.NewStaticHandler( + static.WithFileSystem(afero.NewBasePathFs(h.fs, staticDir.Dir)), static.WithIndex(staticDir.Index), static.WithNext(next), static.WithLogger(ui.StaticLogger), diff --git a/internal/handler/uncors_handler.go b/internal/handler/uncors_handler.go index 699b0d59..d407c0d9 100644 --- a/internal/handler/uncors_handler.go +++ b/internal/handler/uncors_handler.go @@ -70,16 +70,16 @@ func NewUncorsRequestHandler(options ...UncorsRequestHandlerOption) *RequestHand return handler } -func (m *RequestHandler) ServeHTTP(writer *contracts.ResponseWriter, request *contracts.Request) { - m.router.ServeHTTP(writer, request) +func (h *RequestHandler) ServeHTTP(writer *contracts.ResponseWriter, request *contracts.Request) { + h.router.ServeHTTP(writer, request) } -func (m *RequestHandler) createHandler(response config.Response) http.Handler { +func (h *RequestHandler) createHandler(response config.Response) http.Handler { return contracts.CastToHTTPHandler( - mock.NewMockMiddleware( + mock.NewMockHandler( mock.WithLogger(ui.MockLogger), mock.WithResponse(response), - mock.WithFileSystem(m.fs), + mock.WithFileSystem(h.fs), mock.WithAfter(time.After), ), ) diff --git a/internal/handler/uncors_handler_options.go b/internal/handler/uncors_handler_options.go index ac3bdaec..7fd10294 100644 --- a/internal/handler/uncors_handler_options.go +++ b/internal/handler/uncors_handler_options.go @@ -9,31 +9,31 @@ import ( type UncorsRequestHandlerOption = func(*RequestHandler) func WithLogger(logger contracts.Logger) UncorsRequestHandlerOption { - return func(m *RequestHandler) { - m.logger = logger + return func(h *RequestHandler) { + h.logger = logger } } func WithFileSystem(fs afero.Fs) UncorsRequestHandlerOption { - return func(m *RequestHandler) { - m.fs = fs + return func(h *RequestHandler) { + h.fs = fs } } func WithURLReplacerFactory(replacerFactory contracts.URLReplacerFactory) UncorsRequestHandlerOption { - return func(m *RequestHandler) { - m.replacerFactory = replacerFactory + return func(h *RequestHandler) { + h.replacerFactory = replacerFactory } } func WithHTTPClient(client contracts.HTTPClient) UncorsRequestHandlerOption { - return func(m *RequestHandler) { - m.httpClient = client + return func(h *RequestHandler) { + h.httpClient = client } } func WithMappings(mappings config.Mappings) UncorsRequestHandlerOption { - return func(m *RequestHandler) { - m.mappings = mappings + return func(h *RequestHandler) { + h.mappings = mappings } } diff --git a/internal/handler/uncors_handler_test.go b/internal/handler/uncors_handler_test.go index 60d692d3..213b24b9 100644 --- a/internal/handler/uncors_handler_test.go +++ b/internal/handler/uncors_handler_test.go @@ -116,7 +116,7 @@ func TestUncorsRequestHandler(t *testing.T) { panic(sfmt.Sprintf("incorrect request: %s", request.URL.Path)) }) - hand := handler.NewUncorsRequestHandler( + uncorsHandler := handler.NewUncorsRequestHandler( handler.WithLogger(mocks.NewLoggerMock(t)), handler.WithFileSystem(fs), handler.WithURLReplacerFactory(factory), @@ -154,7 +154,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, testCase.url, nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, 200, recorder.Code) assert.Equal(t, testCase.expected, testutils.ReadBody(t, recorder)) @@ -167,7 +167,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "http://localhost/cc/unknown.html", nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, http.StatusOK, recorder.Code) assert.Equal(t, indexHTML, testutils.ReadBody(t, recorder)) @@ -178,7 +178,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "http://localhost/pnp/unknown.html", nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, http.StatusInternalServerError, recorder.Code) expectedMessage := "filed to opend index file: open /assets/index.php: file does not exist" @@ -210,7 +210,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, testCase.url, nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, http.StatusOK, recorder.Code) assert.Equal(t, testCase.expected, testutils.ReadBody(t, recorder)) @@ -223,7 +223,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "http://localhost/img/original.png", nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, http.StatusOK, recorder.Code) assert.Equal(t, "original.png", testutils.ReadBody(t, recorder)) @@ -264,7 +264,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, testCase.url, nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, testCase.expectedCode, recorder.Code) assert.Equal(t, testCase.expected, testutils.ReadBody(t, recorder)) @@ -277,7 +277,7 @@ func TestUncorsRequestHandler(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "http://localhost/api/mocks/4", nil) helpers.NormaliseRequest(request) - hand.ServeHTTP(contracts.WrapResponseWriter(recorder), request) + uncorsHandler.ServeHTTP(contracts.WrapResponseWriter(recorder), request) assert.Equal(t, http.StatusInternalServerError, recorder.Code) expectedMessage := "filed to opent file /unknown.json: open /unknown.json: file does not exist"