-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
executable file
·85 lines (70 loc) · 1.59 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package groupme
import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/suite"
)
/*//////// Base API Suite ////////*/
type APISuite struct {
// Base attributes
suite.Suite
client *Client
server *http.Server
wg sync.WaitGroup
// Overridden by child Suite
addr string
handler http.Handler
}
func (s *APISuite) setupSuite() {
s.addr = "localhost:" + GeneratePort()
s.client = NewClient("")
s.client.apiEndpointBase = "http://" + s.addr
s.server = s.startServer(s.addr, s.handler)
}
func (s *APISuite) TearDownSuite() {
s.client.Close()
s.server.Close()
s.wg.Wait()
}
/*/// Start Server ///*/
func (s *APISuite) startServer(addr string, handler http.Handler) *http.Server {
server := &http.Server{
Addr: addr,
Handler: handler,
ErrorLog: log.New(os.Stdout, "SERVER", log.Ltime),
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := server.ListenAndServe(); err.Error() != "http: Server closed" {
s.Assert().NoError(err)
}
}()
// Wait until server has started listening
url := fmt.Sprintf("http://%s", addr)
// nolint // url is meant to be variable
for _, err := http.Get(url); err != nil; _, err = http.Get(url) {
continue
}
return server
}
/*/// Generate Ephemeral Port ///*/
const (
portMin = 49152
portMax = 65535
portRange = portMax - portMin
)
func GeneratePort() string {
// nolint // weak random generator is ok for creating port number in a test
return strconv.Itoa((rand.Intn(portRange) + portMin))
}
/*//////// Test Main ////////*/
func TestMain(m *testing.M) {
os.Exit(m.Run())
}