goahttpcheck

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 8 Imported by: 0

README

Build Status

goahttpcheck

A test helper for testing endpoints of APIs generated by Goa v3. This makes it possible to test endpoints using ivpusic/httpcheck.

Usage

  1. New checker.
  2. Set the method handler constructor, mounter, and method endpoint in the checker.
  3. Register the middleware with the checker's Use method (If any).
  4. Call checker's Test method and test by ivpusic/httpcheck way.

Example

see. https://github.com/ikawaha/goahttpcheck/blob/master/testdata/calc_test.go

Design

var _ = Service("calc", func() {
	Description("The calc service performs operations on numbers.")
	Method("add", func() {
		Payload(func() {
			Field(1, "a", Int, "Left operand")
			Field(2, "b", Int, "Right operand")
			Required("a", "b")
		})
		Result(Int)
		HTTP(func() {
			GET("/add/{a}/{b}")
			Response(StatusOK)
		})
	})

Tests

import (
...
	"calc/gen/calc"
	"calc/gen/http/calc/server"
)

func TestCalcsrvc_Add(t *testing.T) {
	checker := goahttpcheck.New()
	var logger log.Logger
	checker.Mount(server.NewAddHandler, server.MountAddHandler, calc.NewAddEndpoint(NewCalc(&logger)))

	// see. https://github.com/ikawaha/httpcheck
	checker.Test(t, http.MethodGet, "/add/1/2").
		Check().
		HasStatus(http.StatusOK).
		Cb(func(r *http.Response) {
			b, err := ioutil.ReadAll(r.Body)
			if err != nil {
				t.Fatalf("unexpected error, %v", err)
			}
			r.Body.Close()
			if got, expected := strings.TrimSpace(string(b)), "3"; got != expected {
				t.Errorf("got %+v, expected %v", b, expected)
			}
		})
}

Blog: http://ikawaha.hateblo.jp/entry/2019/12/03/154521


License MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIChecker

type APIChecker struct {
	Mux           goahttp.Muxer
	Middleware    []middleware
	Decoder       decoder
	Encoder       encoder
	ErrorHandler  errorHandler
	Formatter     formatter
	ClientOptions []httpcheck.Option
}

APIChecker represents the API checker.

func New

func New(options ...Option) *APIChecker

New constructs API checker.

func (*APIChecker) Mount

func (c *APIChecker) Mount(builder HandlerBuilder, mounter HandlerMounter, endpoint goa.Endpoint, middlewares ...middleware)

Mount mounts the endpoint handler.

func (APIChecker) Test

func (c APIChecker) Test(t *testing.T, method, path string) *httpcheck.Tester

Test returns a http checker that tests the endpoint. see. https://github.com/ikawaha/httpcheck/

func (*APIChecker) Use

func (c *APIChecker) Use(middleware func(http.Handler) http.Handler)

Use sets the middleware.

type HandlerBuilder

type HandlerBuilder func(goa.Endpoint, goahttp.Muxer, decoder, encoder, errorHandler, formatter) http.Handler

HandlerBuilder represents the goa http handler builder.

type HandlerMounter

type HandlerMounter func(goahttp.Muxer, http.Handler)

HandlerMounter represents the goa http handler mounter.

type Option

type Option func(c *APIChecker)

Option represents options for API checker.

func CheckRedirect added in v0.2.3

func CheckRedirect(policy func(req *http.Request, via []*http.Request) error) Option

CheckRedirect sets the policy of redirection to the HTTP client.

func ClientTimeout added in v0.2.3

func ClientTimeout(d time.Duration) Option

ClientTimeout sets the client timeout.

func Decoder

func Decoder(dec decoder) Option

Decoder sets the decoder.

func Encoder

func Encoder(enc encoder) Option

Encoder sets the encoder.

func ErrorHandler

func ErrorHandler(eh errorHandler) Option

ErrorHandler sets the error handler.

func Formatter

func Formatter(fm formatter) Option

Formatter sets the error handler.

func Muxer

func Muxer(mux goahttp.Muxer) Option

Muxer sets the goa http multiplexer.

func NoRedirect added in v0.2.3

func NoRedirect() Option

NoRedirect is the alias of the following:

CheckRedirect(func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
})

Client returns ErrUseLastResponse, the next request is not sent and the most recent response is returned with its body unclosed.

Jump to

Keyboard shortcuts

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