aetest: using test app id for test requests tickets.

For any tests using aetest.NewContext(), this will look for
the overridden test app id to construct the ticket.

Fixes #27

Change-Id: I01ac05360885bd47d577aa016906ba0a87a10608
diff --git a/internal/api.go b/internal/api.go
index e9c56d4..09562c4 100644
--- a/internal/api.go
+++ b/internal/api.go
@@ -32,7 +32,8 @@
 )
 
 const (
-	apiPath = "/rpc_http"
+	apiPath             = "/rpc_http"
+	defaultTicketSuffix = "/default.20150612t184001.0"
 )
 
 var (
@@ -269,8 +270,13 @@
 	return withContext(parent, c)
 }
 
-func getDefaultTicket() string {
+// DefaultTicket returns a ticket used for background context or dev_appserver.
+func DefaultTicket() string {
 	defaultTicketOnce.Do(func() {
+		if IsDevAppServer() {
+			defaultTicket = "testapp" + defaultTicketSuffix
+			return
+		}
 		appID := partitionlessAppID()
 		escAppID := strings.Replace(strings.Replace(appID, ":", "_", -1), ".", "_", -1)
 		majVersion := VersionID(nil)
@@ -291,7 +297,7 @@
 	}
 
 	// Compute background security ticket.
-	ticket := getDefaultTicket()
+	ticket := DefaultTicket()
 
 	ctxs.bg = &context{
 		req: &http.Request{
@@ -485,9 +491,15 @@
 	}
 
 	ticket := c.req.Header.Get(ticketHeader)
-	// Fall back to use background ticket when the request ticket is not available in Flex.
+	// Use a test ticket under test environment.
 	if ticket == "" {
-		ticket = getDefaultTicket()
+		if appid := ctx.Value(&appIDOverrideKey); appid != nil {
+			ticket = appid.(string) + defaultTicketSuffix
+		}
+	}
+	// Fall back to use background ticket when the request ticket is not available in Flex or dev_appserver.
+	if ticket == "" {
+		ticket = DefaultTicket()
 	}
 	req := &remotepb.Request{
 		ServiceName: &service,
diff --git a/internal/api_test.go b/internal/api_test.go
index 945a2c1..48368e2 100644
--- a/internal/api_test.go
+++ b/internal/api_test.go
@@ -92,7 +92,7 @@
 		http.Error(w, fmt.Sprintf("Bad encoded API request: %v", err), 500)
 		return
 	}
-	if *apiReq.RequestId != "s3cr3t" && *apiReq.RequestId != getDefaultTicket() {
+	if *apiReq.RequestId != "s3cr3t" && *apiReq.RequestId != DefaultTicket() {
 		writeResponse(&remotepb.Response{
 			RpcError: &remotepb.RpcError{
 				Code:   proto.Int32(int32(remotepb.RpcError_SECURITY_VIOLATION)),