You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.9 KiB

package SwagMock
import (
"fmt"
"github.com/go-openapi/spec"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strings"
)
func ValidateConsumes(operation spec.Operation, req http.Request) error {
contentType := req.Header["Content-Type"]
// if the spec says there's no content types consumed,
// validate that no content type header exists
if len(operation.Consumes) == 0 {
if contentType != nil {
return fmt.Errorf("operation %v expected no content type header but %v was found", operation.ID, contentType)
}
return nil
}
// if the spec says there a content type consumed
// validate that the request content type is correct
for _, consumes := range operation.Consumes {
for _, item := range contentType {
if item == consumes {
return nil
}
}
}
return fmt.Errorf("operation %v expected to consume %v but found a content type of %v", operation.ID, operation.Consumes, contentType)
}
func ValidateParameters(operation spec.Operation, req http.Request) error {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return errors.Wrap(err, "Mock Error: missing required formData")
}
if len(body) == 0 {
return fmt.Errorf("Mock Error: missing required formData")
}
barr := []byte{}
for _, b := range body {
barr = append(barr, byte(b))
}
data:=string(barr)
for _, parameter := range operation.Parameters {
switch parameter.In {
case "body":
case "query":
case "path":
default:
return fmt.Errorf("Mock Error: missing required formData")
break
case "formData":
if !strings.Contains(data,parameter.ParamProps.Name+"=") && parameter.Required{
//arr:=strings.Split(data,"=")
//err := validate.AgainstSchema(parameter.Schema, arr, strfmt.Default)
return fmt.Errorf("Mock Error: validating parameter missing ==> " + parameter.ParamProps.Name +", FormData: "+ data)
}
break
}
}
return nil
}