1
2
3
4
5 package intercept
6
7 import (
8 "errors"
9 "net/http"
10 "net/url"
11 )
12
13
14
15 type Interceptor struct {
16 Scheme string
17 FromHost string
18 ToHost string
19 Client *http.Client
20 }
21
22
23 func EnableTestHooks(interceptors []Interceptor) error {
24 if TestHooksEnabled {
25 return errors.New("web: test hooks already enabled")
26 }
27
28 for _, t := range interceptors {
29 if t.FromHost == "" {
30 panic("EnableTestHooks: missing FromHost")
31 }
32 if t.ToHost == "" {
33 panic("EnableTestHooks: missing ToHost")
34 }
35 }
36
37 testInterceptors = interceptors
38 TestHooksEnabled = true
39 return nil
40 }
41
42
43 func DisableTestHooks() {
44 if !TestHooksEnabled {
45 panic("web: test hooks not enabled")
46 }
47 TestHooksEnabled = false
48 testInterceptors = nil
49 }
50
51 var (
52
53 TestHooksEnabled = false
54 testInterceptors []Interceptor
55 )
56
57
58 func URL(u *url.URL) (*Interceptor, bool) {
59 if !TestHooksEnabled {
60 return nil, false
61 }
62 for i, t := range testInterceptors {
63 if u.Host == t.FromHost && (u.Scheme == "" || u.Scheme == t.Scheme) {
64 return &testInterceptors[i], true
65 }
66 }
67 return nil, false
68 }
69
70
71 func Request(req *http.Request) {
72 if t, ok := URL(req.URL); ok {
73 req.Host = req.URL.Host
74 req.URL.Host = t.ToHost
75 }
76 }
77
View as plain text