mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Merge pull request #53 from pedronasser/fix-48
add wrapper to apps and routes responses
This commit is contained in:
@@ -21,9 +21,9 @@ var (
|
||||
type Routes []*Route
|
||||
|
||||
type Route struct {
|
||||
AppName string `json:"appname"`
|
||||
Path string `json:"path"`
|
||||
Image string `json:"image"`
|
||||
AppName string `json:"appname,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Image string `json:"image,omitempty"`
|
||||
Headers http.Header `json:"headers,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@ func handleAppCreate(c *gin.Context) {
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := titancommon.Logger(ctx)
|
||||
|
||||
wapp := &models.AppWrapper{}
|
||||
var wapp models.AppWrapper
|
||||
|
||||
err := c.BindJSON(wapp)
|
||||
err := c.BindJSON(&wapp)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||
@@ -42,7 +42,7 @@ func handleAppCreate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
app, err := Api.Datastore.StoreApp(wapp.App)
|
||||
_, err = Api.Datastore.StoreApp(wapp.App)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorln(models.ErrAppsCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||
@@ -56,5 +56,5 @@ func handleAppCreate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, app)
|
||||
c.JSON(http.StatusCreated, appResponse{"App successfully created", wapp})
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestAppCreate(t *testing.T) {
|
||||
{"/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
|
||||
|
||||
// success
|
||||
{"/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil},
|
||||
{"/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusCreated, nil},
|
||||
} {
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
_, rec := routerRequest(t, router, "POST", test.path, body)
|
||||
|
||||
@@ -15,6 +15,26 @@ import (
|
||||
titancommon "github.com/iron-io/titan/common"
|
||||
)
|
||||
|
||||
type appResponse struct {
|
||||
Message string
|
||||
App models.AppWrapper
|
||||
}
|
||||
|
||||
type appsResponse struct {
|
||||
Message string
|
||||
Apps models.AppsWrapper
|
||||
}
|
||||
|
||||
type routeResponse struct {
|
||||
Message string
|
||||
Route models.RouteWrapper
|
||||
}
|
||||
|
||||
type routesResponse struct {
|
||||
Message string
|
||||
Routes models.RoutesWrapper
|
||||
}
|
||||
|
||||
func testRouter() *gin.Engine {
|
||||
r := gin.Default()
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -59,12 +59,12 @@ func handleRouteCreate(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
route, err := Api.Datastore.StoreRoute(wroute.Route)
|
||||
_, err = Api.Datastore.StoreRoute(wroute.Route)
|
||||
if err != nil {
|
||||
log.WithError(err).Error(models.ErrRoutesCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, route)
|
||||
c.JSON(http.StatusCreated, routeResponse{"Route successfully created", wroute})
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestRouteCreate(t *testing.T) {
|
||||
{"/v1/apps/$/routes", `{ "route": { "image": "iron/hello", "path": "/myroute" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
|
||||
|
||||
// success
|
||||
{"/v1/apps/a/routes", `{ "route": { "name": "myroute", "image": "iron/hello", "path": "/myroute" } }`, http.StatusOK, nil},
|
||||
{"/v1/apps/a/routes", `{ "route": { "name": "myroute", "image": "iron/hello", "path": "/myroute" } }`, http.StatusCreated, nil},
|
||||
} {
|
||||
body := bytes.NewBuffer([]byte(test.body))
|
||||
_, rec := routerRequest(t, router, "POST", test.path, body)
|
||||
|
||||
@@ -38,12 +38,12 @@ func handleRouteUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
route, err := Api.Datastore.StoreRoute(wroute.Route)
|
||||
_, err = Api.Datastore.StoreRoute(wroute.Route)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrAppsCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, route)
|
||||
c.JSON(http.StatusOK, routeResponse{"Route successfully updated", wroute})
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ func TestFullStack(t *testing.T) {
|
||||
body string
|
||||
expectedCode int
|
||||
}{
|
||||
{"POST", "/v1/apps", `{ "app": { "name": "myapp" } }`, http.StatusOK},
|
||||
{"POST", "/v1/apps", `{ "app": { "name": "myapp" } }`, http.StatusCreated},
|
||||
{"GET", "/v1/apps", ``, http.StatusOK},
|
||||
{"GET", "/v1/apps/myapp", ``, http.StatusOK},
|
||||
{"POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute", "path": "/myroute", "image": "iron/hello" } }`, http.StatusOK},
|
||||
{"POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute2", "path": "/myroute2", "image": "iron/error" } }`, http.StatusOK},
|
||||
{"POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute", "path": "/myroute", "image": "iron/hello" } }`, http.StatusCreated},
|
||||
{"POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute2", "path": "/myroute2", "image": "iron/error" } }`, http.StatusCreated},
|
||||
{"GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK},
|
||||
{"GET", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK},
|
||||
{"GET", "/v1/apps/myapp/routes", ``, http.StatusOK},
|
||||
|
||||
Reference in New Issue
Block a user