Documentation ¶
Overview ¶
Utilities to test plugins.
Trivial example:
package main import ( "testing" "github.com/Kong/go-pdk/test" "github.com/stretchr/testify/assert" ) func TestPlugin(t *testing.T) { env, err := test.New(t, test.Request{ Method: "GET", Url: "http://example.com?q=search&x=9", Headers: map[string][]string{ "X-Hi": {"hello"}, }, }) assert.NoError(t, err) env.DoHttps(&Config{}) assert.Equal(t, 200, env.ClientRes.Status) assert.Equal(t, "Go says Hi!", env.ClientRes.Headers.Get("x-hello-from-go")) }
in short:
1. Create a test environment passing a test.Request{} object to the test.New() function.
2. Create a Config{} object (or the appropriate config structure of the plugin)
3. env.DoHttps(t, &config) will pass the request object through the plugin, exercising each event handler and return (if there's no hard error) a simulated response object. There are other env.DoXXX(t, &config) functions for HTTP, TCP, TLS and individual phases.
3.5 The http and https functions assume the service response will be an "echo" of the request (same body and headers) if you need a different service response, use the individual phase methods and set the env.ServiceRes object manually.
4. Do assertions to verify the service request and client response are as expected.
Index ¶
- type Ctx
- type Request
- type Response
- type TestEnv
- func (e *TestEnv) DoAccess(config interface{})
- func (e *TestEnv) DoCertificate(config interface{})
- func (e *TestEnv) DoHttp(config interface{})
- func (e *TestEnv) DoHttps(config interface{})
- func (e *TestEnv) DoLog(config interface{})
- func (e *TestEnv) DoPreread(config interface{})
- func (e *TestEnv) DoResponse(config interface{})
- func (e *TestEnv) DoRewrite(config interface{})
- func (e *TestEnv) DoStream(config interface{})
- func (e *TestEnv) DoTLS(config interface{})
- func (e *TestEnv) Errorf(format string, args ...interface{})
- func (e *TestEnv) Finish()
- func (e *TestEnv) Handle(method string, args_d []byte) []byte
- func (e *TestEnv) IsRunning() bool
- func (e *TestEnv) SubscribeStatusChange(ch chan<- string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Request ¶
The Request type represents the request received from the client or the request sent to the service.
func (Request) ToResponse ¶
ToResponse creates a new Response object from a Request, simulating an "echo" service.
type Response ¶
The Response type represents the response returned from the service or sent to the client.
type TestEnv ¶
type TestEnv struct { ClientReq Request ServiceReq Request ServiceRes Response ClientRes Response Ctx Ctx // contains filtered or unexported fields }
func (*TestEnv) DoAccess ¶
func (e *TestEnv) DoAccess(config interface{})
DoAccess tests the Access method of the plugin with the Request in the test environment and the plugin configuration passed in the argument.
func (*TestEnv) DoCertificate ¶
func (e *TestEnv) DoCertificate(config interface{})
DoCertificate tests the Certificate method of the plugin with the Request in the test environment and the plugin configuration passed in the argument.
func (*TestEnv) DoHttp ¶
func (e *TestEnv) DoHttp(config interface{})
DoHttp simulates an HTTP request/response cycle passing through the Rewrite, Access, Response and Log methods of the plugin.
Between the Access and Response methods, a service response is created from the service request (possibly modified by the previous methods), simulating an "echo" service. If you need a different kind of service, use the individual methods (e.DoRewrite(), e.DoAccess(), e.DoResponse() and e.DoLog())
func (*TestEnv) DoHttps ¶
func (e *TestEnv) DoHttps(config interface{})
DoHttps simulates an HTTPS request/response cycle passing through the Certificate method and then all the same as the DoHttp function.
func (*TestEnv) DoLog ¶
func (e *TestEnv) DoLog(config interface{})
DoLog tests the Log method of the plugin with the plugin configuration passed in the argument.
func (*TestEnv) DoPreread ¶
func (e *TestEnv) DoPreread(config interface{})
DoPreread tests the Preread method of a streaming plugin.
func (*TestEnv) DoResponse ¶
func (e *TestEnv) DoResponse(config interface{})
DoResponse tests the Response method of the plugin with the Request in the test environment and the plugin configuration passed in the argument. Before calling the plugin, the simulated client response is updated with the service response, if any.
func (*TestEnv) DoRewrite ¶
func (e *TestEnv) DoRewrite(config interface{})
DoRewrite tests the Rewrite method of the plugin with the Request in the test environment and the plugin configuration passed in the argument.
func (*TestEnv) DoStream ¶
func (e *TestEnv) DoStream(config interface{})
DoStream simulates a TCP stream (for streaming plugins).
func (*TestEnv) DoTLS ¶
func (e *TestEnv) DoTLS(config interface{})
DoTLS simulates a TLS stream (for streaming plugins).