-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
testing.go
40 lines (34 loc) Β· 999 Bytes
/
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
package goatest
import (
"bytes"
"io"
"log"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
)
// TInterface is an interface for go's testing.T
type TInterface interface {
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
// ResponseSetterFunc func
type ResponseSetterFunc func(resp interface{})
// Encode implements a dummy encoder that returns the value being encoded
func (r ResponseSetterFunc) Encode(v interface{}) error {
r(v)
return nil
}
// Service provide a general goa.Service used for testing purposes
func Service(logBuf *bytes.Buffer, respSetter ResponseSetterFunc) *goa.Service {
s := goa.New("test")
logger := log.New(logBuf, "", log.Ltime)
s.WithLogger(goa.NewLogger(logger))
s.Use(middleware.LogRequest(true))
s.Use(middleware.LogResponse())
newEncoder := func(io.Writer) goa.Encoder {
return respSetter
}
s.Decoder.Register(goa.NewJSONDecoder, "*/*")
s.Encoder.Register(newEncoder, "*/*")
return s
}