Skip to content

Commit

Permalink
Merge pull request #39 from linkernetworks/hwchiu/minor-fix
Browse files Browse the repository at this point in the history
Add the testing for invalid page and unexposed the restful handler
  • Loading branch information
Hung-Wei Chiu authored Jun 25, 2018
2 parents 004f168 + 2deaaaf commit 057ca64
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/server/handler_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"gopkg.in/mgo.v2/bson"
)

func CreateNetworkHandler(ctx *web.Context) {
func createNetworkHandler(ctx *web.Context) {
as, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

network := entity.Network{}
Expand Down Expand Up @@ -59,7 +59,7 @@ func CreateNetworkHandler(ctx *web.Context) {
})
}

func ListNetworkHandler(ctx *web.Context) {
func listNetworkHandler(ctx *web.Context) {
as, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

var pageSize = 10
Expand Down Expand Up @@ -105,7 +105,7 @@ func ListNetworkHandler(ctx *web.Context) {
resp.WriteEntity(networks)
}

func GetNetworkHandler(ctx *web.Context) {
func getNetworkHandler(ctx *web.Context) {
as, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

id := req.PathParameter("id")
Expand All @@ -126,7 +126,7 @@ func GetNetworkHandler(ctx *web.Context) {
resp.WriteEntity(network)
}

func DeleteNetworkHandler(ctx *web.Context) {
func deleteNetworkHandler(ctx *web.Context) {
as, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

id := req.PathParameter("id")
Expand Down
9 changes: 9 additions & 0 deletions src/server/handler_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,13 @@ func TestListNetworkWithInvalidPage(t *testing.T) {
service = newNetworkService(sp)
wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(t, http.StatusBadRequest, httpWriter)

httpRequest, err = http.NewRequest("GET", "http://localhost:7890/v1/networks?page=-1", nil)
assert.NoError(t, err)

httpWriter = httptest.NewRecorder()
service = newNetworkService(sp)
wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(t, http.StatusInternalServerError, httpWriter)

}
4 changes: 2 additions & 2 deletions src/server/handler_storage_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"gopkg.in/mgo.v2/bson"
)

func CreateStorageProvider(ctx *web.Context) {
func createStorageProvider(ctx *web.Context) {
sp, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

storageProvider := entity.StorageProvider{}
Expand Down Expand Up @@ -49,7 +49,7 @@ func CreateStorageProvider(ctx *web.Context) {
})
}

func ListStorageProvider(ctx *web.Context) {
func listStorageProvider(ctx *web.Context) {
sp, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

var pageSize = 10
Expand Down
12 changes: 6 additions & 6 deletions src/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ func newVersionService(sp *serviceprovider.Container) *restful.WebService {
func newNetworkService(sp *serviceprovider.Container) *restful.WebService {
webService := new(restful.WebService)
webService.Path("/v1/networks").Consumes(restful.MIME_JSON, restful.MIME_JSON).Produces(restful.MIME_JSON, restful.MIME_JSON)
webService.Route(webService.GET("/").To(handler.RESTfulServiceHandler(sp, ListNetworkHandler)))
webService.Route(webService.GET("/{id}").To(handler.RESTfulServiceHandler(sp, GetNetworkHandler)))
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, CreateNetworkHandler)))
webService.Route(webService.DELETE("/{id}").To(handler.RESTfulServiceHandler(sp, DeleteNetworkHandler)))
webService.Route(webService.GET("/").To(handler.RESTfulServiceHandler(sp, listNetworkHandler)))
webService.Route(webService.GET("/{id}").To(handler.RESTfulServiceHandler(sp, getNetworkHandler)))
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, createNetworkHandler)))
webService.Route(webService.DELETE("/{id}").To(handler.RESTfulServiceHandler(sp, deleteNetworkHandler)))
return webService
}

func newStorageProviderService(sp *serviceprovider.Container) *restful.WebService {
webService := new(restful.WebService)
webService.Path("/v1/storageprovider").Consumes(restful.MIME_JSON, restful.MIME_JSON).Produces(restful.MIME_JSON, restful.MIME_JSON)
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, CreateStorageProvider)))
webService.Route(webService.GET("/").To(handler.RESTfulServiceHandler(sp, ListStorageProvider)))
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, createStorageProvider)))
webService.Route(webService.GET("/").To(handler.RESTfulServiceHandler(sp, listStorageProvider)))
return webService
}

Expand Down

0 comments on commit 057ca64

Please sign in to comment.