-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
gae_test.go
63 lines (50 loc) · 1.36 KB
/
gae_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
//go:build never
package examples
import (
"net/http"
"os"
"testing"
"github.com/gavv/httpexpect/v2"
"google.golang.org/appengine/aetest"
)
// These tests require installed Google Appengine SDK.
// https://cloud.google.com/appengine/downloads
// init() is used by GAE to start serving the app
// added here for illustration purposes
func init() {
http.Handle("/", GaeHandler())
}
// gaeInstance is our global dev_appserver instance.
var gaeInstance aetest.Instance
// TestMain is called first to create the gaeInstance.
func TestMain(m *testing.M) {
var err error
gaeInstance, err = aetest.NewInstance(nil)
if err != nil {
panic(err)
}
c := m.Run() // call all actual tests
gaeInstance.Close()
os.Exit(c)
}
// gaeTester returns a new Expect instance to test GaeHandler().
func gaeTester(t *testing.T) *httpexpect.Expect {
return httpexpect.WithConfig(httpexpect.Config{
// Use gaeInstance to create requests.
// aetest.Instance is compatible with httpexpect.RequestFactory.
RequestFactory: gaeInstance,
// Pass requests directly to GaeHandler.
Client: &http.Client{
Transport: httpexpect.NewBinder(GaeHandler()),
Jar: httpexpect.NewJar(),
},
// Report errors using testify.
Reporter: httpexpect.NewAssertReporter(t),
})
}
func TestGae(t *testing.T) {
e := gaeTester(t)
e.GET("/ping").Expect().
Status(200).
Text().Equal("pong")
}