/
filter_testing.go
98 lines (90 loc) · 2.7 KB
/
filter_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
87
88
89
90
91
92
93
94
95
96
97
98
// Code generated by goagen v1.4.0, DO NOT EDIT.
//
// API "feedpushr": filter TestHelpers
//
// Command:
// $ goagen
// --design=github.com/ncarlier/feedpushr/design
// --out=$(GOPATH)/src/github.com/ncarlier/feedpushr/autogen
// --version=v1.4.0
package test
import (
"bytes"
"context"
"fmt"
"github.com/goadesign/goa"
"github.com/goadesign/goa/goatest"
"github.com/ncarlier/feedpushr/autogen/app"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
)
// ListFilterOK runs the method List of the given controller with the given parameters.
// It returns the response writer so it's possible to inspect the response headers and the media type struct written to the response.
// If ctx is nil then context.Background() is used.
// If service is nil then a default service is created.
func ListFilterOK(t goatest.TInterface, ctx context.Context, service *goa.Service, ctrl app.FilterController) (http.ResponseWriter, app.FilterCollection) {
// 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("/v1/filters"),
}
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, "FilterTest"), rw, req, prms)
listCtx, _err := app.NewListFilterContext(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, nil
}
// Perform action
_err = ctrl.List(listCtx)
// 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)
}
var mt app.FilterCollection
if resp != nil {
var _ok bool
mt, _ok = resp.(app.FilterCollection)
if !_ok {
t.Fatalf("invalid response media: got variable of type %T, value %+v, expected instance of app.FilterCollection", resp, resp)
}
_err = mt.Validate()
if _err != nil {
t.Errorf("invalid response media type: %s", _err)
}
}
// Return results
return rw, mt
}