github.com/craicoverflow/tyk@v2.9.6-rc3+incompatible/gateway/mw_validate_json_test.go (about)

     1  package gateway
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/TykTechnologies/tyk/apidef"
     9  	"github.com/TykTechnologies/tyk/test"
    10  )
    11  
    12  var testJsonSchema = `{
    13      "title": "Person",
    14      "type": "object",
    15      "properties": {
    16          "firstName": {
    17              "type": "string"
    18          },
    19          "lastName": {
    20              "type": "string"
    21          },
    22          "age": {
    23              "description": "Age in years",
    24              "type": "integer",
    25              "minimum": 0
    26          },
    27  		"objs":{
    28  			"enum":["a","b","c"],
    29  			"type":"string"
    30  		}
    31      },
    32      "required": ["firstName", "lastName"]
    33  }`
    34  
    35  func testPrepareValidateJSONSchema() {
    36  	BuildAndLoadAPI(func(spec *APISpec) {
    37  		UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
    38  			json.Unmarshal([]byte(`[
    39  				{
    40  					"path": "/v",
    41  					"method": "POST",
    42  					"schema": `+testJsonSchema+`
    43  				}
    44  			]`), &v.ExtendedPaths.ValidateJSON)
    45  		})
    46  
    47  		spec.Proxy.ListenPath = "/"
    48  	})
    49  }
    50  
    51  func TestValidateJSONSchema(t *testing.T) {
    52  	ts := StartTest()
    53  	defer ts.Close()
    54  
    55  	testPrepareValidateJSONSchema()
    56  
    57  	ts.Run(t, []test.TestCase{
    58  		{Method: "POST", Path: "/without_validation", Data: "{not_valid}", Code: http.StatusOK},
    59  		{Method: "POST", Path: "/v", Data: `{"age":23}`, BodyMatch: `firstName: firstName is required; lastName: lastName is required`, Code: http.StatusUnprocessableEntity},
    60  		{Method: "POST", Path: "/v", Data: `[]`, BodyMatch: `Expected: object, given: array`, Code: http.StatusUnprocessableEntity},
    61  		{Method: "POST", Path: "/v", Data: `not_json`, Code: http.StatusBadRequest},
    62  		{Method: "POST", Path: "/v", Data: `{"age":23, "firstName": "Harry", "lastName": "Potter"}`, Code: http.StatusOK},
    63  		{Method: "POST", Path: "/v", Data: `{"age":23, "firstName": "Harry", "lastName": "Potter", "objs": "d"}`, Code: http.StatusUnprocessableEntity, BodyMatch: `objs: objs must be one of the following: \\"a\\", \\"b\\", \\"c\\"`},
    64  	}...)
    65  }
    66  
    67  func BenchmarkValidateJSONSchema(b *testing.B) {
    68  	b.ReportAllocs()
    69  
    70  	ts := StartTest()
    71  	defer ts.Close()
    72  
    73  	testPrepareValidateJSONSchema()
    74  
    75  	for i := 0; i < b.N; i++ {
    76  		ts.Run(b, []test.TestCase{
    77  			{Method: "POST", Path: "/without_validation", Data: "{not_valid}", Code: http.StatusOK},
    78  			{Method: "POST", Path: "/v", Data: `{"age":23}`, BodyMatch: `firstName: firstName is required; lastName: lastName is required`, Code: http.StatusUnprocessableEntity},
    79  			{Method: "POST", Path: "/v", Data: `[]`, BodyMatch: `Expected: object, given: array`, Code: http.StatusUnprocessableEntity},
    80  			{Method: "POST", Path: "/v", Data: `not_json`, Code: http.StatusBadRequest},
    81  			{Method: "POST", Path: "/v", Data: `{"age":23, "firstName": "Harry", "lastName": "Potter"}`, Code: http.StatusOK},
    82  		}...)
    83  	}
    84  }