• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	// StartupTimeout is a duration to wait for instance startup.
29	// By default, 15 seconds.
30	StartupTimeout time.Duration
31}
32
33// NewContext starts an instance of the development API server, and returns
34// a context that will route all API calls to that server, as well as a
35// closure that must be called when the Context is no longer required.
36func NewContext() (context.Context, func(), error) {
37	inst, err := NewInstance(nil)
38	if err != nil {
39		return nil, nil, err
40	}
41	req, err := inst.NewRequest("GET", "/", nil)
42	if err != nil {
43		inst.Close()
44		return nil, nil, err
45	}
46	ctx := appengine.NewContext(req)
47	return ctx, func() {
48		inst.Close()
49	}, nil
50}
51
52// PrepareDevAppserver is a hook which, if set, will be called before the
53// dev_appserver.py is started, each time it is started. If aetest.NewContext
54// is invoked from the goapp test tool, this hook is unnecessary.
55var PrepareDevAppserver func() error
56