test

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2022 License: Apache-2.0 Imports: 24 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Request

type Request struct {
	Method  string
	Url     string
	Headers http.Header
	Body    string
}

The Request type represents the request received from the client or the request sent to the service.

func (Request) ToResponse

func (req Request) ToResponse() Response

ToResponse creates a new Response object from a Request, simulating an "echo" service.

func (*Request) Validate

func (req *Request) Validate() error

Validate verifies a request and normalizes the headers. (to make them case-insensitive)

type Response

type Response struct {
	Status  int
	Message string
	Headers http.Header
	Body    string
}

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
	// contains filtered or unexported fields
}

func New

func New(t *testing.T, req Request) (env *TestEnv, err error)

New creates a new test environment.

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).

func (TestEnv) Errorf

func (e TestEnv) Errorf(format string, args ...interface{})

Internal use. Calls the Errof function with the test context.

func (*TestEnv) Handle

func (e *TestEnv) Handle(method string, args_d []byte) []byte

Internal use. Handles a PDK request from the plugin under test.

func (TestEnv) IsRunning

func (e TestEnv) IsRunning() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL