-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
51 lines (41 loc) · 980 Bytes
/
server_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
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
var (
TestServerAddr = "localhost:"
)
// Setup function. We expect parseJsonFile to succeed before running all subsequent tests
//
func TestMain(m *testing.M) {
// Setup for the JSON file
tempFile, err := ioutil.TempFile("", "qrest")
n, err := tempFile.Write([]byte(jsonTestData))
tempFile.Close()
JsonFilePath = tempFile.Name()
if n != len([]byte(jsonTestData)) {
fmt.Fprintln(os.Stderr, "invalid number of bytes written to temp file")
os.Exit(1)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err == nil {
parseJsonFile(tempFile.Name())
// Attempt to start the server on a (hopefully) unused port
if port := os.Getenv("TEST_PORT"); port != "" {
TestServerAddr += port
} else {
TestServerAddr += "3050"
}
logger.Out = ioutil.Discard
go StartServer(TestServerAddr)
} else {
fmt.Fprintln(os.Stderr, "could not create temp file")
}
os.Exit(m.Run())
}