mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 16:42:26 +08:00
Alerting: Use mux router to match hooks, add support for path variables and methods (#79345)
* Use a router inside hooks rather than plain string matching * Add test for mismatched method
This commit is contained in:
@ -19,9 +19,9 @@ func TestHooks(t *testing.T) {
|
||||
invoked := false
|
||||
hook := func(*contextmodel.ReqContext) response.Response { invoked = true; return nil }
|
||||
|
||||
hooks.Set("/some/path", hook)
|
||||
hooks.Set("GET", "/some/path", hook)
|
||||
reqURL, _ := url.Parse("http://domain.test/some/path")
|
||||
handler, ok := hooks.Get(reqURL)
|
||||
handler, ok := hooks.Get("GET", reqURL)
|
||||
|
||||
require.True(t, ok, "hooks did not contain a matching hook for path")
|
||||
require.False(t, invoked, "hook was invoked earlier than expected")
|
||||
@ -33,9 +33,9 @@ func TestHooks(t *testing.T) {
|
||||
hooks := NewHooks(log.NewNopLogger())
|
||||
hook := func(*contextmodel.ReqContext) response.Response { return nil }
|
||||
|
||||
hooks.Set("/some/path", hook)
|
||||
hooks.Set("GET", "/some/path", hook)
|
||||
reqURL, _ := url.Parse("http://domain.test/does/not/match")
|
||||
handler, ok := hooks.Get(reqURL)
|
||||
handler, ok := hooks.Get("GET", reqURL)
|
||||
|
||||
require.False(t, ok, "hooks returned a hook when we expected it not to")
|
||||
require.Nil(t, handler)
|
||||
@ -45,9 +45,21 @@ func TestHooks(t *testing.T) {
|
||||
hooks := NewHooks(log.NewNopLogger())
|
||||
hook := func(*contextmodel.ReqContext) response.Response { return nil }
|
||||
|
||||
hooks.Set("/some/path", hook)
|
||||
hooks.Set("GET", "/some/path", hook)
|
||||
reqURL, _ := url.Parse("http://domain.test/some/path/with/more")
|
||||
handler, ok := hooks.Get(reqURL)
|
||||
handler, ok := hooks.Get("GET", reqURL)
|
||||
|
||||
require.False(t, ok, "hooks returned a hook when we expected it not to")
|
||||
require.Nil(t, handler)
|
||||
})
|
||||
|
||||
t.Run("hooks do not match requests with the wrong HTTP method", func(t *testing.T) {
|
||||
hooks := NewHooks(log.NewNopLogger())
|
||||
hook := func(*contextmodel.ReqContext) response.Response { return nil }
|
||||
|
||||
hooks.Set("POST", "/some/path", hook)
|
||||
reqURL, _ := url.Parse("http://domain.test/some/path")
|
||||
handler, ok := hooks.Get("GET", reqURL)
|
||||
|
||||
require.False(t, ok, "hooks returned a hook when we expected it not to")
|
||||
require.Nil(t, handler)
|
||||
@ -58,9 +70,9 @@ func TestHooks(t *testing.T) {
|
||||
invoked := false
|
||||
hook := func(*contextmodel.ReqContext) response.Response { invoked = true; return nil }
|
||||
|
||||
hooks.Set("/some/path", hook)
|
||||
hooks.Set("GET", "/some/path", hook)
|
||||
reqURL, _ := url.Parse("http://domain.test/some/path?query=param")
|
||||
handler, ok := hooks.Get(reqURL)
|
||||
handler, ok := hooks.Get("GET", reqURL)
|
||||
|
||||
require.True(t, ok, "hooks did not contain a matching hook for path")
|
||||
require.False(t, invoked, "hook was invoked earlier than expected")
|
||||
@ -68,16 +80,19 @@ func TestHooks(t *testing.T) {
|
||||
require.True(t, invoked, "the hook returned by get() was not invoked as expected")
|
||||
})
|
||||
|
||||
t.Run("hooks do not match routes with path variables", func(t *testing.T) {
|
||||
t.Run("hooks match routes with path variables", func(t *testing.T) {
|
||||
hooks := NewHooks(log.NewNopLogger())
|
||||
hook := func(*contextmodel.ReqContext) response.Response { return nil }
|
||||
invoked := false
|
||||
hook := func(*contextmodel.ReqContext) response.Response { invoked = true; return nil }
|
||||
|
||||
hooks.Set("/some/{value}", hook)
|
||||
hooks.Set("GET", "/some/{value}", hook)
|
||||
reqURL, _ := url.Parse("http://domain.test/some/123")
|
||||
handler, ok := hooks.Get(reqURL)
|
||||
handler, ok := hooks.Get("GET", reqURL)
|
||||
|
||||
require.False(t, ok, "hooks returned a hook when we expected it not to")
|
||||
require.Nil(t, handler)
|
||||
require.True(t, ok, "hooks did not contain a matching hook for path")
|
||||
require.False(t, invoked, "hook was invoked earlier than expected")
|
||||
handler(nil)
|
||||
require.True(t, invoked, "the hook returned by get() was not invoked as expected")
|
||||
})
|
||||
})
|
||||
|
||||
@ -88,10 +103,10 @@ func TestHooks(t *testing.T) {
|
||||
hookInvoked := false
|
||||
hookHandler := func(*contextmodel.ReqContext) response.Response { hookInvoked = true; return nil }
|
||||
hooks := NewHooks(log.NewNopLogger())
|
||||
hooks.Set("/some/path", hookHandler)
|
||||
hooks.Set("GET", "/some/path", hookHandler)
|
||||
|
||||
composed := hooks.Wrap(defaultHandler)
|
||||
req := createReqWithURL("http://domain.test/some/path")
|
||||
req := createReqForTests("GET", "http://domain.test/some/path")
|
||||
composed(req)
|
||||
|
||||
require.True(t, hookInvoked, "hook was expected to be invoked, but it was not")
|
||||
@ -104,10 +119,10 @@ func TestHooks(t *testing.T) {
|
||||
hookInvoked := false
|
||||
hookHandler := func(*contextmodel.ReqContext) response.Response { hookInvoked = true; return nil }
|
||||
hooks := NewHooks(log.NewNopLogger())
|
||||
hooks.Set("/some/path", hookHandler)
|
||||
hooks.Set("GET", "/some/path", hookHandler)
|
||||
|
||||
composed := hooks.Wrap(defaultHandler)
|
||||
req := createReqWithURL("http://domain.test/does/not/match")
|
||||
req := createReqForTests("GET", "http://domain.test/does/not/match")
|
||||
composed(req)
|
||||
|
||||
require.False(t, hookInvoked, "hook was invoked, but it should not have been")
|
||||
@ -116,12 +131,13 @@ func TestHooks(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func createReqWithURL(setupURL string) *contextmodel.ReqContext {
|
||||
func createReqForTests(method, setupURL string) *contextmodel.ReqContext {
|
||||
reqURL, _ := url.Parse(setupURL)
|
||||
return &contextmodel.ReqContext{
|
||||
Context: &web.Context{
|
||||
Req: &http.Request{
|
||||
URL: reqURL,
|
||||
Method: method,
|
||||
URL: reqURL,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user