Skip to content

Commit

Permalink
feat: reorder args in HandleAuthenticatedRequestWithBody
Browse files Browse the repository at this point in the history
  • Loading branch information
trakhimenok committed May 27, 2024
1 parent 08e64b4 commit fd02c3d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
23 changes: 11 additions & 12 deletions apicore/put_post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import (
"net/http"
)

// FacadeHandler TODO:?
type FacadeHandler = func(
ctx context.Context,
userCtx facade.User,
) (response interface{}, err error)
// FacadeHandler defines a function that handles a request
type FacadeHandler = func(ctx context.Context, userCtx facade.User) (response any, err error)

// HandleAuthenticatedRequestWithBody is very similar to Execute // TODO: consider code unification & reuse
func HandleAuthenticatedRequestWithBody(w http.ResponseWriter, r *http.Request,
// HandleAuthenticatedRequestWithBody is very similar to Execute - consider code unification & reuse
func HandleAuthenticatedRequestWithBody(
w http.ResponseWriter,
r *http.Request,
request interface{ Validate() error },
facadeHandler FacadeHandler,
successStatusCode int,
options verify.RequestOptions,
successStatusCode int,
facadeHandler FacadeHandler,
) {
ctx, userContext, err := VerifyAuthenticatedRequestAndDecodeBody(w, r, options, request)
if err != nil { // The error has been handled inside the function
return
if err != nil {
return // No need to write to response as the error has been handled inside the called function
}
var response interface{}
var response any
response, err = facadeHandler(ctx, userContext)
ReturnJSON(ctx, w, r, successStatusCode, err, response)
}
50 changes: 50 additions & 0 deletions apicore/put_post_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package apicore

import (
"context"
"github.com/sneat-co/sneat-go-core/apicore/verify"
"github.com/sneat-co/sneat-go-core/facade"
"github.com/sneat-co/sneat-go-core/sneatauth"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

func TestHandleAuthenticatedRequestWithBody(t *testing.T) {
type args struct {
r *http.Request
request interface{ Validate() error }
options verify.RequestOptions
successStatusCode int
facadeHandler FacadeHandler
}

GetAuthTokenFromHttpRequest = func(r *http.Request) (token *sneatauth.Token, err error) {
return nil, nil
}

tests := []struct {
name string
args args
}{
{
name: "TestHandleAuthenticatedRequestWithBody",
args: args{
r: httptest.NewRequest(http.MethodGet, "/", nil),
options: verify.Request(),
successStatusCode: http.StatusMethodNotAllowed, // TODO: make it working using http.StatusNoContent
facadeHandler: func(ctx context.Context, userCtx facade.User) (response any, err error) {
return nil, nil
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := new(httptest.ResponseRecorder)
HandleAuthenticatedRequestWithBody(w, tt.args.r, tt.args.request, tt.args.options, tt.args.successStatusCode, tt.args.facadeHandler)
assert.Equal(t, tt.args.successStatusCode, w.Code)
})
}
}

0 comments on commit fd02c3d

Please sign in to comment.