-
Notifications
You must be signed in to change notification settings - Fork 90
/
server.go
52 lines (44 loc) · 1.15 KB
/
server.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
package upload
import (
"context"
"errors"
"net/http"
"time"
)
func StartMockServer(endpoint string, method string, expectedUpdateCursor string, expectedVersionLabel string, expectedLicense string, archive []byte) (chan bool, error) {
stopCh := make(chan bool)
srv := &http.Server{Addr: ":3001"}
http.HandleFunc("/api/v1/upload", func(w http.ResponseWriter, r *http.Request) {
if r.Method != method {
w.WriteHeader(404)
return
}
w.Write([]byte(`{"slug": "sluggy"}`))
})
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{}`))
})
go func() {
srv.ListenAndServe()
}()
go func() {
<-stopCh
srv.Shutdown(context.TODO())
}()
// for the the http server to be ready
quickClient := &http.Client{
Timeout: time.Millisecond * 100,
}
start := time.Now()
for {
response, err := quickClient.Get("http://localhost:3001/healthz")
if err == nil && response.StatusCode == http.StatusOK {
break
}
if time.Now().Sub(start) > time.Duration(time.Second*5) {
return nil, errors.New("mock server failed to start in the allocated time")
}
time.Sleep(time.Millisecond * 10)
}
return stopCh, nil
}