Skip to content

Commit 2558dd2

Browse files
author
Hasan Demir
committed
Fix user create endpoint
1 parent 323cbca commit 2558dd2

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Diff for: controllers/v1/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (u *User) Create(c *gin.Context) {
5050
return
5151
}
5252

53-
c.JSON(http.StatusInternalServerError, gin.H{
53+
c.JSON(http.StatusCreated, gin.H{
5454
"data": user,
5555
})
5656
}

Diff for: tests/controllers/controller_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestMain(m *testing.M) {
2222
os.Exit(exitVal)
2323
}
2424

25+
// jsonReaderFactory creates a json reader from struct
2526
func jsonReaderFactory(in interface{}) (io.Reader) {
2627
buf := bytes.NewBuffer(nil)
2728
enc := json.NewEncoder(buf)

Diff for: tests/controllers/user_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ import (
1111

1212
func TestCreateUser(t *testing.T) {
1313
w := httptest.NewRecorder()
14-
req, _ := http.NewRequest("POST", "/api/v1/user", interface{
15-
"name": "test",
16-
})
14+
15+
body := struct {
16+
Name string
17+
Email string
18+
Password string
19+
}{
20+
Name: "name",
21+
22+
Password: "secret",
23+
}
24+
25+
req, _ := http.NewRequest("POST", "/api/v1/user", jsonReaderFactory(body))
1726
router.ServeHTTP(w, req)
18-
assert.Equal(t, 200, w.Code)
27+
assert.Equal(t, 201, w.Code)
1928
}

0 commit comments

Comments
 (0)