-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
59 lines (47 loc) · 942 Bytes
/
http_test.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
package mediaprobe_test
import (
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"time"
)
type Server struct {
srv *httptest.Server
}
func (s *Server) Stop() {
s.srv.Close()
}
func (s *Server) Endpoint() string {
return s.srv.URL
}
func ServeHttp(handler *Handler) *Server {
httptest.NewServer(handler)
srv := httptest.NewServer(handler)
time.Sleep(100 * time.Millisecond)
return &Server{srv: srv}
}
type Handler struct {
Status int
Filename string
FailOnAttempt uint64
Counter uint64
mu sync.RWMutex
}
func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if h.Status != 0 {
rw.WriteHeader(h.Status)
return
}
if h.FailOnAttempt > 0 {
h.mu.RLock()
counter := h.Counter
h.mu.RUnlock()
if counter >= h.FailOnAttempt {
rw.WriteHeader(http.StatusInternalServerError)
return
}
atomic.AddUint64(&h.Counter, 1)
}
http.ServeFile(rw, req, h.Filename)
}