-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.go
More file actions
54 lines (45 loc) · 833 Bytes
/
testing.go
File metadata and controls
54 lines (45 loc) · 833 Bytes
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
package babel
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
)
type MockHttpServer struct {
srv *http.Server
addr string
mux *http.ServeMux
}
func NewMockHttpServer(port uint64) *MockHttpServer {
mux := http.NewServeMux()
addr := fmt.Sprintf("127.0.0.1:%d", port)
s := &http.Server{
Addr: addr,
Handler: mux,
}
go func() {
if err := s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
time.Sleep(500 * time.Millisecond)
srv := &MockHttpServer{
srv: s,
addr: addr,
mux: mux,
}
return srv
}
func (m *MockHttpServer) Mux() *http.ServeMux {
return m.mux
}
func (m *MockHttpServer) Http() string {
return "http://" + m.addr
}
func (m *MockHttpServer) Stop() {
if err := m.srv.Shutdown(context.Background()); err != nil {
log.Fatal(err)
}
}