-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
52 lines (42 loc) · 838 Bytes
/
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
package main
import (
"context"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
"time"
"github.com/agis/spawn"
)
func TestMain(m *testing.M) {
// start the server
server := spawn.New(main)
ctx, cancel := context.WithCancel(context.Background())
server.Start(ctx)
// wait a bit for it to become ready
time.Sleep(500 * time.Millisecond)
// execute the test suite
result := m.Run()
// cleanly shutdown server
cancel()
err := server.Wait()
if err != nil {
log.Fatal(err)
}
os.Exit(result)
}
func TestFoo(t *testing.T) {
res, err := http.Get("http://localhost:8080/foo")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if string(resBody) != "Hello!" {
t.Fatalf("expected response to be 'Hello!', got '%s'", resBody)
}
}