instance.go 1.5 KB

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