h3test

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: ISC Imports: 8 Imported by: 0

README ¶

🧪 H3Test

A helper func for testing http requests.

📦 Usage

go get -u github.com/tsingshaner/go-pkg/h3test
package e2e

// import ...

type Data struct {
    Code string `json:"code"`
}

var handler http.Handler

func TestMain(m *testing.M) {
    server = &http.Server{
      // ...
    }

    handler = server.Handler

    os.Exit(m.Run())
}

func TestRegister(t *testing.T) {
    request := h3test.New("/api/auth/register").POST()
    payload := map[string]string{
        "username": "test",
        "password": "123456",
    }

    response := request.JSON(payload).Send(handler)

    assert.Equal(t, http.StatusCreated, res.Code)

    data := h3test.ExtractJSON[Data](res)

    assert.Equal(t, "Custom Success Code", data.Code)
}

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func ExtractJSON ¶

func ExtractJSON[T any](resp *httptest.ResponseRecorder) *T

ExtractJSON extracts the JSON response body into a struct pointer.

func ToJSON ¶

func ToJSON[T any](v T) ([]byte, error)

func ToPlain ¶

func ToPlain[T Stringer](v T) []byte

func ToXML ¶

func ToXML[T any](v T) ([]byte, error)

Types ¶

type Engine ¶

type Engine interface {
	ServeHTTP(w http.ResponseWriter, req *http.Request)
}

type Request ¶

type Request struct {
	Method string      // GET, POST, PUT, PATCH, DELETE
	Path   string      // /api/v1/users
	Body   any         // body
	Query  url.Values  // query
	Header http.Header // header
}

func New ¶

func New(path string) *Request

func (*Request) AddHeader ¶

func (r *Request) AddHeader(key, value string) *Request

func (*Request) AddQuery ¶

func (r *Request) AddQuery(key, value string) *Request

func (*Request) Clone ¶

func (r *Request) Clone() *Request

func (*Request) DELETE ¶

func (r *Request) DELETE() *Request

func (*Request) GET ¶

func (r *Request) GET() *Request

func (*Request) JSON ¶

func (r *Request) JSON(body any) *Request

func (*Request) PATCH ¶

func (r *Request) PATCH() *Request

func (*Request) POST ¶

func (r *Request) POST() *Request

func (*Request) PUT ¶

func (r *Request) PUT() *Request

func (*Request) Send ¶

func (r *Request) Send(engine http.Handler) *httptest.ResponseRecorder

func (*Request) SetBody ¶

func (r *Request) SetBody(body any) *Request

type Stringer ¶

type Stringer interface {
	~string | ~[]byte
}

Jump to

Keyboard shortcuts

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