1package aetest 2 3import ( 4 "io" 5 "net/http" 6 "time" 7 8 "golang.org/x/net/context" 9 "google.golang.org/appengine" 10) 11 12// Instance represents a running instance of the development API Server. 13type Instance interface { 14 // Close kills the child api_server.py process, releasing its resources. 15 io.Closer 16 // NewRequest returns an *http.Request associated with this instance. 17 NewRequest(method, urlStr string, body io.Reader) (*http.Request, error) 18} 19 20// Options is used to specify options when creating an Instance. 21type Options struct { 22 // AppID specifies the App ID to use during tests. 23 // By default, "testapp". 24 AppID string 25 // StronglyConsistentDatastore is whether the local datastore should be 26 // strongly consistent. This will diverge from production behaviour. 27 StronglyConsistentDatastore bool 28 // SupportDatastoreEmulator is whether use Cloud Datastore Emulator or 29 // use old SQLite based Datastore backend or use default settings. 30 SupportDatastoreEmulator *bool 31 // SuppressDevAppServerLog is whether the dev_appserver running in tests 32 // should output logs. 33 SuppressDevAppServerLog bool 34 // StartupTimeout is a duration to wait for instance startup. 35 // By default, 15 seconds. 36 StartupTimeout time.Duration 37} 38 39// NewContext starts an instance of the development API server, and returns 40// a context that will route all API calls to that server, as well as a 41// closure that must be called when the Context is no longer required. 42func NewContext() (context.Context, func(), error) { 43 inst, err := NewInstance(nil) 44 if err != nil { 45 return nil, nil, err 46 } 47 req, err := inst.NewRequest("GET", "/", nil) 48 if err != nil { 49 inst.Close() 50 return nil, nil, err 51 } 52 ctx := appengine.NewContext(req) 53 return ctx, func() { 54 inst.Close() 55 }, nil 56} 57 58// PrepareDevAppserver is a hook which, if set, will be called before the 59// dev_appserver.py is started, each time it is started. If aetest.NewContext 60// is invoked from the goapp test tool, this hook is unnecessary. 61var PrepareDevAppserver func() error 62