Skip to content

Commit

Permalink
tests: add ctx test handlers, fix hanging channel, fix typo (#174)
Browse files Browse the repository at this point in the history
* Allow tests to reference the context `kong.context`
* Fix issue where `kong.Response.Exit()` would hand in test cases
  • Loading branch information
winslowdibona committed Feb 21, 2024
1 parent b689270 commit ca4b404
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bridge/bridgetest/bridgetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type mockEnvironment interface {
func MockFunc(e mockEnvironment) net.Conn {
conA, conB := net.Pipe()

statusCh := make(chan string)
statusCh := make(chan string, 1)
e.SubscribeStatusChange(statusCh)

go func() {
Expand Down Expand Up @@ -144,7 +144,7 @@ func MockFunc(e mockEnvironment) net.Conn {

select {
case msg := <-statusCh:
if msg == "finnished" {
if msg == "finished" {
return
}
default: // do nothing
Expand Down
17 changes: 17 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func (res *Response) merge(other Response) {
}
}

type Ctx struct {
Store map[string]interface{}
}

type envState int

const (
Expand All @@ -192,6 +196,7 @@ type TestEnv struct {
ServiceReq Request
ServiceRes Response
ClientRes Response
Ctx Ctx
}

// New creates a new test environment.
Expand All @@ -208,6 +213,7 @@ func New(t *testing.T, req Request) (env *TestEnv, err error) {
ServiceReq: req.clone(),
ServiceRes: Response{Headers: make(http.Header)},
ClientRes: Response{Headers: make(http.Header)},
Ctx: Ctx{Store: make(map[string]interface{})},
}

b := bridge.New(bridgetest.MockFunc(env)) // check
Expand Down Expand Up @@ -279,6 +285,17 @@ func (e *TestEnv) Handle(method string, args_d []byte) []byte {
case "kong.client.get_protocol":
out = bridge.WrapString("https")

case "kong.ctx.shared.set":
args := kong_plugin_protocol.KV{}
e.noErr(proto.Unmarshal(args_d, &args))
e.Ctx.Store[args.K] = args.V

case "kong.ctx.shared.get":
args := kong_plugin_protocol.String{}
e.noErr(proto.Unmarshal(args_d, &args))
v := fmt.Sprintf("%v", e.Ctx.Store[args.V])
out = bridge.WrapString(v)

case "kong.ip.is_trusted":
out = &kong_plugin_protocol.Bool{V: true}

Expand Down
39 changes: 39 additions & 0 deletions test/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test

import (
"errors"
"net/http"
"testing"

"github.com/Kong/go-pdk"
"github.com/stretchr/testify/assert"
)

type foo struct {
shouldExit bool
}

func MockNew() *foo {
return &foo{
// manually change me to see both ways
shouldExit: true,
}
}

func (f *foo) Response(kong *pdk.PDK) {
if f.shouldExit {
kong.Response.Exit(http.StatusInternalServerError, []byte(errors.New("exit").Error()), nil)
}
}

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

env, err := New(t, Request{
Method: "POST",
Url: "",
Body: []byte("{}"),
})
assert.NoError(t, err)
env.DoHttps(MockNew())
}

0 comments on commit ca4b404

Please sign in to comment.