-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
18 changed files
with
158 additions
and
64 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,31 @@ | ||
package contracts | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
type Request = http.Request | ||
|
||
type Handler interface { | ||
ServeHTTP(*ResponseWriter, *Request) | ||
} | ||
|
||
type HandlerFunc func(*ResponseWriter, *Request) | ||
|
||
func (f HandlerFunc) ServeHTTP(w *ResponseWriter, r *Request) { | ||
f(w, r) | ||
} | ||
|
||
var ErrResponceNotCasted = errors.New("received incorrect response writer type") | ||
|
||
func CastToHTTPHandler(handler Handler) http.Handler { | ||
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
writer, ok := response.(*ResponseWriter) | ||
if !ok { | ||
panic(ErrResponceNotCasted) | ||
} | ||
|
||
handler.ServeHTTP(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,42 @@ | ||
package contracts_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/internal/contracts" | ||
"github.com/evg4b/uncors/internal/sfmt" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCastToHTTPHandler(t *testing.T) { | ||
const expectedBody = `{ "OK": true }` | ||
uncorsHandler := contracts.HandlerFunc(func(w *contracts.ResponseWriter, r *contracts.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
sfmt.Fprint(w, expectedBody) | ||
}) | ||
|
||
httpHandler := contracts.CastToHTTPHandler(uncorsHandler) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/data", nil) | ||
|
||
t.Run("cast correctly", func(t *testing.T) { | ||
recirder := httptest.NewRecorder() | ||
responceWriter := contracts.WrapResponseWriter(recirder) | ||
|
||
assert.NotPanics(t, func() { | ||
httpHandler.ServeHTTP(responceWriter, req) | ||
assert.Equal(t, expectedBody, testutils.ReadBody(t, recirder)) | ||
}) | ||
}) | ||
|
||
t.Run("panit when request is not wrapped", func(t *testing.T) { | ||
recirder := httptest.NewRecorder() | ||
|
||
assert.PanicsWithValue(t, contracts.ErrResponceNotCasted, func() { | ||
httpHandler.ServeHTTP(recirder, req) | ||
}) | ||
}) | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/handler/static/response_writer.go → internal/contracts/response_writer.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package static | ||
package contracts | ||
|
||
import ( | ||
"net/http" | ||
|
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
File renamed without changes.
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
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
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
Oops, something went wrong.