Skip to content

Commit

Permalink
Merge branch 'development' into fix-nav-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vipul-rawat authored Apr 10, 2024
2 parents 025df97 + c59a2df commit 064e315
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/http-server-using-redis/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestHTTPServerUsingRedis(t *testing.T) {
statusCode int
}{
{"post handler", http.MethodPost, []byte(`{"key1":"GoFr"}`), "/redis",
http.StatusOK},
http.StatusCreated},
{"post invalid body", http.MethodPost, []byte(`{key:abc}`), "/redis",
http.StatusInternalServerError},
{"get handler", http.MethodGet, nil, "/redis/key1", http.StatusOK},
Expand Down
4 changes: 2 additions & 2 deletions examples/using-add-rest-handlers/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func TestIntegration_AddRESTHandlers(t *testing.T) {
}{
{"empty path", http.MethodGet, "/", nil, 404},
{"success Create", http.MethodPost, "/user",
[]byte(`{"id":10, "name":"john doe", "age":99, "isEmployed": true}`), 200},
[]byte(`{"id":10, "name":"john doe", "age":99, "isEmployed": true}`), 201},
{"success GetAll", http.MethodGet, "/user", nil, 200},
{"success Get", http.MethodGet, "/user/10", nil, 200},
{"success Update", http.MethodPut, "/user/10",
[]byte(`{"name":"john doe", "age":99, "isEmployed": false}`), 200},
{"success Delete", http.MethodDelete, "/user/10", nil, 200},
{"success Delete", http.MethodDelete, "/user/10", nil, 204},
}

for i, tc := range tests {
Expand Down
2 changes: 1 addition & 1 deletion examples/using-file-bind/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMain_BindError(t *testing.T) {
req.Header.Set("content-type", contentType)

resp, err = c.Do(req)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, 201, resp.StatusCode)
}

func generateMultiPartBody(t *testing.T) (*bytes.Buffer, string) {
Expand Down
2 changes: 1 addition & 1 deletion examples/using-migrations/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestExampleMigration(t *testing.T) {
statusCode int
}{
{"post new employee with valid data", http.MethodPost, "/employee",
[]byte(`{"id":2,"name":"John","gender":"Male","contact_number":1234567890,"dob":"2000-01-01"}`), 200},
[]byte(`{"id":2,"name":"John","gender":"Male","contact_number":1234567890,"dob":"2000-01-01"}`), 201},
{"get employee with valid name", http.MethodGet, "/employee?name=John", nil, 200},
{"get employee does not exist", http.MethodGet, "/employee?name=Invalid", nil, 500},
{"get employee with empty name", http.MethodGet, "/employee", nil, http.StatusInternalServerError},
Expand Down
4 changes: 2 additions & 2 deletions examples/using-publisher/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestExamplePublisher(t *testing.T) {
desc: "valid order",
path: "/publish-order",
body: []byte(`{"data":{"orderId":"123","status":"pending"}}`),
expectedStatusCode: http.StatusOK,
expectedStatusCode: http.StatusCreated,
},
{
desc: "invalid order",
Expand All @@ -37,7 +37,7 @@ func TestExamplePublisher(t *testing.T) {
desc: "valid product",
path: "/publish-product",
body: []byte(`{"data":{"productId":"123","price":"599"}}`),
expectedStatusCode: http.StatusOK,
expectedStatusCode: http.StatusCreated,
},
{
desc: "invalid product",
Expand Down
2 changes: 1 addition & 1 deletion pkg/gofr/crud_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func createTestContext(method, path, id string, body []byte, cont *container.Con
testReq.Header.Set("Content-Type", "application/json")
gofrReq := gofrHTTP.NewRequest(testReq)

return newContext(gofrHTTP.NewResponder(httptest.NewRecorder()), gofrReq, cont)
return newContext(gofrHTTP.NewResponder(httptest.NewRecorder(), method), gofrReq, cont)
}

func Test_scanEntity(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gofr/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type handler struct {
}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := newContext(gofrHTTP.NewResponder(w), gofrHTTP.NewRequest(r), h.container)
c := newContext(gofrHTTP.NewResponder(w, r.Method), gofrHTTP.NewRequest(r), h.container)
defer c.Trace("gofr-handler").End()
c.responder.Respond(h.function(c))
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/gofr/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ var (
func TestHandler_ServeHTTP(t *testing.T) {
testCases := []struct {
desc string
method string
data interface{}
err error
statusCode int
body string
}{
{"data is nil and error is nil", nil, nil, http.StatusOK},
{"data is mil, error is not nil", nil, errTest, http.StatusInternalServerError},
{"method is get, data is nil and error is nil", http.MethodGet, nil, nil, http.StatusOK,
`{}`},
{"method is get, data is mil, error is not nil", http.MethodGet, nil, errTest, http.StatusInternalServerError,
`{"error":{"message":"some error"}}`},
{"method is post, data is nil and error is nil", http.MethodPost, "Created", nil, http.StatusCreated,
`{"data":"Created"}`},
{"method is delete, data is nil and error is nil", http.MethodDelete, nil, nil, http.StatusNoContent,
`{}`},
}

for i, tc := range testCases {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
r := httptest.NewRequest(tc.method, "/", http.NoBody)
c := &container.Container{
Logger: logging.NewLogger(logging.FATAL),
}
Expand All @@ -46,7 +54,8 @@ func TestHandler_ServeHTTP(t *testing.T) {
container: c,
}.ServeHTTP(w, r)

assert.Equal(t, w.Code, tc.statusCode, "TEST[%d], Failed.\n%s", i, tc.desc)
assert.Containsf(t, w.Body.String(), tc.body, "TEST[%d], Failed.\n%s", i, tc.desc)
assert.Equal(t, tc.statusCode, w.Code, "TEST[%d], Failed.\n%s", i, tc.desc)
}
}

Expand Down
16 changes: 12 additions & 4 deletions pkg/gofr/http/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
)

// NewResponder creates a new Responder instance from the given http.ResponseWriter..
func NewResponder(w http.ResponseWriter) *Responder {
return &Responder{w: w}
func NewResponder(w http.ResponseWriter, method string) *Responder {
return &Responder{w: w, method: method}
}

// Responder encapsulates an http.ResponseWriter and is responsible for crafting structured responses.
type Responder struct {
w http.ResponseWriter
w http.ResponseWriter
method string
}

// Respond sends a response with the given data and handles potential errors, setting appropriate
Expand Down Expand Up @@ -51,7 +52,14 @@ func (r Responder) Respond(data interface{}, err error) {
// HTTPStatusFromError maps errors to HTTP status codes.
func (r Responder) HTTPStatusFromError(err error) (status int, errObj interface{}) {
if err == nil {
return http.StatusOK, nil
switch r.method {
case http.MethodPost:
return http.StatusCreated, nil
case http.MethodDelete:
return http.StatusNoContent, nil
default:
return http.StatusOK, nil
}
}

if errors.Is(err, http.ErrMissingFile) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gofr/http/responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestResponder_Respond(t *testing.T) {
r := NewResponder(httptest.NewRecorder())
r := NewResponder(httptest.NewRecorder(), http.MethodGet)

tests := []struct {
desc string
Expand All @@ -32,7 +32,7 @@ func TestResponder_Respond(t *testing.T) {
}

func TestResponder_HTTPStatusFromError(t *testing.T) {
r := NewResponder(httptest.NewRecorder())
r := NewResponder(httptest.NewRecorder(), http.MethodGet)

tests := []struct {
desc string
Expand Down

0 comments on commit 064e315

Please sign in to comment.