diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..f545ab2 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,13 @@ +coverage: + precision: 2 + round: down + + status: + project: + default: + target: 80% + threshold: 1% + patch: + default: + target: 50% + threshold: 2% diff --git a/v2/httputil/util.go b/v2/httputil/util.go index bc2d3dc..e67c1c4 100644 --- a/v2/httputil/util.go +++ b/v2/httputil/util.go @@ -9,7 +9,7 @@ import ( func IsHTTPS(r *http.Request) bool { const protoHTTPS = "https" switch { - case r.URL.Scheme == protoHTTPS: + case r.URL != nil && r.URL.Scheme == protoHTTPS: return true case r.TLS != nil: return true diff --git a/v2/httputil/util_test.go b/v2/httputil/util_test.go new file mode 100644 index 0000000..e53f08b --- /dev/null +++ b/v2/httputil/util_test.go @@ -0,0 +1,50 @@ +package httputil + +import ( + "crypto/tls" + "net/http" + "net/url" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIsHTTPS(t *testing.T) { + tests := []struct { + name string + r *http.Request + expected bool + }{ + { + name: "HTTPS scheme", + r: &http.Request{URL: &url.URL{Scheme: "https"}}, + expected: true, + }, + { + name: "is TLS", + r: &http.Request{TLS: &tls.ConnectionState{}}, + expected: true, + }, + { + name: "HTTPS proto", + r: &http.Request{Proto: "HTTPS/1.1"}, + expected: true, + }, + { + name: "X-Forwarded-Proto", + r: &http.Request{Header: http.Header{"X-Forwarded-Proto": []string{"https"}}}, + expected: true, + }, + { + name: "not HTTPS", + r: &http.Request{}, + expected: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := IsHTTPS(tt.r) + require.Equal(t, tt.expected, got) + }) + } +} diff --git a/v2/sentryutil/sentry_test.go b/v2/sentryutil/sentry_test.go index c71e6b6..1ce7be8 100644 --- a/v2/sentryutil/sentry_test.go +++ b/v2/sentryutil/sentry_test.go @@ -4,11 +4,14 @@ import ( "bytes" "context" "fmt" + "net/http/httptest" "os" "strings" "testing" "github.com/getsentry/sentry-go" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -57,3 +60,63 @@ func TestRecoverWithContext(t *testing.T) { }) } } + +func TestGinWrapper(t *testing.T) { + tests := []struct { + name string + fn func(*gin.RouterGroup, string, ...gin.HandlerFunc) gin.IRoutes + }{ + { + name: "GET", + fn: GET, + }, + { + name: "POST", + fn: POST, + }, + { + name: "PUT", + fn: PUT, + }, + { + name: "DELETE", + fn: DELETE, + }, + { + name: "PATCH", + fn: PATCH, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := gin.New() + gr := r.Group("/") + tt.fn(gr, tt.name, func(c *gin.Context) { + span := sentry.SpanFromContext(c.Request.Context()) + assert.NotNil(t, span) + }) + + req := httptest.NewRequest(tt.name, "/", nil) + rr := httptest.NewRecorder() + r.ServeHTTP(rr, req) + }) + } +} + +func TestMakeSpan(t *testing.T) { + span := MakeSpan(context.Background(), 0) + assert.NotNil(t, span) +} + +func TestMakeTransaction(t *testing.T) { + ctx, span, hub := MakeTransaction(context.Background(), "test") + assert.NotNil(t, ctx) + assert.NotNil(t, span) + assert.NotNil(t, hub) +} + +func TestErrorDoesntPanic(t *testing.T) { + err := fmt.Errorf("test error") + Error(context.Background(), err) +}