goa

package module
v0.0.0-...-34b20d6 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 6 Imported by: 0

README

httpcheck Goa plugin

This plugin provides a mounter for the Goa HTTP endpoints, enabling seamless integration with httpcheck. The mounter satisfies the http.Handler interface, allowing it to be directly set and utilized within httpcheck.

Usage Example

The following example demonstrates how to use the goa plugin to test a Goa HTTP endpoint. In this example, we're testing an endpoint that multiplies two numbers.

package calc

import (
	"io"
	"net/http"
	"strconv"
	"strings"
	"testing"

	"github.com/ikawaha/httpcheck"
	"github.com/ikawaha/httpcheck/plugin/goa"
	gen "github.com/ikawaha/httpcheck/plugin/goa/calc/gen/calc"
	"github.com/ikawaha/httpcheck/plugin/goa/calc/gen/http/calc/server"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestMounter(t *testing.T) {
	m := goa.NewMounter()
	m.Mount(goa.EndpointModular{
		Builder:  server.NewMultiplyHandler,
		Mounter:  server.MountMultiplyHandler,
		Endpoint: gen.NewMultiplyEndpoint(NewCalc()),
	})
	httpcheck.New(m).Test(t, "GET", "/multiply/3/5").
		Check().
		MustHasStatus(http.StatusOK).
		Cb(func(r *http.Response) {
			b, err := io.ReadAll(r.Body)
			require.NoError(t, err)
			i, err := strconv.Atoi(strings.TrimSpace(string(b)))
			assert.NoError(t, err, string(b))
			assert.Equal(t, 3*5, i)
		})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EndpointModular

type EndpointModular struct {
	Builder            HandlerBuilder
	Mounter            HandlerMounter
	Endpoint           goa.Endpoint
	EndpointMiddleware []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 Mounter

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

Mounter represents Goa v3 handler mounter.

func NewMounter

func NewMounter(opts ...Option) *Mounter

NewMounter constructs a mounter of the goa endpoints.

func (*Mounter) Handler

func (m *Mounter) Handler() http.Handler

func (*Mounter) Mount

func (m *Mounter) Mount(e EndpointModular)

func (*Mounter) MountEndpoint

func (m *Mounter) MountEndpoint(builder HandlerBuilder, mounter HandlerMounter, endpoint goa.Endpoint, middlewares ...middleware)

MountEndpoint mounts an endpoint handler and it's middleware.

func (*Mounter) ServeHTTP

func (m *Mounter) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Mounter) Use

func (m *Mounter) Use(middleware func(http.Handler) http.Handler)

Use sets the middleware.

type Option

type Option func(m *Mounter)

Option represents options for API checker.

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.

Jump to

Keyboard shortcuts

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