-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
63 lines (53 loc) · 1.32 KB
/
main_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
60
61
62
63
package main
import (
"log"
"net/http"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)
func TestRESTEndpointsAvailableIntegration(t *testing.T) {
port := 61978
tests := []struct {
endpoint string
method string
}{
{
endpoint: "http://localhost:" + strconv.Itoa(port) + "/update",
method: http.MethodGet,
},
}
server := newServer(port, func(writer http.ResponseWriter, request *http.Request) {
})
go func() {
log.Fatal(server.ListenAndServe())
}()
// Avoid: http://localhost:61978/update: dial tcp 127.0.0.1:61978: connect: connection refused
waitForConnection(t, "http://localhost:"+strconv.Itoa(port))
for _, test := range tests {
req, _ := http.NewRequest(test.method, test.endpoint, nil)
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.NotNil(t, res)
assert.NotEqual(t, http.StatusNotFound, res.StatusCode)
_ = res.Body.Close()
}
}
func waitForConnection(t *testing.T, url string) {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
for {
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("%s", err.Error())
if strings.Contains(err.Error(), "connection refused") {
continue
}
require.NoError(t, err)
}
_ = resp.Body.Close()
break
}
}