Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events ability to halt #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package authboss

import (
"fmt"
"github.com/friendsofgo/errors"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use "errors" ? It has been including errors.Is since Go 1.13.

"net/http"
)

Expand All @@ -9,6 +11,8 @@ import (
// Event type is for describing events
type Event int

var ErrHalt = fmt.Errorf("event halt")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No param so probably just use errors.New ?


// Event kinds
const (
EventRegister Event = iota
Expand Down Expand Up @@ -96,6 +100,13 @@ func (c *Events) call(evs []EventHandler, w http.ResponseWriter, r *http.Request
for _, fn := range evs {
interrupt, err := fn(w, r, handled)
if err != nil {
if errors.Is(err, ErrHalt) {
if interrupt {
handled = true
}
break
}

return false, err
}
if interrupt {
Expand Down
88 changes: 88 additions & 0 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,94 @@ func TestEventsHandled(t *testing.T) {
}
}

func TestEventsHaltHandled(t *testing.T) {
t.Parallel()

ab := New()
firstCalled := false
secondCalled := false

firstHandled := false
secondHandled := false

ab.Events.Before(EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
firstCalled = true
firstHandled = handled
return true, ErrHalt
})
ab.Events.Before(EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
secondCalled = true
secondHandled = handled
return false, nil
})

handled, err := ab.Events.FireBefore(EventRegister, nil, nil)
if err != nil {
t.Error("Unexpected error:", err)
}
if !handled {
t.Error("it should have been handled")
}

if !firstCalled {
t.Error("expected first to have been called")
}
if firstHandled {
t.Error("first should not see the event as being handled")
}

if secondCalled {
t.Error("expected second to not have been called")
}
if secondHandled {
t.Error("second should not see the event as being handled (as second should not be running at all)")
}
}

func TestEventsHaltNotHandled(t *testing.T) {
t.Parallel()

ab := New()
firstCalled := false
secondCalled := false

firstHandled := false
secondHandled := false

ab.Events.Before(EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
firstCalled = true
firstHandled = handled
return false, ErrHalt
})
ab.Events.Before(EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
secondCalled = true
secondHandled = handled
return false, nil
})

handled, err := ab.Events.FireBefore(EventRegister, nil, nil)
if err != nil {
t.Error("Unexpected error:", err)
}
if handled {
t.Error("it should not have been handled")
}

if !firstCalled {
t.Error("expected first to have been called")
}
if firstHandled {
t.Error("first should not see the event as being handled")
}

if secondCalled {
t.Error("expected second to not have been called")
}
if secondHandled {
t.Error("second should not see the event as being handled (as second should not be running at all)")
}
}

func TestEventsErrors(t *testing.T) {
t.Parallel()

Expand Down