Code Monkey home page Code Monkey logo

goahttpcheck's Introduction

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

goahttpcheck's People

Contributors

ikawaha avatar dependabot[bot] avatar dependabot-preview[bot] avatar mcauto avatar khaledez avatar

Stargazers

Giulio avatar tharuta avatar Isagani Mendoza avatar Roman avatar  avatar Jesús García Crespo avatar  avatar Naoki Kanada avatar Raphael Simon avatar  avatar yoshiki masubuchi avatar Luca Osti avatar Takeshi Akima avatar

Watchers

James Cloos avatar

goahttpcheck's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.