Skip to content

Commit c9f695f

Browse files
committed
gorrila/mux.SQLCommenterMiddleware test
1 parent 743b18c commit c9f695f

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

go/gorrila/mux/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/google/sqlcommenter/go/gorrila/mux
2+
3+
go 1.19
4+
5+
require (
6+
github.com/google/sqlcommenter/go/core v0.0.3-beta
7+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta
8+
github.com/gorilla/mux v1.8.0
9+
)
10+
11+
require (
12+
go.opentelemetry.io/otel v1.11.1 // indirect
13+
go.opentelemetry.io/otel/trace v1.11.1 // indirect
14+
)

go/gorrila/mux/go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/google/sqlcommenter/go/core v0.0.3-beta h1:gWUfq/UyMPEmHpAyhjcsGGAXRCV2fjB6e6F8CPedlnU=
2+
github.com/google/sqlcommenter/go/core v0.0.3-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70=
3+
github.com/google/sqlcommenter/go/net/http v0.0.2-beta/go.mod h1:1sd6t92iCHaNQc/v5qxTHp+td7KNoD8IIeG4BRetFZo=
4+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta h1:IE/vO3xKddn/2Bq3k+hSy4CxcEuvE1lUiIDYTXjApzA=
5+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta/go.mod h1:duXQQvXZYCX8eQ+XOrlojWF512ltEp1eSKXc/KiS9lg=
6+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
7+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
8+
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
9+
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
10+
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
11+
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
12+
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
13+
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
14+
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
15+
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=

go/gorrila/mux/mux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mux
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/google/sqlcommenter/go/core"
7+
httpnet "github.com/google/sqlcommenter/go/net/http"
8+
"github.com/gorilla/mux"
9+
)
10+
11+
func SQLCommenterMiddleware(h http.Handler) http.Handler {
12+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13+
route := mux.CurrentRoute(r)
14+
pathTemplate, err := route.GetPathTemplate()
15+
if err != nil {
16+
pathTemplate = ""
17+
}
18+
19+
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestTags("gorrila/mux", pathTemplate, core.GetFunctionName(route.GetHandler())))
20+
h.ServeHTTP(w, r.WithContext(ctx))
21+
})
22+
}

go/gorrila/mux/mux_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mux
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/google/sqlcommenter/go/core"
9+
"github.com/gorilla/mux"
10+
)
11+
12+
func TestSQLCommenterMiddleware(t *testing.T) {
13+
framework := "gorrila/mux"
14+
route := "/test/{id}"
15+
action := "github.com/google/sqlcommenter/go/gorrila/mux.TestSQLCommenterMiddleware.func1"
16+
17+
mockHandler := func(w http.ResponseWriter, r *http.Request) {
18+
ctx := r.Context()
19+
_framework := ctx.Value(core.Framework)
20+
_route := ctx.Value(core.Route)
21+
_action := ctx.Value(core.Action)
22+
23+
if _framework != framework {
24+
t.Errorf("mismatched framework - got: %s, want: %s", _framework, framework)
25+
}
26+
27+
if _route != route {
28+
t.Errorf("mismatched route - got: %s, want: %s", _route, route)
29+
}
30+
31+
if _action != action {
32+
t.Errorf("mismatched action - got: %s, want: %s", _action, action)
33+
}
34+
}
35+
36+
router := mux.NewRouter()
37+
router.Use(SQLCommenterMiddleware)
38+
router.HandleFunc(route, mockHandler).Methods("GET")
39+
40+
rr := httptest.NewRecorder()
41+
req, err := http.NewRequest("GET", "/test/1", nil)
42+
43+
if err != nil {
44+
t.Errorf("error while building req: %v", err)
45+
}
46+
47+
router.ServeHTTP(rr, req)
48+
}

0 commit comments

Comments
 (0)