From 2082357b9c18209cd9fd750497301b83bac44d02 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Tue, 10 Oct 2023 12:02:32 -0400 Subject: [PATCH] Add tests --- service/cachemdw/middleware_test.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/service/cachemdw/middleware_test.go b/service/cachemdw/middleware_test.go index 33f1eb6..d434928 100644 --- a/service/cachemdw/middleware_test.go +++ b/service/cachemdw/middleware_test.go @@ -15,7 +15,7 @@ import ( "github.com/kava-labs/kava-proxy-service/service" ) -func TestServiceCacheMiddleware(t *testing.T) { +func TestE2ETestServiceCacheMiddleware(t *testing.T) { logger, err := logging.New("TRACE") require.NoError(t, err) @@ -68,6 +68,33 @@ func TestServiceCacheMiddleware(t *testing.T) { }) } +func TestE2ETestServiceCacheMiddlewareInvalidRequestBody(t *testing.T) { + logger, err := logging.New("TRACE") + require.NoError(t, err) + + inMemoryCache := cache.NewInMemoryCache() + evmClient := NewMockEVMClient() + cacheTTL := time.Duration(0) // TTL: no expiry + + serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey, &logger) + + req, err := http.NewRequest(http.MethodPost, "/test", nil) + require.NoError(t, err) + req = req.WithContext(context.WithValue(req.Context(), service.DecodedRequestContextKey, "invalid")) + + recorder := httptest.NewRecorder() + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("test response")) + }) + + serviceCache.Middleware(handler).ServeHTTP(recorder, req) + + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, "test response", recorder.Body.String()) + require.Empty(t, recorder.Header().Get(CacheHeaderKey)) +} + func createTestHttpRequest( t *testing.T, url string,