forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtertest.go
81 lines (73 loc) · 3.02 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
// Copyright 2015 Zalando SE
//
// 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 filtertest implements mock versions of the Filter, Spec and
FilterContext interfaces used during tests.
*/
package filtertest
import (
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/filters"
)
// 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
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 nil }
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) Tracer() opentracing.Tracer {
if fc.FTracer != nil {
return fc.FTracer
}
return &opentracing.NoopTracer{}
}
func (fc *Context) Serve(resp *http.Response) {
fc.FServedWithResponse = true
fc.FResponse = resp
fc.FServed = true
}
func (spec *Filter) CreateFilter(config []interface{}) (filters.Filter, error) {
return &Filter{spec.FilterName, config}, nil
}