示例#1
0
func TestVersion(t *testing.T) {

	type TestMessage struct {
		Message *string `json:"message,required"`
	}

	//value := "Hello World!"
	testMessage := TestMessage{}

	err := utils.JSONValidation(testMessage)

	assert.NotEqual(t, nil, err, "should be an error")
}
示例#2
0
//BodyParser loads builder with maxSize and tries to load the message.
//if for some reason it can't parse the message, it will return an error.
//if successful, it will put the processed data into context with key 'json_body'
func BodyParser(builder func() interface{}, maxSize int64) func(chi.Handler) chi.Handler {
	return func(next chi.Handler) chi.Handler {
		return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
			to := builder()

			if err := utils.StreamJSONToStructWithLimit(r.Body, to, maxSize); err != nil {
				utils.Respond(w, 422, err)
				return
			}

			//check for required fields
			if err := utils.JSONValidation(to); err != nil {
				utils.Respond(w, 400, err)
				return
			}

			ctx = context.WithValue(ctx, constants.CtxKeyParsedBody, to)

			next.ServeHTTPC(ctx, w, r)
		})
	}
}