-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge_test.go
54 lines (48 loc) · 1.29 KB
/
challenge_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package larkhertz
import (
"context"
"encoding/json"
"testing"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/go-lark/lark"
"github.com/stretchr/testify/assert"
)
func TestChallengePassed(t *testing.T) {
var (
r = server.Default()
middleware = NewLarkMiddleware()
)
r.Use(middleware.LarkChallengeHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
// do nothing
})
message := lark.EventChallengeReq{
Challenge: "test",
Type: "url_verification",
}
resp := performRequest(r, "POST", "/", message)
var respData lark.EventChallengeReq
if assert.NotNil(t, resp.Body) {
json.NewDecoder(resp.Body).Decode(&respData)
assert.Equal(t, "test", respData.Challenge)
}
}
func TestChallengeMismatch(t *testing.T) {
r := server.Default()
middleware := NewLarkMiddleware().BindURLPrefix("/abc")
r.Use(middleware.LarkChallengeHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
// do nothing
})
message := lark.EventChallengeReq{
Challenge: "test",
Type: "url_verification",
}
resp := performRequest(r, "POST", "/", message)
var respData lark.EventChallengeReq
if assert.NotNil(t, resp.Body) {
err := json.NewDecoder(resp.Body).Decode(&respData)
assert.Error(t, err)
}
}