Пример #1
0
// GetAllNotes writes a JSON formatted list of all Note IDs in the authorized User's Notebook to w.
func GetAllNotes(w http.ResponseWriter, c appengine.Context, notebook *tessernote.Notebook) {
	notes, err := notebook.Notes(c)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	reply, err := json.Marshal(notes)
	if err != nil {
		c.Errorf("marshaling notes (%d): %s", len(notes), err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Write(reply)
}
Пример #2
0
func TestCreateNote(t *testing.T) {
	note := tessernote.Note{Body: "body"}
	bytes, err := json.Marshal(note)
	if err != nil {
		t.Fatal(err)
	}
	w := httptest.NewRecorder()
	r, err := http.NewRequest("POST", "https://tessernote.appspot.com"+NotesURL, strings.NewReader(string(bytes)))
	if err != nil {
		t.Fatal(err)
	}

	// create a test notebook
	notebook := new(tessernote.Notebook)
	c, err := appenginetesting.NewContext(nil)
	defer c.Close()
	if err != nil {
		t.Fatal(err)
	}
	key := datastore.NewIncompleteKey(c, "Notebook", nil)
	key, err = datastore.Put(c, key, notebook)
	if err != nil {
		t.Fatal(err)
	}
	notebook.ID = key.Encode()

	CreateNote(w, r, c, notebook)

	// check note was added
	notes, err := notebook.Notes(c)
	if err != nil {
		t.Fatal(err)
	}
	if len(notes) != 1 {
		t.Fatalf("expected=%d actual=%d", 1, len(notes))
	}
	if notes[0].Body != note.Body {
		t.Fatalf("expected=%s actual=%s", notes[0].Body, note.Body)
	}

	// check response ID is the same
	response := []byte(w.Body.String())
	err = json.Unmarshal(response, note)
	if err != nil {
		t.Fatal(err, string(response))
	}
	if notes[0].ID != note.ID {
		t.Fatalf("expected=%s actual=%s", notes[0].ID, note.ID)
	}
}