-
Notifications
You must be signed in to change notification settings - Fork 105
/
unittest.go
127 lines (109 loc) · 3.33 KB
/
unittest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright 2018 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package unittest
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/service"
)
// ResponseWriter mocks http.ResponseWriter and records response for testing
type ResponseWriter interface {
http.ResponseWriter
Code() int
Bytes() []byte
}
// NewResponseWriter creates a new ResponseWriter
func NewResponseWriter() ResponseWriter {
return newRW()
}
type responseWriter struct {
code int
header http.Header
buf *bytes.Buffer
}
func newRW() *responseWriter {
return &responseWriter{0, http.Header{}, bytes.NewBuffer(nil)}
}
func (r *responseWriter) Write(d []byte) (int, error) {
return r.buf.Write(d)
}
func (r *responseWriter) WriteHeader(code int) {
r.code = code
}
func (r *responseWriter) Header() http.Header {
return r.header
}
func (r *responseWriter) Code() int {
return r.code
}
func (r *responseWriter) Bytes() []byte {
return r.buf.Bytes()
}
// NewTestService creates a service.Service for testing.
func NewTestService(desc ...definition.Descriptor) (service.Service, error) {
builder := service.NewBuilder()
builder.SetModifier(service.FirstContextParameter())
builder.AddFilter(service.RedirectTrailingSlash(), service.FillLeadingSlash(), service.ParseRequestForm())
if err := builder.AddDescriptor(desc...); err != nil {
return nil, err
}
return builder.Build()
}
// NewTestServiceWithConfig creates a service.Service for testing with user specified modifier and
// filters. If modifier or filters is nil, default option will be used.
func NewTestServiceWithConfig(desc []definition.Descriptor, modifier service.DefinitionModifier,
filters []service.Filter) (service.Service, error) {
builder := service.NewBuilder()
if modifier == nil {
modifier = service.FirstContextParameter()
}
builder.SetModifier(modifier)
if filters == nil {
filters = []service.Filter{service.RedirectTrailingSlash(), service.FillLeadingSlash(), service.ParseRequestForm()}
}
builder.AddFilter(filters...)
if err := builder.AddDescriptor(desc...); err != nil {
return nil, err
}
return builder.Build()
}
// NewJSONRequest creates a http.Request with json Content-Type. The data parameter can be io.Reader, []byte or a struct.
func NewJSONRequest(ctx context.Context, method, url string, data interface{}) (*http.Request, error) {
var r io.Reader
if data != nil {
switch t := data.(type) {
case io.Reader:
r = t
case []byte:
r = bytes.NewBuffer(t)
default:
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("error encode data: %v", err)
}
r = bytes.NewBuffer(jsonBytes)
}
}
req, err := http.NewRequest(method, url, r)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Add("Content-Type", "application/json")
return req, nil
}