github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/google.golang.org/appengine/aetest/instance.go (about)

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