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

add tests for test package #75

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 1 addition & 5 deletions bridge/bridgetest/bridgetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func MockFunc(e mockEnvironment) net.Conn {
for {
d, err := readPbFrame(conB)
if err != nil {
e.Errorf("Can't read method name")
// Conn probably closed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was getting an error with go test -race trying to access e from this goroutine. Open to ideas how to fix this better, but it got my tests working

break
}
method := string(d)
Expand All @@ -137,10 +137,6 @@ func MockFunc(e mockEnvironment) net.Conn {
e.Errorf("Can't write back return values")
break
}

if !e.IsRunning() {
break
}
}
}()
return conA
Expand Down
12 changes: 8 additions & 4 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ func (req *Request) Validate() error {

req.Headers = mergeHeaders(make(http.Header), req.Headers)

if req.Method == "GET" {
switch req.Method {
case "GET":
if req.Body != "" {
return fmt.Errorf("GET requests must not have body, found \"%v\"", req.Body)
}
return nil
case "POST":
return nil
default:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason to only whitelist certain methods?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like unfinished code.

return fmt.Errorf("unsupported method \"%v\"", req.Method)
}
return fmt.Errorf("Unsupported method \"%v\"", req.Method)
}

func getPort(u *url.URL) int32 {
Expand Down Expand Up @@ -442,9 +446,9 @@ func (e *TestEnv) Handle(method string, args_d []byte) []byte {
args := kong_plugin_protocol.ExitArgs{}
e.noErr(proto.Unmarshal(args_d, &args))
e.ClientRes.Status = int(args.Status)
e.state = finished
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ExitStatus was not finishing

Copy link

Choose a reason for hiding this comment

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

ack for this addition

e.ClientRes.Body = args.Body
if args.Headers != nil {
e.state = finished
e.ClientRes.Body = args.Body
headers := bridge.UnwrapHeaders(args.Headers)
mergeHeaders(e.ClientRes.Headers, headers)
}
Expand Down
77 changes: 77 additions & 0 deletions test/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package test

import (
"net/http"
"testing"

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

type Config struct{}

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

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

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

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, "get", env.ClientRes.Body)
}

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

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

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

env.DoHttps(&Config{})
assert.Equal(t, http.StatusNotImplemented, env.ClientRes.Status)
assert.Equal(t, "", env.ClientRes.Body)
}