Exemplo n.º 1
0
func TestGetTasks(t *testing.T) {
	tsks := task.All()
	if len(tsks) != 3 {
		t.Errorf("Number of returned tasks should be 3. Got %d", len(tsks))
	}

	switch tsks[0].ID {
	case "1", "2", "3":
	default:
		t.Errorf("The ID of task should be 1,2 or 3. Got %q", tsks[0].ID)
	}
}
Exemplo n.º 2
0
// getTasks handles the GET request for tasks
func getTasks(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	var err error
	tsks := task.All()
	rw.Header().Set("Content-Type", "application/json")
	rw.Header().Set("Access-Control-Allow-Origin", "*")
	rw.WriteHeader(http.StatusOK)

	// If there are no tasks return empty json
	if tsks == nil {
		if _, err = rw.Write([]byte("[]")); err != nil {
			log.Fatal(err)
		}
		return
	}

	// Send to user all tasks
	if err = json.NewEncoder(rw).Encode(tsks); err != nil {
		log.Fatal(err)
	}
}