// FakeSingleContext returns a context whose Call invocations will be serviced // by f, which should be a function that has two arguments of the input and output // protocol buffer type, and one error return. func FakeSingleContext(t *testing.T, service, method string, f interface{}) context.Context { fv := reflect.ValueOf(f) if fv.Kind() != reflect.Func { t.Fatal("not a function") } ft := fv.Type() if ft.NumIn() != 2 || ft.NumOut() != 1 { t.Fatalf("f has %d in and %d out, want 2 in and 1 out", ft.NumIn(), ft.NumOut()) } for i := 0; i < 2; i++ { at := ft.In(i) if !at.Implements(protoMessageType) { t.Fatalf("arg %d does not implement proto.Message", i) } } if ft.Out(0) != errorType { t.Fatalf("f's return is %v, want error", ft.Out(0)) } s := &single{ t: t, service: service, method: method, f: fv, } return internal.WithCallOverride(internal.ContextForTesting(&http.Request{}), s.call) }
func TestBasicUserAPI(t *testing.T) { for i, tc := range basicUserTests { req := baseReq() req.Header.Set("X-AppEngine-User-Nickname", tc.nickname) req.Header.Set("X-AppEngine-User-Email", tc.email) req.Header.Set("X-AppEngine-Auth-Domain", tc.authDomain) req.Header.Set("X-AppEngine-User-Is-Admin", tc.admin) c := internal.ContextForTesting(req) if ga := IsAdmin(c); ga != tc.isAdmin { t.Errorf("test %d: expected IsAdmin(c) = %v, got %v", i, tc.isAdmin, ga) } u := Current(c) if tc.isNil { if u != nil { t.Errorf("test %d: expected u == nil, got %+v", i, u) } continue } if u == nil { t.Errorf("test %d: expected u != nil, got nil", i) continue } if u.Email != tc.email { t.Errorf("test %d: expected u.Email = %q, got %q", i, tc.email, u.Email) } if gs := u.String(); gs != tc.displayName { t.Errorf("test %d: expected u.String() = %q, got %q", i, tc.displayName, gs) } if u.Admin != tc.isAdmin { t.Errorf("test %d: expected u.Admin = %v, got %v", i, tc.isAdmin, u.Admin) } } }