forked from goadesign/goa-cellar
/
health_testing.go
86 lines (78 loc) · 2.29 KB
/
health_testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Code generated by goagen v1.3.1, DO NOT EDIT.
//
// API "cellar": health TestHelpers
//
// Command:
// $ goagen
// --design=github.com/goadesign/goa-cellar/design
// --out=$(GOPATH)/src/github.com/goadesign/goa-cellar
// --version=v1.3.1
package test
import (
"bytes"
"context"
"fmt"
"github.com/goadesign/goa"
"github.com/goadesign/goa-cellar/app"
"github.com/goadesign/goa/goatest"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
)
// HealthHealthOK runs the method Health of the given controller with the given parameters.
// It returns the response writer so it's possible to inspect the response headers.
// If ctx is nil then context.Background() is used.
// If service is nil then a default service is created.
func HealthHealthOK(t goatest.TInterface, ctx context.Context, service *goa.Service, ctrl app.HealthController) http.ResponseWriter {
// Setup service
var (
logBuf bytes.Buffer
resp interface{}
respSetter goatest.ResponseSetterFunc = func(r interface{}) { resp = r }
)
if service == nil {
service = goatest.Service(&logBuf, respSetter)
} else {
logger := log.New(&logBuf, "", log.Ltime)
service.WithLogger(goa.NewLogger(logger))
newEncoder := func(io.Writer) goa.Encoder { return respSetter }
service.Encoder = goa.NewHTTPEncoder() // Make sure the code ends up using this decoder
service.Encoder.Register(newEncoder, "*/*")
}
// Setup request context
rw := httptest.NewRecorder()
u := &url.URL{
Path: fmt.Sprintf("/cellar/_ah/health"),
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
panic("invalid test " + err.Error()) // bug
}
prms := url.Values{}
if ctx == nil {
ctx = context.Background()
}
goaCtx := goa.NewContext(goa.WithAction(ctx, "HealthTest"), rw, req, prms)
healthCtx, _err := app.NewHealthHealthContext(goaCtx, req, service)
if _err != nil {
e, ok := _err.(goa.ServiceError)
if !ok {
panic("invalid test data " + _err.Error()) // bug
}
t.Errorf("unexpected parameter validation error: %+v", e)
return nil
}
// Perform action
_err = ctrl.Health(healthCtx)
// Validate response
if _err != nil {
t.Fatalf("controller returned %+v, logs:\n%s", _err, logBuf.String())
}
if rw.Code != 200 {
t.Errorf("invalid response status code: got %+v, expected 200", rw.Code)
}
// Return results
return rw
}