|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/http/httputil" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "sync/atomic" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/omalloc/tavern/internal/constants" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + mu sync.RWMutex |
| 23 | + localAddr = "127.0.0.1:8888" |
| 24 | + dump = atomic.Bool{} |
| 25 | + dumpReq = atomic.Bool{} |
| 26 | + |
| 27 | + manual = atomic.Bool{} |
| 28 | +) |
| 29 | + |
| 30 | +type E2E struct { |
| 31 | + caseUrl string |
| 32 | + srcHandler http.Handler |
| 33 | + ts *httptest.Server |
| 34 | + cs *http.Client |
| 35 | + req *http.Request |
| 36 | + resp *http.Response |
| 37 | + err error |
| 38 | +} |
| 39 | + |
| 40 | +func New(caseUrl string, srcHandler http.HandlerFunc) *E2E { |
| 41 | + e := &E2E{ |
| 42 | + caseUrl: caseUrl, |
| 43 | + srcHandler: srcHandler, |
| 44 | + } |
| 45 | + |
| 46 | + u, err := url.Parse(caseUrl) |
| 47 | + if err != nil { |
| 48 | + return e |
| 49 | + } |
| 50 | + |
| 51 | + if e.srcHandler == nil { |
| 52 | + e.srcHandler = http.NotFoundHandler() |
| 53 | + } |
| 54 | + |
| 55 | + e.ts = httptest.NewServer(e.srcHandler) |
| 56 | + |
| 57 | + dialer := &net.Dialer{} |
| 58 | + |
| 59 | + // replace the default transport with a custom one that uses the local address |
| 60 | + e.ts.Client().Transport = &http.Transport{ |
| 61 | + DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { |
| 62 | + mu.RLock() |
| 63 | + addr := localAddr |
| 64 | + mu.RUnlock() |
| 65 | + |
| 66 | + if strings.HasSuffix(addr, ".sock") { |
| 67 | + return dialer.DialContext(ctx, "unix", addr) |
| 68 | + } |
| 69 | + return dialer.DialContext(ctx, network, addr) |
| 70 | + }, |
| 71 | + } |
| 72 | + e.cs = e.ts.Client() |
| 73 | + |
| 74 | + req, err := http.NewRequest(http.MethodGet, e.caseUrl, nil) |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + |
| 79 | + req.Header.Set("X-Test-Case", u.Path) |
| 80 | + req.Header.Set("X-Store-Url", u.String()) |
| 81 | + |
| 82 | + e.req = req |
| 83 | + return e |
| 84 | +} |
| 85 | + |
| 86 | +func (e *E2E) Do(rewrite func(r *http.Request)) (*http.Response, error) { |
| 87 | + rewrite(e.req) |
| 88 | + |
| 89 | + method := e.req.Method |
| 90 | + |
| 91 | + e.req.Header.Set(constants.InternalUpstreamAddr, e.ts.Listener.Addr().String()) |
| 92 | + |
| 93 | + if dumpReq.Load() && method != "PURGE" { |
| 94 | + DumpReq(e.req) |
| 95 | + } |
| 96 | + |
| 97 | + if manual.Load() { |
| 98 | + fmt.Printf("manual mode wait 20s, src addr %q\n", e.ts.Listener.Addr().String()) |
| 99 | + time.Sleep(time.Second * 20) |
| 100 | + } |
| 101 | + |
| 102 | + resp, err := e.cs.Do(e.req) |
| 103 | + e.resp = resp |
| 104 | + e.err = err |
| 105 | + |
| 106 | + // PURGE 不打印响应 |
| 107 | + if dump.Load() && method != "PURGE" { |
| 108 | + DumpResp(resp) |
| 109 | + } |
| 110 | + |
| 111 | + // wait for a while to let the connection close properly |
| 112 | + // time.Sleep(time.Millisecond * 200) |
| 113 | + |
| 114 | + return resp, err |
| 115 | +} |
| 116 | + |
| 117 | +func (e *E2E) Close() { |
| 118 | + e.ts.Close() |
| 119 | + if e.resp != nil && e.resp.Body != nil { |
| 120 | + _ = e.resp.Body.Close() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func SetLocalAddr(addr string) { |
| 125 | + mu.Lock() |
| 126 | + defer mu.Unlock() |
| 127 | + |
| 128 | + localAddr = addr |
| 129 | +} |
| 130 | + |
| 131 | +func SetDump(b bool) { |
| 132 | + dump.Store(b) |
| 133 | +} |
| 134 | + |
| 135 | +func SetDumpReq(b bool) { |
| 136 | + dumpReq.Store(b) |
| 137 | +} |
| 138 | + |
| 139 | +func SetManual(b bool) { |
| 140 | + manual.Store(b) |
| 141 | +} |
| 142 | + |
| 143 | +func DumpReq(req *http.Request) { |
| 144 | + if req == nil { |
| 145 | + fmt.Println("request is nil") |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + buf, err := httputil.DumpRequest(req, false) |
| 150 | + if err != nil { |
| 151 | + return |
| 152 | + } |
| 153 | + fmt.Println() |
| 154 | + fmt.Println(string(buf)) |
| 155 | +} |
| 156 | + |
| 157 | +func DumpResp(resp *http.Response) { |
| 158 | + if resp == nil { |
| 159 | + fmt.Println("response is nil") |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + buf, err := httputil.DumpResponse(resp, false) |
| 164 | + if err != nil { |
| 165 | + return |
| 166 | + } |
| 167 | + fmt.Println() |
| 168 | + fmt.Println(string(buf)) |
| 169 | +} |
| 170 | + |
| 171 | +func Purge(t *testing.T, url string) { |
| 172 | + resp, err := New(url, func(w http.ResponseWriter, r *http.Request) { |
| 173 | + w.WriteHeader(http.StatusBadGateway) |
| 174 | + }).Do(func(r *http.Request) { |
| 175 | + r.Method = "PURGE" |
| 176 | + }) |
| 177 | + |
| 178 | + assert.NoError(t, err, "purge should not error") |
| 179 | + |
| 180 | + assert.Contains(t, []int{http.StatusOK, http.StatusNotFound}, resp.StatusCode, "PURGE should be 404 or 200") |
| 181 | + |
| 182 | + if dump.Load() { |
| 183 | + t.Logf("Purge %s success", url) |
| 184 | + } |
| 185 | +} |
0 commit comments