-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c22bc58
commit b98aa8b
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/claudealdric/go-todolist-restful-api-server/models" | ||
) | ||
|
||
func (s *Server) HandlePostUser(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("content-type", jsonContentType) | ||
var dto models.CreateUserDTO | ||
json.NewDecoder(r.Body).Decode(&dto) | ||
user, _ := s.store.CreateUser(dto) | ||
w.WriteHeader(http.StatusCreated) | ||
json.NewEncoder(w).Encode(user) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/claudealdric/go-todolist-restful-api-server/models" | ||
"github.com/claudealdric/go-todolist-restful-api-server/testutils" | ||
"github.com/claudealdric/go-todolist-restful-api-server/testutils/assert" | ||
) | ||
|
||
func TestHandlePostUser(t *testing.T) { | ||
t.Run("creates and returns the user with a 201 Status Created", func(t *testing.T) { | ||
data := testutils.NewMockStore(false) | ||
server := NewServer(data) | ||
|
||
dto := models.CreateUserDTO{ | ||
Name: "Claude Aldric", | ||
Email: "[email protected]", | ||
Password: "password", | ||
} | ||
jsonData, err := json.Marshal(dto) | ||
assert.HasNoError(t, err) | ||
request := httptest.NewRequest( | ||
http.MethodPost, | ||
"/users", | ||
bytes.NewBuffer(jsonData), | ||
) | ||
response := httptest.NewRecorder() | ||
server.Handler.ServeHTTP(response, request) | ||
newUser := models.NewUser(1, dto.Name, dto.Email, dto.Password) | ||
|
||
assert.ContentType( | ||
t, | ||
testutils.GetContentTypeFromResponse(response), | ||
jsonContentType, | ||
) | ||
assert.Status(t, response.Code, http.StatusCreated) | ||
assert.Calls(t, data.CreateUserCalls, 1) | ||
assert.Equals( | ||
t, | ||
testutils.GetUserFromResponse(t, response.Body), | ||
*newUser, | ||
) | ||
}) | ||
|
||
// t.Run("responds with a 400 Bad Request given an invalid body", func(t *testing.T) { | ||
// data := testutils.NewMockStore(false) | ||
// server := NewServer(data) | ||
// | ||
// invalidJson := `{` | ||
// request := httptest.NewRequest( | ||
// http.MethodPost, | ||
// "/users", | ||
// bytes.NewBuffer([]byte(invalidJson)), | ||
// ) | ||
// response := httptest.NewRecorder() | ||
// server.Handler.ServeHTTP(response, request) | ||
// | ||
// assert.Status(t, response.Code, http.StatusBadRequest) | ||
// assert.Calls(t, data.CreateUserCalls, 0) | ||
// }) | ||
// | ||
// t.Run("responds with a 500 error when the store user creation fails", func(t *testing.T) { | ||
// data := testutils.NewMockStore(true) | ||
// server := NewServer(data) | ||
// | ||
// newUser := models.NewUser(2, "Exercise") | ||
// jsonData, err := json.Marshal(newUser) | ||
// assert.HasNoError(t, err) | ||
// request := httptest.NewRequest( | ||
// http.MethodPost, | ||
// "/users", | ||
// bytes.NewBuffer(jsonData), | ||
// ) | ||
// response := httptest.NewRecorder() | ||
// server.Handler.ServeHTTP(response, request) | ||
// | ||
// assert.Status(t, response.Code, http.StatusInternalServerError) | ||
// assert.Calls(t, data.CreateUserCalls, 1) | ||
// }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters