Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cookbook/crud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ func updateUser(c *echo.Context) error {
return err
}
id, _ := strconv.Atoi(c.Param("id"))
users[id].Name = u.Name
return c.JSON(http.StatusOK, users[id])
existingUser, ok := users[id]
if !ok {
return echo.NewHTTPError(http.StatusNotFound, "user not found")
}
existingUser.Name = u.Name
return c.JSON(http.StatusOK, existingUser)
}

func deleteUser(c *echo.Context) error {
Expand Down
69 changes: 69 additions & 0 deletions cookbook/crud/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)

func newTestServer() *echo.Echo {
lock.Lock()
users = map[int]*user{}
seq = 1
lock.Unlock()

e := echo.New()
e.Use(middleware.RequestLogger())
e.Use(middleware.Recover())
e.GET("/users", getAllUsers)
e.POST("/users", createUser)
e.GET("/users/:id", getUser)
e.PUT("/users/:id", updateUser)
e.DELETE("/users/:id", deleteUser)
return e
}

func TestUpdateUserNotFound(t *testing.T) {
e := newTestServer()
req := httptest.NewRequest(http.MethodPut, "/users/9999", strings.NewReader(`{"name":"x"}`))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()

e.ServeHTTP(rec, req)

if rec.Code != http.StatusNotFound {
t.Fatalf("expected status %d, got %d: %s", http.StatusNotFound, rec.Code, rec.Body.String())
}
}

func TestUpdateUser(t *testing.T) {
e := newTestServer()
createReq := httptest.NewRequest(http.MethodPost, "/users", strings.NewReader(`{"name":"before"}`))
createReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
createRec := httptest.NewRecorder()
e.ServeHTTP(createRec, createReq)
if createRec.Code != http.StatusCreated {
t.Fatalf("expected create status %d, got %d: %s", http.StatusCreated, createRec.Code, createRec.Body.String())
}

updateReq := httptest.NewRequest(http.MethodPut, "/users/1", strings.NewReader(`{"name":"after"}`))
updateReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
updateRec := httptest.NewRecorder()
e.ServeHTTP(updateRec, updateReq)
if updateRec.Code != http.StatusOK {
t.Fatalf("expected update status %d, got %d: %s", http.StatusOK, updateRec.Code, updateRec.Body.String())
}

var updated user
if err := json.Unmarshal(updateRec.Body.Bytes(), &updated); err != nil {
t.Fatalf("decode updated user: %v", err)
}
if updated.Name != "after" {
t.Fatalf("expected updated name %q, got %q", "after", updated.Name)
}
}
Loading