-
Notifications
You must be signed in to change notification settings - Fork 351
/
filtertest.go
91 lines (76 loc) · 3 KB
/
filtertest.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
/*
Package filtertest implements mock versions of the Filter, Spec and
FilterContext interfaces used during tests.
*/
package filtertest
import (
"net/http"
"github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/filters"
log "github.com/sirupsen/logrus"
)
// Noop filter, used to verify the filter name and the args in the route.
// Implements both the Filter and the Spec interfaces.
type Filter struct {
FilterName string
Args []interface{}
}
// Simple FilterContext implementation.
type Context struct {
FResponseWriter http.ResponseWriter
FOriginalRequest *http.Request
FRequest *http.Request
FResponse *http.Response
FServed bool
FServedWithResponse bool
FParams map[string]string
FStateBag map[string]interface{}
FBackendUrl string
FOutgoingHost string
FMetrics filters.Metrics
FTracer opentracing.Tracer
}
func (spec *Filter) Name() string { return spec.FilterName }
func (f *Filter) Request(ctx filters.FilterContext) {}
func (f *Filter) Response(ctx filters.FilterContext) {}
func (fc *Context) ResponseWriter() http.ResponseWriter { return fc.FResponseWriter }
func (fc *Context) Request() *http.Request { return fc.FRequest }
func (fc *Context) Response() *http.Response { return fc.FResponse }
func (fc *Context) MarkServed() { fc.FServed = true }
func (fc *Context) Served() bool { return fc.FServed }
func (fc *Context) PathParam(key string) string { return fc.FParams[key] }
func (fc *Context) StateBag() map[string]interface{} { return fc.FStateBag }
func (fc *Context) OriginalRequest() *http.Request { return fc.FOriginalRequest }
func (fc *Context) OriginalResponse() *http.Response { return nil }
func (fc *Context) BackendUrl() string { return fc.FBackendUrl }
func (fc *Context) OutgoingHost() string { return fc.FOutgoingHost }
func (fc *Context) SetOutgoingHost(h string) { fc.FOutgoingHost = h }
func (fc *Context) Metrics() filters.Metrics { return fc.FMetrics }
func (fc *Context) ResponseController() *http.ResponseController {
return http.NewResponseController(fc.FResponseWriter)
}
func (fc *Context) Tracer() opentracing.Tracer {
if fc.FTracer != nil {
return fc.FTracer
}
return &opentracing.NoopTracer{}
}
func (fc *Context) ParentSpan() opentracing.Span {
return opentracing.StartSpan("test_span")
}
func (fc *Context) Logger() filters.FilterContextLogger {
return log.StandardLogger()
}
func (fc *Context) Serve(resp *http.Response) {
fc.FServedWithResponse = true
fc.FResponse = resp
fc.FServed = true
}
//lint:ignore ST1016 ignore receiver name, because of type reuse
func (spec *Filter) CreateFilter(config []interface{}) (filters.Filter, error) {
return &Filter{spec.FilterName, config}, nil
}
func (fc *Context) Loopback() {}
func (fc *Context) Split() (filters.FilterContext, error) {
return fc, nil
}