本文整理匯總了Golang中github.com/gaego/context.NewContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewContext函數的具體用法?Golang NewContext怎麽用?Golang NewContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CurrentUserHasRole
// CurrentUserHasRole checks for the presents of a role listed under the current user.
// The role is retrieved from the users session to save on lookups.
func CurrentUserHasRole(w http.ResponseWriter, r *http.Request, role string) bool {
// Confirm we have a user.
if id, err := CurrentUserID(r); id != "" || err != nil {
return false
}
c := context.NewContext(r)
store, err := session.GetStore(c)
if err != nil {
c.Criticalf("user: There was an error retrieving the session store Error: %v", err)
return false
}
// 1st Check the session.
s, err := store.Get(r, "user|roles")
if err != nil {
c.Criticalf("user: There was an error retrieving the session Error: %v", err)
return false
}
if s.Values[role] == true {
return true
}
// 2nd Check the ds.
u, err := Current(r)
if err != nil {
return false
}
if u.HasRole(role) {
// Set the role to true in the session to avoid this look up in the future.
if err = CurrentUserSetRole(w, r, role, true); err != nil {
return false
}
return true
}
return false
}
示例2: TestGetOrInsert
func TestGetOrInsert(t *testing.T) {
defer context.Close()
c := context.NewContext(nil)
// Set it.
m := map[string]string{
"A": "1",
}
cnfg, err := GetOrInsert(c, "first", m)
// Confirm.
checkErr(t, err)
if x := cnfg.Values["A"]; x != "1" {
t.Errorf(`config["A"]: %v, want %v`, x, "1")
}
// The orginal map should be returned.
m = map[string]string{
"A": "2",
}
cnfg, err = GetOrInsert(c, "first", m)
// Confirm.
checkErr(t, err)
if x := cnfg.Values["A"]; x != "1" {
t.Errorf(`config["A"]: %v, want %v`, x, "1")
}
}
示例3: Test_handler
func Test_handler(t *testing.T) {
setup()
defer teardown()
_ = context.NewContext(nil)
// Register the Provider
p := &TPComplete{}
Register("example5", p)
r, _ := http.NewRequest("GET", "http://localhost:8080/-/auth/example5", nil)
w := httptest.NewRecorder()
// Run it through the auth handler.
handler(w, r)
// Inspected the redirect.
hdr := w.Header()
if hdr["Location"][0] != SuccessURL {
t.Errorf(`hdr["Location"]: %q, want %q`, hdr["Location"][0], SuccessURL)
t.Errorf(`w: %q`, w)
t.Errorf(`hdr: %q`, hdr)
}
}
示例4: TestGetStore
func TestGetStore(t *testing.T) {
setup()
defer teardown()
r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
w := httptest.NewRecorder()
c := context.NewContext(r)
// create the store
tStore, err := GetStore(c)
if err != nil {
t.Errorf(`err getting store: %q, want nil`, err)
}
// Get or Create a new session using the store
s, err := tStore.Get(r, "t1")
if err != nil {
t.Errorf(`err getting session: %q, want nil`, err)
}
// Confirm api
s.Values["val1"] = "example value"
err = s.Save(r, w)
if err != nil {
t.Errorf(`err saving session: %q, want nil`, err)
}
}
示例5: TestEdit
func TestEdit(t *testing.T) {
defer context.Close()
c := context.NewContext(nil)
// Set it.
m := map[string]string{
"A": "1",
}
cnfg, err := GetOrInsert(c, "first", m)
// Change it.
cnfg.Values["A"] = "2"
// Save it.
err = cnfg.Put(c)
checkErr(t, err)
// Confirm.
cnfg, err = Get(c, "first")
checkErr(t, err)
if x := cnfg.Values["A"]; x != "2" {
t.Errorf(`cnfg["A"]: %v, want %v`, x, "2")
}
}
示例6: TestDelete
func TestDelete(t *testing.T) {
setup()
c := context.NewContext(nil)
defer tearDown()
var x interface{}
// Put A
x = &A{S: "a", I: 1, B: []byte{}, T: now}
key := datastore.NewKey(c, "A", "1", 0, nil)
key, _ = Put(c, key, x)
// Delete A
err := Delete(c, key)
if err != nil {
t.Errorf(`err = %s; expected nil`, err)
}
// Test Store for absence of values
x = &A{}
err = memory.Get(c, key, x)
if err != dserrors.ErrNoSuchEntity {
t.Errorf(`err = %s; expected %s`, err, dserrors.ErrNoSuchEntity)
}
err = memcache.Get(c, key, x)
if err != dserrors.ErrNoSuchEntity {
t.Errorf(`err = %s; expected %s`, err, dserrors.ErrNoSuchEntity)
}
err = dsds.Get(c, key, x)
if err != dserrors.ErrNoSuchEntity {
t.Errorf(`err = %s; expected %s`, err, dserrors.ErrNoSuchEntity)
}
}
示例7: TestDelete
func TestDelete(t *testing.T) {
setup()
defer tearDown()
c := context.NewContext(nil)
// Put.
k, x := NewX(c, "1")
_, err := s.Put(c, k, x)
// Delete.
err = s.Delete(c, k)
if err != nil {
t.Errorf("err: %v; want: %v.", err, nil)
}
// Confirm.
if cnt := s.Count(c); cnt != 0 {
t.Errorf(`After delete; Count: %v; want %v`, cnt, 0)
}
// Delete non-existence
k = datastore.NewKey(c, "X", "fakekey", 0, nil)
r := new(X)
err = s.Get(c, k, r)
if err != dse.ErrNoSuchEntity {
t.Errorf("err: %v; want: %v.", err, dse.ErrNoSuchEntity)
}
}
示例8: Authenticate
// Authenticate process the request and returns a populated UserProfile.
// If the Authenticate method can not authenticate the User based on the
// request, an error or a redirect URL wll be return.
func (p *Provider) Authenticate(w http.ResponseWriter, r *http.Request) (
up *profile.Profile, redirectURL string, err error) {
c := context.NewContext(r)
url := r.FormValue("provider")
// Set provider info.
up = profile.New(p.Name, url)
// Check for current User.
u := aeuser.Current(c)
if u == nil {
redirectURL := r.URL.Path + "/callback"
loginUrl, err := aeuser.LoginURLFederated(c, redirectURL, url)
return up, loginUrl, err
}
if u.FederatedIdentity != "" {
up.ID = u.FederatedIdentity
} else {
up.ID = u.ID
}
per := new(person.Person)
per.Email = u.Email
per.Emails = []*person.PersonEmails{
&person.PersonEmails{true, "home", u.Email},
}
per.URL = u.FederatedIdentity
up.Person = per
return up, "", nil
}
示例9: verifyHandler
func verifyHandler(w http.ResponseWriter, r *http.Request) {
//code := r.FormValue("code")
var e *Email
var et *token.Token
var err error
c := context.NewContext(r)
code := r.URL.Query().Get("code")
errURL := "/"
successURL := "/"
et, err = token.Get(c, code)
if err != nil {
goto Error
}
e, err = Get(c, et.EmailAddress)
if err != nil {
goto Error
}
e.Status = verified
if err = e.Put(c); err != nil {
goto Error
}
http.Redirect(w, r, successURL, http.StatusFound)
Error:
// TODO added error to session
http.Redirect(w, r, errURL, http.StatusNotFound)
}
示例10: TestGet
func TestGet(t *testing.T) {
setup()
defer tearDown()
c := context.NewContext(nil)
// Put.
k, x := NewX(c, "1")
_, err := s.Put(c, k, x)
// Get.
r := new(X)
err = s.Get(c, k, r)
if err != nil {
t.Errorf("err: %v; want: %v.", err, nil)
}
// Confirm.
CheckX(c, t, r, "1")
// Get non-existence
k = datastore.NewKey(c, "X", "fakekey", 0, nil)
r = new(X)
err = s.Get(c, k, r)
if err != dse.ErrNoSuchEntity {
t.Errorf("err: %v; want: %v.", err, dse.ErrNoSuchEntity)
}
}
示例11: TestPut
func TestPut(t *testing.T) {
setup()
defer tearDown()
c := context.NewContext(nil)
k, x1 := NewX(c, "1")
_, err := s.Put(c, k, x1)
if err != nil {
t.Errorf("err: %v; want: %v.", err, nil)
}
}
示例12: CreateAndLogin
// CreateAndLogin does the following:
//
// - Search for an existing user - session -> Profile -> email address
// - Saves the Profile to the datastore
// - Creates a User or appends the AuthID to the Requesting user's account
// - Logs in the User
// - Adds the admin role to the User if they are an GAE Admin.
func CreateAndLogin(w http.ResponseWriter, r *http.Request,
p *profile.Profile) (u *user.User, err error) {
c := context.NewContext(r)
if u, err = p.UpdateUser(w, r); err != nil {
return
}
if err = user.CurrentUserSetID(w, r, p.UserID); err != nil {
return
}
err = p.Put(c)
return
}
示例13: account
func account(w http.ResponseWriter, r *http.Request) {
m := tmplMap()
c := context.NewContext(r)
if u, err := user.Current(r); err == nil {
m["User"] = u
c.Debugf("User: %v", u)
c.Debugf("User.Person: %v", u.Person)
}
if err := accountTmpl.Execute(w, m); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
示例14: callback
func (p *Provider) callback(r *http.Request) error {
// Exchange code for an access token at OAuth provider.
code := r.FormValue("code")
t := &oauth.Transport{
Config: p.Config(r.URL),
Transport: &urlfetch.Transport{
Context: context.NewContext(r),
},
}
_, err := t.Exchange(code)
return err
}
示例15: TestNewKey
func TestNewKey(t *testing.T) {
c := context.NewContext(nil)
defer tearDown()
k1 := datastore.NewKey(c, "AuthProfile", "google|12345", 0, nil)
k2 := newKey(c, "Google", "12345")
if k1.String() != k2.String() {
t.Errorf("k2: %q, want %q.", k2, k1)
t.Errorf("k1:", k1)
t.Errorf("k2:", k2)
}
}