forked from jakecoffman/crud
-
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.
break swagger out, simplify API, writing tests
- Loading branch information
1 parent
ce1660c
commit cefbd42
Showing
7 changed files
with
386 additions
and
211 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 |
---|---|---|
|
@@ -22,3 +22,5 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
|
||
swagger.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package crud | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// this is where the validation happens! | ||
func preHandler(spec Spec) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
val := spec.Validate | ||
if val.Query != nil { | ||
values := c.Request.URL.Query() | ||
for field, schema := range val.Query { | ||
// query values are always strings, so we must try to convert | ||
queryValue := values.Get(field) | ||
|
||
// don't try to convert if the field is empty | ||
if queryValue == "" { | ||
if schema.required != nil && *schema.required { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Query validation failed for field %v: %v", field, ErrRequired)) | ||
} | ||
return | ||
} | ||
var convertedValue interface{} | ||
switch schema.kind { | ||
case KindBoolean: | ||
if queryValue == "true" { | ||
convertedValue = true | ||
} else if queryValue == "false" { | ||
convertedValue = false | ||
} else { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Query validation failed for field %v: %v", field, ErrWrongType)) | ||
return | ||
} | ||
case KindString: | ||
convertedValue = queryValue | ||
case KindNumber: | ||
var err error | ||
convertedValue, err = strconv.ParseFloat(queryValue, 64) | ||
if err != nil { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Query validation failed for field %v: %v", field, ErrWrongType)) | ||
return | ||
} | ||
case KindInteger: | ||
var err error | ||
convertedValue, err = strconv.Atoi(queryValue) | ||
if err != nil { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Query validation failed for field %v: %v", field, ErrWrongType)) | ||
return | ||
} | ||
case KindArray: | ||
// TODO I'm not sure how this works yet | ||
c.AbortWithStatusJSON(http.StatusNotImplemented, "TODO") | ||
return | ||
default: | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Validation not possible due to kind: %v", schema.kind)) | ||
} | ||
if err := schema.Validate(convertedValue); err != nil { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Query validation failed for field %v: %v", field, err.Error())) | ||
return | ||
} | ||
} | ||
} | ||
|
||
if val.Body != nil { | ||
// TODO this could be an array, basic type, or "null" | ||
var body map[string]interface{} | ||
if err := c.BindJSON(&body); err != nil { | ||
c.AbortWithStatusJSON(400, err.Error()) | ||
return | ||
} | ||
for field, schema := range val.Body { | ||
if err := schema.Validate(body[field]); err != nil { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Body validation failed for field %v: %v", field, err.Error())) | ||
return | ||
} | ||
} | ||
// TODO perhaps the user passes a struct to bind to instead? | ||
data, _ := json.Marshal(body) | ||
c.Request.Body = ioutil.NopCloser(bytes.NewReader(data)) | ||
} | ||
|
||
if val.Path != nil { | ||
for field, schema := range val.Path { | ||
path := c.Param(field) | ||
if schema.required != nil && *schema.required && path == "" { | ||
c.AbortWithStatusJSON(400, fmt.Sprintf("Missing path param")) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} |
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,154 @@ | ||
package crud | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func init() { | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
|
||
func query(query string) (*httptest.ResponseRecorder, *gin.Context) { | ||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
c.Request = httptest.NewRequest("GET", "http://example.com"+query, nil) | ||
return w, c | ||
} | ||
|
||
func TestQueryValidation(t *testing.T) { | ||
tests := []struct { | ||
Schema map[string]Field | ||
Input string | ||
Expected int | ||
}{ | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": String(), | ||
}, | ||
Input: "", | ||
Expected: 200, | ||
}, { | ||
Schema: map[string]Field{ | ||
"testquery": String().Required(), | ||
}, | ||
Input: "", | ||
Expected: 400, | ||
}, { | ||
Schema: map[string]Field{ | ||
"testquery": String().Required(), | ||
}, | ||
Input: "?testquery=", | ||
Expected: 400, | ||
}, { | ||
Schema: map[string]Field{ | ||
"testquery": String().Required(), | ||
}, | ||
Input: "?testquery=ok", | ||
Expected: 200, | ||
}, { | ||
Schema: map[string]Field{ | ||
"testquery": Number(), | ||
}, | ||
Input: "", | ||
Expected: 200, | ||
}, { | ||
Schema: map[string]Field{ | ||
"testquery": Number().Required(), | ||
}, | ||
Input: "", | ||
Expected: 400, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Number().Required(), | ||
}, | ||
Input: "?testquery=1", | ||
Expected: 200, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Number().Required(), | ||
}, | ||
Input: "?testquery=1.1", | ||
Expected: 200, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Number(), | ||
}, | ||
Input: "?testquery=a", | ||
Expected: 400, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Boolean(), | ||
}, | ||
Input: "?testquery=true", | ||
Expected: 200, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Boolean(), | ||
}, | ||
Input: "?testquery=false", | ||
Expected: 200, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Boolean(), | ||
}, | ||
Input: "?testquery=1", | ||
Expected: 400, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Integer(), | ||
}, | ||
Input: "?testquery=1", | ||
Expected: 200, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Integer().Max(1), | ||
}, | ||
Input: "?testquery=2", | ||
Expected: 400, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Integer().Min(5), | ||
}, | ||
Input: "?testquery=4", | ||
Expected: 400, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Integer(), | ||
}, | ||
Input: "?testquery=1.1", | ||
Expected: 400, | ||
}, | ||
{ | ||
Schema: map[string]Field{ | ||
"testquery": Integer(), | ||
}, | ||
Input: "?testquery=a", | ||
Expected: 400, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
handler := preHandler(Spec{ | ||
Validate: Validate{Query: test.Schema}, | ||
}) | ||
|
||
w, c := query(test.Input) | ||
handler(c) | ||
|
||
if w.Result().StatusCode != test.Expected { | ||
t.Errorf("expected '%v' got '%v'. input: '%v'. schema: '%v'", test.Expected, w.Code, test.Input, test.Schema) | ||
} | ||
} | ||
} |
Oops, something went wrong.