-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: webhook channel query param (#40)
* Events can now be received at both `/events` or `/events/<channel-name>` endpoints * Resolve conversation * move gorilla into webhook package Signed-off-by: Nico Braun <rainbowstack@gmail.com> * fix golangci lint issues Signed-off-by: Nico Braun <rainbowstack@gmail.com> --------- Signed-off-by: Nico Braun <rainbowstack@gmail.com> Co-authored-by: Nico Braun <rainbowstack@gmail.com>
- Loading branch information
1 parent
76e036a
commit 806fde4
Showing
4 changed files
with
113 additions
and
10 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
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
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
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,74 @@ | ||
package webhook | ||
|
||
import ( | ||
"context" | ||
"net/http/httptest" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWebhook_ServeHTTP(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
giveURL string | ||
giveBody string | ||
wantStatus int | ||
wantResBody string | ||
wantChan string | ||
}{ | ||
{ | ||
name: "query parameter", | ||
giveURL: "/events?chan=query", | ||
giveBody: "hello", | ||
wantStatus: 200, | ||
wantResBody: warnQueryDeprecated, | ||
wantChan: "query", | ||
}, | ||
{ | ||
name: "path parameter", | ||
giveURL: "/events/path", | ||
giveBody: "hello", | ||
wantStatus: 202, | ||
wantChan: "path", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
ms = mockScheduler{} | ||
api = New(&ms) | ||
rec = httptest.NewRecorder() | ||
req = httptest.NewRequest("POST", tt.giveURL, strings.NewReader(tt.giveBody)) | ||
) | ||
api.ServeHTTP(rec, req) | ||
res := rec.Result() | ||
defer res.Body.Close() | ||
assertEq(t, res.StatusCode, tt.wantStatus) | ||
assertEq(t, rec.Body.String(), tt.wantResBody) | ||
assertEq(t, ms.ch, tt.wantChan) | ||
assertEq(t, string(ms.buf), tt.giveBody) | ||
}) | ||
} | ||
} | ||
|
||
type mockScheduler struct { | ||
ch string | ||
buf []byte | ||
} | ||
|
||
func (m *mockScheduler) Schedule(_ context.Context, chn string, data []byte) error { | ||
m.ch = chn | ||
m.buf = data | ||
return nil | ||
} | ||
|
||
func assertEq[T any](t *testing.T, got, want T) { | ||
t.Helper() | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
} |