github.com/zak-blake/goa@v1.4.1/middleware/middleware_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  
     7  	"context"
     8  
     9  	"github.com/goadesign/goa"
    10  )
    11  
    12  // Helper that sets up a "working" service
    13  func newService(logger goa.LogAdapter) *goa.Service {
    14  	service := goa.New("test")
    15  	service.Encoder.Register(goa.NewJSONEncoder, "*/*")
    16  	service.Decoder.Register(goa.NewJSONDecoder, "*/*")
    17  	service.WithLogger(logger)
    18  	return service
    19  }
    20  
    21  // Creates a test context
    22  func newContext(service *goa.Service, rw http.ResponseWriter, req *http.Request, params url.Values) context.Context {
    23  	ctrl := service.NewController("test")
    24  	return goa.NewContext(ctrl.Context, rw, req, params)
    25  }
    26  
    27  type logEntry struct {
    28  	Msg  string
    29  	Data []interface{}
    30  }
    31  
    32  type testLogger struct {
    33  	Context      []interface{}
    34  	InfoEntries  []logEntry
    35  	ErrorEntries []logEntry
    36  }
    37  
    38  func (t *testLogger) Info(msg string, data ...interface{}) {
    39  	e := logEntry{msg, append(t.Context, data...)}
    40  	t.InfoEntries = append(t.InfoEntries, e)
    41  }
    42  
    43  func (t *testLogger) Error(msg string, data ...interface{}) {
    44  	e := logEntry{msg, append(t.Context, data...)}
    45  	t.ErrorEntries = append(t.ErrorEntries, e)
    46  }
    47  
    48  func (t *testLogger) New(data ...interface{}) goa.LogAdapter {
    49  	t.Context = append(t.Context, data...)
    50  	return t
    51  }
    52  
    53  type testResponseWriter struct {
    54  	ParentHeader http.Header
    55  	Body         []byte
    56  	Status       int
    57  }
    58  
    59  func newTestResponseWriter() *testResponseWriter {
    60  	h := make(http.Header)
    61  	return &testResponseWriter{ParentHeader: h}
    62  }
    63  
    64  func (t *testResponseWriter) Header() http.Header {
    65  	return t.ParentHeader
    66  }
    67  
    68  func (t *testResponseWriter) Write(b []byte) (int, error) {
    69  	t.Body = append(t.Body, b...)
    70  	return len(b), nil
    71  }
    72  
    73  func (t *testResponseWriter) WriteHeader(s int) {
    74  	t.Status = s
    75  }