1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package openapi3filter_test
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/stretchr/testify/require"
)
type ExampleRequest struct {
Method string
URL string
ContentType string
Body interface{}
}
type ExampleResponse struct {
Status int
ContentType string
Body interface{}
}
func TestFilter(t *testing.T) {
// Declare router
swagger := &openapi3.Swagger{
Servers: openapi3.Servers{
{
URL: "http://example.com/api/",
},
},
Paths: openapi3.Paths{
"/prefix/{pathArg}/suffix": &openapi3.PathItem{
Post: &openapi3.Operation{
Parameters: openapi3.Parameters{
{
Value: &openapi3.Parameter{
In: "path",
Name: "pathArg",
Schema: openapi3.NewStringSchema().WithMaxLength(2).NewRef(),
},
},
{
Value: &openapi3.Parameter{
In: "query",
Name: "queryArg",
Schema: openapi3.NewStringSchema().WithMaxLength(2).NewRef(),
},
},
},
},
},
},
}
router := openapi3filter.NewRouter().WithSwagger(swagger)
expect := func(req ExampleRequest, resp ExampleResponse) error {
t.Logf("Request: %s %s", req.Method, req.URL)
httpReq, _ := http.NewRequest(req.Method, req.URL, marshalReader(req.Body))
httpReq.Header.Set("Content-Type", req.ContentType)
// Find route
route, pathParams, err := router.FindRoute(httpReq.Method, httpReq.URL)
require.NoError(t, err)
// Validate request
requestValidationInput := &openapi3filter.RequestValidationInput{
Request: httpReq,
PathParams: pathParams,
Route: route,
}
if err := openapi3filter.ValidateRequest(context.TODO(), requestValidationInput); err != nil {
return err
}
t.Logf("Response: %d", resp.Status)
responseValidationInput := &openapi3filter.ResponseValidationInput{
RequestValidationInput: requestValidationInput,
Status: resp.Status,
Header: http.Header{
"Content-Type": []string{
resp.ContentType,
},
},
}
if resp.Body != nil {
data, err := json.Marshal(resp.Body)
require.NoError(t, err)
responseValidationInput.SetBodyBytes(data)
}
err = openapi3filter.ValidateResponse(context.TODO(), responseValidationInput)
require.NoError(t, err)
return err
}
var err error
var req ExampleRequest
var resp ExampleResponse
// Test paths
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/v/suffix",
}
resp = ExampleResponse{
Status: 200,
}
err = expect(req, resp)
require.NoError(t, err)
// Test query parameter openapi3filter
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/EXCEEDS_MAX_LENGTH/suffix",
}
err = expect(req, resp)
require.IsType(t, &openapi3filter.RequestError{}, err)
// Test query parameter openapi3filter
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/v/suffix?queryArg=a",
}
err = expect(req, resp)
require.NoError(t, err)
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/v/suffix?queryArg=EXCEEDS_MAX_LENGTH",
}
err = expect(req, resp)
require.IsType(t, &openapi3filter.RequestError{}, err)
// Test query parameter openapi3filter
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/v/suffix?queryArg=a",
}
err = expect(req, resp)
require.NoError(t, err)
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/v/suffix?queryArg=EXCEEDS_MAX_LENGTH",
}
err = expect(req, resp)
require.IsType(t, &openapi3filter.RequestError{}, err)
req = ExampleRequest{
Method: "POST",
URL: "http://example.com/api/prefix/v/suffix",
}
resp = ExampleResponse{
Status: 200,
}
err = expect(req, resp)
// require.IsType(t, &openapi3filter.ResponseError{}, err)
require.NoError(t, err)
}
func marshalReader(value interface{}) io.ReadCloser {
if value == nil {
return nil
}
data, err := json.Marshal(value)
if err != nil {
panic(err)
}
return ioutil.NopCloser(bytes.NewReader(data))
}
|