Skip to content

ikawaha/goahttpcheck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Go Reference

goahttpcheck

A test helper for testing endpoints of APIs generated by Goa v3. This makes it possible to test endpoints using ikawaha/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 ikawaha/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: https://zenn.dev/ikawaha/articles/hatena-20191203-154521


License MIT