shad-go/middleware/httpgauge/httpgauge_test.go
Sergey Paramoshkin dc939b6ed1
update repo
2023-10-02 22:32:41 +03:00

62 lines
1.4 KiB
Go

package httpgauge_test
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/require"
"gitlab.com/manytask/itmo-go/private/middleware/httpgauge"
)
func TestMiddleware(t *testing.T) {
g := httpgauge.New()
m := chi.NewRouter()
m.Use(g.Wrap)
m.Get("/simple", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
m.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("bug")
})
m.Get("/user/{userID}", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
m.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/simple", nil))
m.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/simple", nil))
require.Panics(t, func() {
m.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/panic", nil))
})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
m.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", fmt.Sprintf("/user/%d", j), nil))
}
}()
}
wg.Wait()
require.Equal(t, g.Snapshot(), map[string]int{
"/simple": 2,
"/panic": 1,
"/user/{userID}": 10000,
})
w := httptest.NewRecorder()
g.ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
require.Equal(t, w.Body.String(), "/panic 1\n/simple 2\n/user/{userID} 10000\n")
}