easy

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockHandler

type MockHandler struct {
	W *httptest.ResponseRecorder
	R *http.Request

	Cookies []string
}

func NewFormData

func NewFormData(path string, method string, data *Multipart) (*MockHandler, error)

multipart/form-data type. Use the POST or PUT method.

func NewJson

func NewJson(path string, method string, data any) (*MockHandler, error)

Post json. Use the POST or PUT method.

func NewMock

func NewMock(path string, method string, body string) (*MockHandler, error)

Create mock objects. if set path is empty, replace to /.

Example:

m, err := NewMock("", http.MethodGet, "/")

func NewMockReader

func NewMockReader(path string, method string, body io.Reader) (*MockHandler, error)

Create mock objects use io.Reader body. if set path is empty, replace to /.

Example:

m, err := NewMockReader(strings.NewReader(""), http.MethodGet, "/")

func NewURLEncoded

func NewURLEncoded(path string, method string, data url.Values) (*MockHandler, error)

application/x-www-form-urlencoded type. Use the POST or PUT method.

func (*MockHandler) Cookie

func (c *MockHandler) Cookie(cookies []*http.Cookie)

Including cookies in the request

func (*MockHandler) Echo

func (c *MockHandler) Echo() echo.Context

Returns echo context this method is require labstack/echo package.

Usage:

c := m.Echo()
c.SetPath("/users/:email")
c.SetParamNames("email")
c.SetParamValues("jon@labstack.com")

func (*MockHandler) EqBody

func (c *MockHandler) EqBody(t *testing.T, body string)

Compare response body

func (*MockHandler) EqJson

func (c *MockHandler) EqJson(t *testing.T, obj any)

Compare response body written json

func (*MockHandler) FindCookie

func (c *MockHandler) FindCookie(name string) *http.Cookie

Returns set-cookie

func (*MockHandler) Handler

func (c *MockHandler) Handler(hand func(w http.ResponseWriter, r *http.Request))

Add handler

func (*MockHandler) Json

func (c *MockHandler) Json(v any) error

Prase json body

func (*MockHandler) Ok

func (c *MockHandler) Ok(t *testing.T)

Check if request success

func (*MockHandler) Response

func (c *MockHandler) Response() *http.Response

Returns response

func (*MockHandler) SetAddr

func (c *MockHandler) SetAddr(addr string)

Set RemoteAddr

default to 192.0.2.1:1234 is "TEST-NET" in RFC 5737

func (*MockHandler) SetCookies

func (c *MockHandler) SetCookies() []*http.Cookie

Returns Set-Cookie headers

func (*MockHandler) Status

func (c *MockHandler) Status(t *testing.T, status int)

Check response status code

type MockServer

type MockServer struct {
	Server *httptest.Server
	Header *http.Header

	Cookies []string
}

func NewMockServer

func NewMockServer(handler http.Handler) *MockServer

Start mock server

func NewMockTLSServer

func NewMockTLSServer(handler http.Handler) *MockServer

Start mock server with TLS mode

func (*MockServer) Close

func (c *MockServer) Close()

close server

func (*MockServer) Cookie

func (c *MockServer) Cookie(cookies []*http.Cookie)

func (*MockServer) Do

func (c *MockServer) Do(t *testing.T, path string, method string, body io.Reader) *Response

func (*MockServer) FormData

func (c *MockServer) FormData(t *testing.T, path string, method string, form *Multipart) *Response

multipart/form-data

func (*MockServer) Get

func (c *MockServer) Get(t *testing.T, path string) *Response

GET Request

func (*MockServer) GetOK

func (c *MockServer) GetOK(t *testing.T, path string) *Response

Get Request and check status 200

func (*MockServer) Post

func (c *MockServer) Post(t *testing.T, path string, contentType string, body io.Reader) *Response

POST Requests

func (*MockServer) PostForm

func (c *MockServer) PostForm(t *testing.T, path string, value url.Values) *Response

application/x-www-form-urlencoded

func (*MockServer) PostFormData

func (c *MockServer) PostFormData(t *testing.T, path string, form *Multipart) *Response

POST multipart/form-data

func (*MockServer) PostJson

func (c *MockServer) PostJson(t *testing.T, path string, obj any) *Response

application/json

func (*MockServer) PostString

func (c *MockServer) PostString(t *testing.T, path string, contentType string, body string) *Response

func (*MockServer) URL

func (c *MockServer) URL(path string) string

convert to mock server url

type Multipart

type Multipart struct {
	// contains filtered or unexported fields
}

func NewMultipart

func NewMultipart() *Multipart

Create a new multipart/form-data object

Example:

m := NewMultipart()
// Insert k-v data
err := m.Insert("key", "value")
require.NoError(t, err)
// Insert files
err := m.InsertFile("key", file)

func (*Multipart) Export

func (c *Multipart) Export() *bytes.Buffer

Outputs a multipart/form-data format.

func (*Multipart) Insert

func (c *Multipart) Insert(key string, value string) error

Add a string form

func (*Multipart) InsertFile

func (c *Multipart) InsertFile(key string, file *os.File) error

Add a file objects

Example:

file, err := os.Open("file path")
require.NoError(t, err)
m.InsertFile("file", file)

type Response

type Response struct {
	Resp *http.Response
}

func NewResponse

func NewResponse(resp *http.Response) *Response

func (*Response) Body

func (c *Response) Body() *bytes.Buffer

func (*Response) EqBody

func (c *Response) EqBody(t *testing.T, body string)

func (*Response) EqJson

func (c *Response) EqJson(t *testing.T, obj any)

func (*Response) Json

func (c *Response) Json(v any) error

Prase json body

func (*Response) Ok

func (c *Response) Ok(t *testing.T)

func (*Response) SetCookies

func (c *Response) SetCookies() []*http.Cookie

Returns set-cookie's

func (*Response) Status

func (c *Response) Status(t *testing.T, status int)

Jump to

Keyboard shortcuts

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