Skip to content

Commit abe6f67

Browse files
committed
feat: add simple http example
0 parents  commit abe6f67

5 files changed

Lines changed: 193 additions & 0 deletions

File tree

README.md

Whitespace-only changes.

go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module example
2+
3+
go 1.23.0
4+
5+
toolchain go1.23.10
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/labstack/echo/v4 v4.13.4 // indirect
10+
github.com/labstack/gommon v0.4.2 // indirect
11+
github.com/mattn/go-colorable v0.1.14 // indirect
12+
github.com/mattn/go-isatty v0.0.20 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
github.com/stretchr/testify v1.10.0 // indirect
15+
github.com/valyala/bytebufferpool v1.0.0 // indirect
16+
github.com/valyala/fasttemplate v1.2.2 // indirect
17+
golang.org/x/crypto v0.38.0 // indirect
18+
golang.org/x/net v0.40.0 // indirect
19+
golang.org/x/sys v0.33.0 // indirect
20+
golang.org/x/text v0.25.0 // indirect
21+
gopkg.in/yaml.v3 v3.0.1 // indirect
22+
)

go.sum

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
4+
github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ=
5+
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
6+
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
7+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
8+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
9+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
10+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
14+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
15+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
16+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
17+
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
18+
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
19+
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
20+
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
21+
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
22+
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
23+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
25+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
26+
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
27+
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
28+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
29+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
30+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo/v4"
7+
)
8+
9+
type User struct {
10+
ID string `param:"id"`
11+
FirstName string `json:"first_name"`
12+
LastName string `json:"last_name"`
13+
}
14+
15+
func main() {
16+
userDB := make(map[string]*User)
17+
e := echo.New()
18+
e.PUT("/users/:id", updateUser(userDB))
19+
e.Logger.Fatal(e.Start(":8080"))
20+
}
21+
22+
func updateUser(userDB map[string]*User) echo.HandlerFunc {
23+
return func(c echo.Context) error {
24+
var user User
25+
err := c.Bind(&user)
26+
if err != nil {
27+
return c.NoContent(http.StatusBadRequest)
28+
}
29+
dbUser, ok := userDB[user.ID]
30+
if !ok {
31+
return c.NoContent(http.StatusNotFound)
32+
}
33+
34+
dbUser.FirstName = user.FirstName
35+
dbUser.LastName = user.LastName
36+
37+
return c.JSON(http.StatusOK, dbUser)
38+
}
39+
}

main_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"github.com/labstack/echo/v4"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
var ()
15+
16+
func TestUpdateUser(t *testing.T) {
17+
18+
t.Run("should update user when user exists", func(t *testing.T) {
19+
20+
mockDB := map[string]*User{
21+
"test_user_id": &User{
22+
ID: "test_user_id",
23+
FirstName: "Test",
24+
LastName: "User",
25+
},
26+
}
27+
userJSON := `{"first_name":"UpdatedTest", "last_name":"UpdatedUser"}`
28+
29+
e := echo.New()
30+
req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(userJSON))
31+
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
32+
rec := httptest.NewRecorder()
33+
c := e.NewContext(req, rec)
34+
c.SetPath("/users/:id")
35+
c.SetParamNames("id")
36+
c.SetParamValues("test_user_id")
37+
h := updateUser(mockDB)
38+
39+
if assert.NoError(t, h(c)) {
40+
assert.Equal(t, http.StatusOK, rec.Code)
41+
var response User
42+
if !assert.NoError(t, json.Unmarshal(rec.Body.Bytes(), &response), "unable to unmarshal body to user") {
43+
t.FailNow()
44+
}
45+
assert.Equal(t, mockDB["test_user_id"], &response)
46+
assert.Equal(t, "UpdatedTest", mockDB["test_user_id"].FirstName)
47+
assert.Equal(t, "UpdatedUser", mockDB["test_user_id"].LastName)
48+
}
49+
})
50+
51+
t.Run("should return 404 when user not found", func(t *testing.T) {
52+
53+
mockDB := map[string]*User{
54+
"test_user_id": &User{
55+
ID: "test_user_id",
56+
FirstName: "Test",
57+
LastName: "User",
58+
},
59+
}
60+
userJSON := `{"first_name":"UpdatedTest", "last_name":"UpdatedUser"}`
61+
62+
e := echo.New()
63+
req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(userJSON))
64+
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
65+
rec := httptest.NewRecorder()
66+
c := e.NewContext(req, rec)
67+
c.SetPath("/users/:id")
68+
c.SetParamNames("id")
69+
c.SetParamValues("bad_user_id")
70+
h := updateUser(mockDB)
71+
72+
if assert.NoError(t, h(c)) {
73+
assert.Equal(t, http.StatusNotFound, rec.Code)
74+
}
75+
})
76+
77+
t.Run("should return 400 when request is invalid", func(t *testing.T) {
78+
79+
mockDB := map[string]*User{
80+
"test_user_id": &User{
81+
ID: "test_user_id",
82+
FirstName: "Test",
83+
LastName: "User",
84+
},
85+
}
86+
userJSON := `{"first_name":"UpdatedTest", "last_name":300}`
87+
88+
e := echo.New()
89+
req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(userJSON))
90+
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
91+
rec := httptest.NewRecorder()
92+
c := e.NewContext(req, rec)
93+
c.SetPath("/users/:id")
94+
c.SetParamNames("id")
95+
c.SetParamValues("bad_user_id")
96+
h := updateUser(mockDB)
97+
98+
if assert.NoError(t, h(c)) {
99+
assert.Equal(t, http.StatusBadRequest, rec.Code)
100+
}
101+
})
102+
}

0 commit comments

Comments
 (0)