-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serve-http): add ServeHTTP func for server
- Loading branch information
1 parent
8c24c5b
commit 292aaba
Showing
2 changed files
with
89 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,30 @@ | ||
package govalin | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type ServeHTTPFunc func(w http.ResponseWriter, r *http.Request) | ||
|
||
// HttpServe registers a ServeHttpFunc to a path which adheres to the http.Handler interface. | ||
func (server *App) HTTPServe(path string, httpServeFunc ServeHTTPFunc) { | ||
fullPath := server.currentFragment + path | ||
handler := server.getOrCreatePathHandlerByPath(fullPath) | ||
|
||
handlerFunc := func(call *Call) { | ||
call.bypassLifecycle = true | ||
httpServeFunc(*call.Raw.W, call.Raw.Req) | ||
} | ||
|
||
handler.Get = handlerFunc | ||
handler.Post = handlerFunc | ||
handler.Put = handlerFunc | ||
handler.Patch = handlerFunc | ||
handler.Delete = handlerFunc | ||
handler.Options = handlerFunc | ||
handler.Head = handlerFunc | ||
|
||
for _, onRouteAdded := range server.config.server.events.onRouteAdded { | ||
onRouteAdded("ServeHTTP", fullPath, handlerFunc) | ||
} | ||
} |
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,59 @@ | ||
package govalin_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/pkkummermo/govalin" | ||
"github.com/pkkummermo/govalin/internal/govalintesting" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestServeHTTP(t *testing.T) { | ||
govalintesting.HTTPTestUtil(func(app *govalin.App) *govalin.App { | ||
app.HTTPServe("/httpserve", func(w http.ResponseWriter, _ *http.Request) { | ||
_, err := w.Write([]byte("httpservegovalin")) | ||
w.WriteHeader(http.StatusOK) | ||
assert.Nil(t, err, "Should write to response writer") | ||
}) | ||
|
||
return app | ||
}, func(http govalintesting.GovalinHTTP) { | ||
assert.Equal( | ||
t, | ||
"httpservegovalin", | ||
http.Get("/httpserve", nil), | ||
"Should create httpserve GET endpoint", | ||
) | ||
assert.Equal( | ||
t, | ||
"httpservegovalin", | ||
http.Post("/httpserve", nil), | ||
"Should create httpserve POST endpoint", | ||
) | ||
assert.Equal( | ||
t, | ||
"httpservegovalin", | ||
http.Patch("/httpserve", nil), | ||
"Should create httpserve PATCH endpoint", | ||
) | ||
assert.Equal( | ||
t, | ||
"httpservegovalin", | ||
http.Delete("/httpserve", nil), | ||
"Should create httpserve DELETE endpoint", | ||
) | ||
assert.Equal( | ||
t, | ||
"httpservegovalin", | ||
http.Options("/httpserve", nil), | ||
"Should create httpserve OPTIONS endpoint", | ||
) | ||
assert.Equal( | ||
t, | ||
"", | ||
http.Head("/httpserve"), | ||
"Should create httpserve HEAD endpoint", | ||
) | ||
}) | ||
} |