Example #1
0
func Index(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "GET request only", http.StatusMethodNotAllowed)
		return
	}

	c := appengine.NewContext(r)

	q := datastore.NewQuery("Greeting").Ancestor(models.GuestBookKey(c)).Order("-Date").Limit(10)
	greetings := make([]models.Greeting, 0, 10)
	if _, err := q.GetAll(c, &greetings); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	type exec struct {
		Author    string
		Greetings []models.Greeting
	}
	e := exec{
		Greetings: greetings,
	}
	if u := user.Current(c); u != nil {
		e.Author = u.String()
	}

	if err := views.GuestBookTemplate.Execute(w, e); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
func GuestSign(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "POST request only", http.StatusMethodNotAllowed)
		return
	}

	c := appengine.NewContext(r)

	g := models.Greeting{
		Content: r.FormValue("content"),
		Date:    time.Now(),
	}

	if u := user.Current(c); u != nil {
		g.Author = u.String()
	}

	key := datastore.NewIncompleteKey(c, "Greeting", models.GuestBookKey(c))
	_, err := datastore.Put(c, key, &g)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	http.Redirect(w, r, "/", http.StatusSeeOther)
}
func TaskBackend(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	content := r.FormValue("content")
	c.Debugf("content = %s", content)

	for i := 0; i < 1; i++ {
		g := models.Greeting{
			Content: content + fmt.Sprintf(" inc = %d", i),
			Date:    time.Now(),
		}

		key := datastore.NewIncompleteKey(c, "Greeting", models.GuestBookKey(c))
		_, err := datastore.Put(c, key, &g)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	fmt.Fprintf(w, "task exec end")
}
func TestSign(t *testing.T) {
	now := time.Now()

	inst, err := aetest.NewInstance(nil)
	if err != nil {
		t.Error(err)
	}
	defer inst.Close()

	testCases := []struct {
		method   string
		content  string
		user     *user.User
		code     int
		greeting *models.Greeting
	}{
		{
			method: "GET",
			code:   405,
		},
		{
			method:   "POST",
			content:  "Normal post",
			code:     303,
			greeting: &models.Greeting{Content: "Normal post"},
		},
		{
			method:   "POST",
			content:  "Post with user",
			user:     &user.User{Email: "*****@*****.**"},
			code:     303,
			greeting: &models.Greeting{Content: "Post with user", Author: "*****@*****.**"},
		},
	}

	for _, tt := range testCases {
		// create RequestData
		val := url.Values{
			"content": []string{tt.content},
		}

		req, err := inst.NewRequest(tt.method, "/guest-book/sign", strings.NewReader(val.Encode()))
		if err != nil {
			t.Error(err)
		}

		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
		if tt.user != nil {
			aetest.Login(tt.user, req)
		}

		resp := httptest.NewRecorder()
		// exec request
		GuestSign(resp, req)

		if resp.Code != tt.code {
			t.Errorf("Got response code %d; want %d; body:\n%s", resp.Code, tt.code, resp.Body.String())
		}

		// Check the latest greeting against our expectation.
		c := appengine.NewContext(req)
		q := datastore.NewQuery("Greeting").Ancestor(models.GuestBookKey(c)).Order("-Date").Limit(1)
		var g models.Greeting
		_, err = q.Run(c).Next(&g)
		if err == datastore.Done {
			if tt.greeting != nil {
				t.Errorf("No greeting stored. Expected %v", tt.greeting)
			}
			continue
		}
		if err != nil {
			t.Errorf("Failed to fetch greeting: %v", err)
		}
		if tt.greeting == nil {
			if !g.Date.Before(now) {
				t.Errorf("Expected no new greeting, found: %v", g)
			}
			continue
		}

		if g.Date.Before(now) {
			t.Errorf("Greeting stored at %v, want at least %v", g.Date, now)
		}

		g.Date = time.Time{}
		if !reflect.DeepEqual(g, *tt.greeting) {
			t.Errorf("Greetings don't match. \nGot: %v\nWant: %v", g, *tt.greeting)
		}
	}
}