Skip to content

Commit

Permalink
Improve gin transaction tests with different paths for router and req…
Browse files Browse the repository at this point in the history
…uest
  • Loading branch information
antonsacred committed Aug 14, 2023
1 parent 3b50c09 commit c1edc11
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions gin/sentrygin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ func TestIntegration(t *testing.T) {
largePayload := strings.Repeat("Large", 3*1024) // 15 KB

tests := []struct {
Path string
Method string
WantStatus int
Body string
Handler gin.HandlerFunc
RequestPath string
RoutePath string
Method string
WantStatus int
Body string
Handler gin.HandlerFunc

WantEvent *sentry.Event
WantTransaction *sentry.Event
}{
{
Path: "/panic/:id",
Method: "GET",
WantStatus: 200,
RequestPath: "/panic/1",
RoutePath: "/panic/:id",
Method: "GET",
WantStatus: 200,
Handler: func(c *gin.Context) {
panic("test")
},
Expand All @@ -42,7 +44,7 @@ func TestIntegration(t *testing.T) {
Type: "transaction",
Transaction: "GET /panic/:id",
Request: &sentry.Request{
URL: "/panic/:id",
URL: "/panic/1",
Method: "GET",
Headers: map[string]string{
"Accept-Encoding": "gzip",
Expand All @@ -55,7 +57,7 @@ func TestIntegration(t *testing.T) {
Level: sentry.LevelFatal,
Message: "test",
Request: &sentry.Request{
URL: "/panic/:id",
URL: "/panic/1",
Method: "GET",
Headers: map[string]string{
"Accept-Encoding": "gzip",
Expand All @@ -65,12 +67,11 @@ func TestIntegration(t *testing.T) {
},
},
{
Path: "/404/1",
Method: "GET",
WantStatus: 404,
Handler: func(c *gin.Context) {
c.AbortWithStatus(404)
},
RequestPath: "/404/1",
RoutePath: "/some-other-route",
Method: "GET",
WantStatus: 404,
Handler: nil,
WantTransaction: &sentry.Event{
Level: sentry.LevelInfo,
Type: "transaction",
Expand All @@ -85,12 +86,14 @@ func TestIntegration(t *testing.T) {
},
TransactionInfo: &sentry.TransactionInfo{Source: "url"},
},
WantEvent: nil,
},
{
Path: "/post",
Method: "POST",
WantStatus: 200,
Body: "payload",
RequestPath: "/post",
RoutePath: "/post",
Method: "POST",
WantStatus: 200,
Body: "payload",
Handler: func(c *gin.Context) {
hub := sentry.GetHubFromContext(c.Request.Context())
body, err := io.ReadAll(c.Request.Body)
Expand Down Expand Up @@ -132,9 +135,10 @@ func TestIntegration(t *testing.T) {
},
},
{
Path: "/get",
Method: "GET",
WantStatus: 200,
RequestPath: "/get",
RoutePath: "/get",
Method: "GET",
WantStatus: 200,
Handler: func(c *gin.Context) {
hub := sentry.GetHubFromContext(c.Request.Context())
hub.CaptureMessage("get")
Expand Down Expand Up @@ -168,10 +172,11 @@ func TestIntegration(t *testing.T) {
},
},
{
Path: "/post/large",
Method: "POST",
WantStatus: 200,
Body: largePayload,
RequestPath: "/post/large",
RoutePath: "/post/large",
Method: "POST",
WantStatus: 200,
Body: largePayload,
Handler: func(c *gin.Context) {
hub := sentry.GetHubFromContext(c.Request.Context())
body, err := io.ReadAll(c.Request.Body)
Expand Down Expand Up @@ -212,10 +217,11 @@ func TestIntegration(t *testing.T) {
},
},
{
Path: "/post/body-ignored",
Method: "POST",
WantStatus: 200,
Body: "client sends, server ignores, SDK doesn't read",
RequestPath: "/post/body-ignored",
RoutePath: "/post/body-ignored",
Method: "POST",
WantStatus: 200,
Body: "client sends, server ignores, SDK doesn't read",
Handler: func(c *gin.Context) {
hub := sentry.GetHubFromContext(c.Request.Context())
hub.CaptureMessage("body ignored")
Expand Down Expand Up @@ -254,9 +260,10 @@ func TestIntegration(t *testing.T) {
},
},
{
Path: "/badreq",
Method: "GET",
WantStatus: 400,
RequestPath: "/badreq",
RoutePath: "/badreq",
Method: "GET",
WantStatus: 400,
Handler: func(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"status": "bad_request"})
},
Expand Down Expand Up @@ -300,7 +307,7 @@ func TestIntegration(t *testing.T) {
router.Use(sentrygin.New(sentrygin.Options{}))

for _, tt := range tests {
router.Handle(tt.Method, tt.Path, tt.Handler)
router.Handle(tt.Method, tt.RoutePath, tt.Handler)
}

srv := httptest.NewServer(router)
Expand All @@ -325,7 +332,7 @@ func TestIntegration(t *testing.T) {
wanttrans = append(wanttrans, tt.WantTransaction)
wantCodes = append(wantCodes, sentry.HTTPtoSpanStatus(tt.WantStatus))

req, err := http.NewRequest(tt.Method, srv.URL+tt.Path, strings.NewReader(tt.Body))
req, err := http.NewRequest(tt.Method, srv.URL+tt.RequestPath, strings.NewReader(tt.Body))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c1edc11

Please sign in to comment.