Skip to content

Commit

Permalink
tests(test): add more tests to test util
Browse files Browse the repository at this point in the history
Co-Authored-by: Guilherme Salazar <gsz@acm.org>
  • Loading branch information
jgramoll and gszr committed Mar 13, 2024
1 parent af1a7d9 commit 0e58de6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
12 changes: 6 additions & 6 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ type TestEnv struct {
}

// New creates a new test environment.
func New(t *testing.T, req Request) (env *TestEnv, err error) {
err = req.Validate()
func New(t *testing.T, req Request) (*TestEnv, error) {
err := req.Validate()
if err != nil {
return
return nil, err
}

env = &TestEnv{
env := TestEnv{
t: t,
state: running,
ClientReq: req,
Expand All @@ -216,7 +216,7 @@ func New(t *testing.T, req Request) (env *TestEnv, err error) {
Ctx: Ctx{Store: make(map[string]interface{})},
}

b := bridge.New(bridgetest.MockFunc(env)) // check
b := bridge.New(bridgetest.MockFunc(&env)) // check
env.pdk = &pdk.PDK{
Client: client.Client{PdkBridge: b},
Ctx: ctx.Ctx{PdkBridge: b},
Expand All @@ -231,7 +231,7 @@ func New(t *testing.T, req Request) (env *TestEnv, err error) {
ServiceRequest: service_request.Request{PdkBridge: b},
ServiceResponse: service_response.Response{PdkBridge: b},
}
return
return &env, nil
}

func (e *TestEnv) noErr(err error) {
Expand Down
89 changes: 83 additions & 6 deletions test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,48 @@ import (
"google.golang.org/protobuf/types/known/structpb"
)

type foo struct {
type Config struct {
shouldExit bool
}

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

func (f *foo) Response(kong *pdk.PDK) {
if f.shouldExit {
// Plugin used for tests
func (conf *Config) Access(kong *pdk.PDK) {
path, err := kong.Request.GetPath()
if err != nil {
kong.Response.Exit(http.StatusInternalServerError, []byte(err.Error()), nil)
return
}

switch path {
case "/method":
method, err := kong.Request.GetMethod()
if err != nil {
kong.Response.Exit(http.StatusInternalServerError, []byte(err.Error()), nil)
return
}

switch method {
case "GET":
kong.Response.Exit(http.StatusOK, []byte("get"), nil)
case "POST":
kong.Response.Exit(http.StatusOK, []byte("post"), nil)
default:
kong.Response.ExitStatus(http.StatusNotImplemented)
}
default:
kong.Response.ExitStatus(http.StatusNotImplemented)
}
}

func (c *Config) Response(kong *pdk.PDK) {
if c.shouldExit {
kong.Response.Exit(http.StatusInternalServerError, []byte(errors.New("exit").Error()), nil)
}
}
Expand All @@ -40,7 +69,9 @@ func TestNoHangingChannel(t *testing.T) {
Body: []byte("{}"),
})
assert.NoError(t, err)
env.DoHttps(MockNew())
env.DoHttps(&Config{ shouldExit: true })

env.Finish()
}

func TestSharedContext(t *testing.T) {
Expand Down Expand Up @@ -89,4 +120,50 @@ func TestSharedContext(t *testing.T) {
for _, v := range testValues {
perform(v)
}

env.Finish()
}

func TestAllowGET(t *testing.T) {
env, err := New(t, Request{
Method: "GET",
Url: "http://localhost/method",
})
assert.NoError(t, err)

env.DoHttps(&Config{})
assert.Equal(t, 200, env.ClientRes.Status)
assert.Equal(t, []byte("get"), env.ClientRes.Body)

env.Finish()
}

func TestAllowPOST(t *testing.T) {
env, err := New(t, Request{
Method: "POST",
Url: "http://localhost/method",
Body: []byte("post body"),
})
assert.NoError(t, err)

env.DoHttps(&Config{})
assert.Equal(t, 200, env.ClientRes.Status)
assert.Equal(t, []byte("post"), env.ClientRes.Body)

env.Finish()
}

func TestExitStatus(t *testing.T) {
env, err := New(t, Request{
Method: "POST",
Url: "http://localhost/notimplimented",
Body: []byte("Should not copy"),
})
assert.NoError(t, err)

env.DoHttps(&Config{})
assert.Equal(t, http.StatusNotImplemented, env.ClientRes.Status)
assert.Equal(t, []byte(nil), env.ClientRes.Body)

env.Finish()
}

0 comments on commit 0e58de6

Please sign in to comment.