|
| 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