-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gorrila/mux.SQLCommenterMiddleware test
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/google/sqlcommenter/go/gorrila/mux | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/google/sqlcommenter/go/core v0.0.3-beta | ||
github.com/google/sqlcommenter/go/net/http v0.0.3-beta | ||
github.com/gorilla/mux v1.8.0 | ||
) | ||
|
||
require ( | ||
go.opentelemetry.io/otel v1.11.1 // indirect | ||
go.opentelemetry.io/otel/trace v1.11.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
github.com/google/sqlcommenter/go/core v0.0.3-beta h1:gWUfq/UyMPEmHpAyhjcsGGAXRCV2fjB6e6F8CPedlnU= | ||
github.com/google/sqlcommenter/go/core v0.0.3-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70= | ||
github.com/google/sqlcommenter/go/net/http v0.0.2-beta/go.mod h1:1sd6t92iCHaNQc/v5qxTHp+td7KNoD8IIeG4BRetFZo= | ||
github.com/google/sqlcommenter/go/net/http v0.0.3-beta h1:IE/vO3xKddn/2Bq3k+hSy4CxcEuvE1lUiIDYTXjApzA= | ||
github.com/google/sqlcommenter/go/net/http v0.0.3-beta/go.mod h1:duXQQvXZYCX8eQ+XOrlojWF512ltEp1eSKXc/KiS9lg= | ||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= | ||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= | ||
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4= | ||
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ= | ||
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4= | ||
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE= | ||
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E= | ||
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM= | ||
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ= | ||
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mux | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/sqlcommenter/go/core" | ||
httpnet "github.com/google/sqlcommenter/go/net/http" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func SQLCommenterMiddleware(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
route := mux.CurrentRoute(r) | ||
pathTemplate, err := route.GetPathTemplate() | ||
if err != nil { | ||
pathTemplate = "" | ||
} | ||
|
||
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestTags("gorrila/mux", pathTemplate, core.GetFunctionName(route.GetHandler()))) | ||
h.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mux | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/sqlcommenter/go/core" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func TestSQLCommenterMiddleware(t *testing.T) { | ||
framework := "gorrila/mux" | ||
route := "/test/{id}" | ||
action := "github.com/google/sqlcommenter/go/gorrila/mux.TestSQLCommenterMiddleware.func1" | ||
|
||
mockHandler := func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
_framework := ctx.Value(core.Framework) | ||
_route := ctx.Value(core.Route) | ||
_action := ctx.Value(core.Action) | ||
|
||
if _framework != framework { | ||
t.Errorf("mismatched framework - got: %s, want: %s", _framework, framework) | ||
} | ||
|
||
if _route != route { | ||
t.Errorf("mismatched route - got: %s, want: %s", _route, route) | ||
} | ||
|
||
if _action != action { | ||
t.Errorf("mismatched action - got: %s, want: %s", _action, action) | ||
} | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.Use(SQLCommenterMiddleware) | ||
router.HandleFunc(route, mockHandler).Methods("GET") | ||
|
||
rr := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/test/1", nil) | ||
|
||
if err != nil { | ||
t.Errorf("error while building req: %v", err) | ||
} | ||
|
||
router.ServeHTTP(rr, req) | ||
} |