// Homework 45, challenge: Update an item. Solution. package main import ( "encoding/json" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" ) // useTempStore gives the handlers an empty store in a temporary folder. func useTempStore(t *testing.T) { s, err := openItemStore(filepath.Join(t.TempDir(), "items.json")) if err != nil { t.Fatalf("openItemStore: %v", err) } store = s } // send runs one request through the routes and returns what was written. func send(method string, target string, body string) *httptest.ResponseRecorder { request := httptest.NewRequest(method, target, strings.NewReader(body)) recorder := httptest.NewRecorder() routes().ServeHTTP(recorder, request) return recorder } func TestListEmpty(t *testing.T) { useTempStore(t) response := send("GET", "/api/items", "") if response.Code != http.StatusOK { t.Fatalf("status = %d, want %d", response.Code, http.StatusOK) } if got := response.Header().Get("Content-Type"); got != "application/json" { t.Errorf("Content-Type = %q, want application/json", got) } if got := strings.TrimSpace(response.Body.String()); got != "[]" { t.Errorf("body = %q, want []", got) } } func TestCreateThenGet(t *testing.T) { useTempStore(t) response := send("POST", "/api/items", `{"name": " Rice ", "quantity": 2}`) if response.Code != http.StatusCreated { t.Fatalf("POST status = %d, want %d; body %s", response.Code, http.StatusCreated, response.Body) } if got := response.Header().Get("Location"); got != "/api/items/1" { t.Errorf("Location = %q, want /api/items/1", got) } response = send("GET", "/api/items/1", "") if response.Code != http.StatusOK { t.Fatalf("GET status = %d, want %d", response.Code, http.StatusOK) } var got item err := json.Unmarshal(response.Body.Bytes(), &got) if err != nil { t.Fatalf("body is not an item: %v", err) } want := item{ID: 1, Name: "Rice", Quantity: 2} if got != want { t.Errorf("got %+v, want %+v", got, want) } } func TestCreateRejectsBadInput(t *testing.T) { tests := []struct { name string body string want int }{ {"not JSON", `hello`, http.StatusBadRequest}, {"unknown field", `{"name": "Rice", "quantity": 2, "colour": "white"}`, http.StatusBadRequest}, {"empty name", `{"name": " ", "quantity": 2}`, http.StatusBadRequest}, {"no quantity", `{"name": "Rice"}`, http.StatusBadRequest}, {"two objects", `{"name": "Rice", "quantity": 2} {}`, http.StatusBadRequest}, {"too big", `{"name": "` + strings.Repeat("a", 5000) + `"}`, http.StatusRequestEntityTooLarge}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { useTempStore(t) response := send("POST", "/api/items", test.body) if response.Code != test.want { t.Errorf("status = %d, want %d; body %s", response.Code, test.want, response.Body) } if len(store.All()) != 0 { t.Errorf("a bad request added an item") } }) } } func TestGetBadID(t *testing.T) { useTempStore(t) if response := send("GET", "/api/items/abc", ""); response.Code != http.StatusBadRequest { t.Errorf("GET /api/items/abc: status = %d, want %d", response.Code, http.StatusBadRequest) } if response := send("GET", "/api/items/99", ""); response.Code != http.StatusNotFound { t.Errorf("GET /api/items/99: status = %d, want %d", response.Code, http.StatusNotFound) } } func TestDelete(t *testing.T) { useTempStore(t) send("POST", "/api/items", `{"name": "Tea", "quantity": 1}`) response := send("DELETE", "/api/items/1", "") if response.Code != http.StatusNoContent { t.Fatalf("first DELETE: status = %d, want %d", response.Code, http.StatusNoContent) } response = send("DELETE", "/api/items/1", "") if response.Code != http.StatusNotFound { t.Errorf("second DELETE: status = %d, want %d", response.Code, http.StatusNotFound) } } func TestItemsSurviveARestart(t *testing.T) { path := filepath.Join(t.TempDir(), "items.json") first, err := openItemStore(path) if err != nil { t.Fatalf("openItemStore: %v", err) } _, err = first.Add("Rice", 2) if err != nil { t.Fatalf("Add: %v", err) } second, err := openItemStore(path) if err != nil { t.Fatalf("openItemStore again: %v", err) } items := second.All() if len(items) != 1 || items[0].Name != "Rice" { t.Errorf("after reopening, items = %+v, want one Rice", items) } } func TestUpdate(t *testing.T) { useTempStore(t) send("POST", "/api/items", `{"name": "Rice", "quantity": 2}`) response := send("PUT", "/api/items/1", `{"name": "Brown rice", "quantity": 5}`) if response.Code != http.StatusOK { t.Fatalf("PUT status = %d, want %d; body %s", response.Code, http.StatusOK, response.Body) } got, err := store.Get(1) if err != nil { t.Fatalf("Get: %v", err) } want := item{ID: 1, Name: "Brown rice", Quantity: 5} if got != want { t.Errorf("after PUT, item = %+v, want %+v", got, want) } } func TestUpdateRejects(t *testing.T) { tests := []struct { name string target string body string want int }{ {"no such item", "/api/items/9", `{"name": "Tea", "quantity": 1}`, http.StatusNotFound}, {"bad id", "/api/items/abc", `{"name": "Tea", "quantity": 1}`, http.StatusBadRequest}, {"empty name", "/api/items/1", `{"name": "", "quantity": 1}`, http.StatusBadRequest}, {"unknown field", "/api/items/1", `{"name": "Tea", "quantity": 1, "id": 5}`, http.StatusBadRequest}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { useTempStore(t) send("POST", "/api/items", `{"name": "Rice", "quantity": 2}`) response := send("PUT", test.target, test.body) if response.Code != test.want { t.Errorf("status = %d, want %d; body %s", response.Code, test.want, response.Body) } got, err := store.Get(1) if err != nil || got.Name != "Rice" { t.Errorf("the item changed to %+v", got) } }) } }