示例#1
0
func TestGetTask(t *testing.T) {
	tsk := task.Get("1")
	if tsk.Name != "Buy milk" {
		t.Error("Get returned wrong task")
	}

	tsk = task.Get("2")
	if tsk.Name != "Write todo app" {
		t.Error("Get returned wrong task")
	}

	tsk = task.Get("1000")
	if tsk != nil {
		t.Error("Returned not existing task")
	}
}
示例#2
0
// getTask handles the GET request for specific task
func getTask(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
	var err error
	tsk := task.Get(p.ByName("id"))

	// Handle if the task doesn't exists
	if tsk == nil {
		rw.WriteHeader(http.StatusNotFound)
		if _, err = rw.Write([]byte("Task with given ID doesn't exists")); err != nil {
			log.Fatal(err)
		}
		return
	}

	// Send to user the found task
	rw.Header().Set("Content-Type", "application/json")
	if err = json.NewEncoder(rw).Encode(tsk); err != nil {
		log.Fatal(err)
	}
}