-
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
451ed88
commit 19c3b65
Showing
5 changed files
with
172 additions
and
6 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,33 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/ChristophBe/go-crud/types" | ||
"net/http" | ||
) | ||
|
||
// NewFunctionHandler creates a http.Handler that handles the creation of a model | ||
func NewFunctionHandler(service types.FunctionHandlerService, responseWriter types.ResponseWriter, errorWriter types.ErrorResponseWriter) http.HandlerFunc { | ||
return func(writer http.ResponseWriter, request *http.Request) { | ||
ctx := request.Context() | ||
dto, err := service.ParseValidatableFromRequest(request) | ||
if err != nil { | ||
errorWriter(err, writer, request) | ||
return | ||
} | ||
|
||
if err = dto.IsValid(ctx, false); err != nil { | ||
errorWriter(err, writer, request) | ||
return | ||
} | ||
|
||
result, status, err := service.Function(ctx, dto) | ||
if err != nil { | ||
errorWriter(err, writer, request) | ||
return | ||
} | ||
|
||
if err = responseWriter(result, status, writer, request); err != nil { | ||
errorWriter(err, writer, request) | ||
} | ||
} | ||
} |
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,105 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCrudHandlersImpl_Function(t *testing.T) { | ||
|
||
tt := []struct { | ||
name string | ||
service functionServiceMock | ||
responseWriterError error | ||
expectedError error | ||
}{ | ||
{ | ||
name: "dto parsing failed", | ||
service: functionServiceMock{ | ||
dtoErr: errors.New("test"), | ||
}, | ||
expectedError: errors.New("test"), | ||
}, | ||
{ | ||
name: "dto invalid", | ||
service: functionServiceMock{ | ||
dto: dtoMock{ | ||
validationError: errors.New("test"), | ||
}, | ||
}, | ||
expectedError: errors.New("test"), | ||
}, | ||
{ | ||
name: "function returns error invalid", | ||
service: functionServiceMock{ | ||
dto: dtoMock{}, | ||
functionErr: errors.New("test"), | ||
}, | ||
expectedError: errors.New("test"), | ||
}, | ||
{ | ||
name: "function succeeded invalid", | ||
service: functionServiceMock{ | ||
dto: dtoMock{}, | ||
responseStatus: http.StatusOK, | ||
}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "response writer returns error", | ||
service: functionServiceMock{ | ||
dto: dtoMock{}, | ||
responseStatus: http.StatusOK, | ||
}, | ||
responseWriterError: errors.New("test-error"), | ||
expectedError: errors.New("test-error"), | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
responseRecorder := new(responseWriterRecorder) | ||
errorRecorder := new(errorWriterRecorder) | ||
|
||
responseWriter := newMockResponseWriter(responseRecorder, tc.responseWriterError) | ||
|
||
errorWriter := newMockErrorWriter(errorRecorder) | ||
|
||
handler := NewFunctionHandler(tc.service, responseWriter, errorWriter) | ||
w := httptest.ResponseRecorder{} | ||
handler.ServeHTTP(&w, new(http.Request)) | ||
|
||
if tc.expectedError != nil { | ||
|
||
// expect error writer to be called | ||
if errorRecorder.err == nil { | ||
t.Error("error to be not nil") | ||
return | ||
} | ||
if errorRecorder.err.Error() != tc.expectedError.Error() { | ||
t.Errorf("expected err to be %v, got %v", tc.expectedError, errorRecorder.err) | ||
} | ||
return | ||
} | ||
if tc.expectedError == nil { | ||
// expect response writer to be called | ||
|
||
if responseRecorder.status != tc.service.responseStatus { | ||
t.Errorf("expected response status to be %v, got %v", tc.service.responseStatus, responseRecorder.status) | ||
} | ||
if responseRecorder.body != nil { | ||
t.Errorf("expected response body to be nil, got %v", responseRecorder.body) | ||
} | ||
|
||
} else { | ||
// expect response not to called | ||
if responseRecorder.called { | ||
t.Error("expected response writer not to be called") | ||
} | ||
} | ||
}) | ||
} | ||
} |
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