-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to only whitelist certain methods? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ExitStatus was not finishing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
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) | ||
} |
There was a problem hiding this comment.
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