test: add unit tests of binary upload (#587)
* test: add unit tests of binary upload * add more http status code desc --------- Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
b0d18fcdf8
commit
efb2235754
|
@ -84,6 +84,7 @@
|
||||||
"404": "404 Not Found",
|
"404": "404 Not Found",
|
||||||
"405": "405 Method Not Allowed",
|
"405": "405 Method Not Allowed",
|
||||||
"409": "409 Conflict",
|
"409": "409 Conflict",
|
||||||
|
"413": "Content Too Large",
|
||||||
"415": "415 Unsupported Media Type",
|
"415": "415 Unsupported Media Type",
|
||||||
"422": "422 Unprocessable Content",
|
"422": "422 Unprocessable Content",
|
||||||
"500": "500 Internal Server Error",
|
"500": "500 Internal Server Error",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2023 API Testing Authors.
|
Copyright 2023-2024 API Testing Authors.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -16,159 +16,159 @@ limitations under the License.
|
||||||
package testing
|
package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/linuxsuren/api-testing/pkg/util"
|
"github.com/linuxsuren/api-testing/pkg/util"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestSuite represents a set of test cases
|
// TestSuite represents a set of test cases
|
||||||
type TestSuite struct {
|
type TestSuite struct {
|
||||||
Name string `yaml:"name,omitempty" json:"name,omitempty"`
|
Name string `yaml:"name,omitempty" json:"name,omitempty"`
|
||||||
API string `yaml:"api,omitempty" json:"api,omitempty"`
|
API string `yaml:"api,omitempty" json:"api,omitempty"`
|
||||||
Spec APISpec `yaml:"spec,omitempty" json:"spec,omitempty"`
|
Spec APISpec `yaml:"spec,omitempty" json:"spec,omitempty"`
|
||||||
Param map[string]string `yaml:"param,omitempty" json:"param,omitempty"`
|
Param map[string]string `yaml:"param,omitempty" json:"param,omitempty"`
|
||||||
Items []TestCase `yaml:"items,omitempty" json:"items,omitempty"`
|
Items []TestCase `yaml:"items,omitempty" json:"items,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type APISpec struct {
|
type APISpec struct {
|
||||||
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"`
|
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"`
|
||||||
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
||||||
RPC *RPCDesc `yaml:"rpc,omitempty" json:"rpc,omitempty"`
|
RPC *RPCDesc `yaml:"rpc,omitempty" json:"rpc,omitempty"`
|
||||||
Secure *Secure `yaml:"secure,omitempty" json:"secure,omitempty"`
|
Secure *Secure `yaml:"secure,omitempty" json:"secure,omitempty"`
|
||||||
Metric *Metric `yaml:"metric,omitempty" json:"metric,omitempty"`
|
Metric *Metric `yaml:"metric,omitempty" json:"metric,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HistoryTestSuite struct {
|
type HistoryTestSuite struct {
|
||||||
HistorySuiteName string `yaml:"name,omitempty" json:"name,omitempty"`
|
HistorySuiteName string `yaml:"name,omitempty" json:"name,omitempty"`
|
||||||
Items []HistoryTestCase `yaml:"items,omitempty" json:"items,omitempty"`
|
Items []HistoryTestCase `yaml:"items,omitempty" json:"items,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HistoryTestCase struct {
|
type HistoryTestCase struct {
|
||||||
ID string `yaml:"id,omitempty" json:"id,omitempty"`
|
ID string `yaml:"id,omitempty" json:"id,omitempty"`
|
||||||
CaseName string `yaml:"caseName,omitempty" json:"name,omitempty"`
|
CaseName string `yaml:"caseName,omitempty" json:"name,omitempty"`
|
||||||
SuiteName string `yaml:"suiteName,omitempty" json:"suiteName,omitempty"`
|
SuiteName string `yaml:"suiteName,omitempty" json:"suiteName,omitempty"`
|
||||||
HistorySuiteName string `yaml:"historySuiteName,omitempty" json:"historySuiteName,omitempty"`
|
HistorySuiteName string `yaml:"historySuiteName,omitempty" json:"historySuiteName,omitempty"`
|
||||||
CreateTime time.Time `yaml:"createTime,omitempty" json:"createTime,omitempty"`
|
CreateTime time.Time `yaml:"createTime,omitempty" json:"createTime,omitempty"`
|
||||||
SuiteAPI string `yaml:"api,omitempty" json:"api,omitempty"`
|
SuiteAPI string `yaml:"api,omitempty" json:"api,omitempty"`
|
||||||
SuiteSpec APISpec `yaml:"spec,omitempty" json:"spec,omitempty"`
|
SuiteSpec APISpec `yaml:"spec,omitempty" json:"spec,omitempty"`
|
||||||
SuiteParam map[string]string `yaml:"param,omitempty" json:"param,omitempty"`
|
SuiteParam map[string]string `yaml:"param,omitempty" json:"param,omitempty"`
|
||||||
Data TestCase `yaml:"data,omitempty" json:"data,omitempty"`
|
Data TestCase `yaml:"data,omitempty" json:"data,omitempty"`
|
||||||
HistoryHeader map[string]string `yaml:"historyHeader,omitempty" json:"historyHeader,omitempty"`
|
HistoryHeader map[string]string `yaml:"historyHeader,omitempty" json:"historyHeader,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HistoryTestResult struct {
|
type HistoryTestResult struct {
|
||||||
Message string `yaml:"message,omitempty" json:"message,omitempty"`
|
Message string `yaml:"message,omitempty" json:"message,omitempty"`
|
||||||
Error string `yaml:"error,omitempty" json:"error,omitempty"`
|
Error string `yaml:"error,omitempty" json:"error,omitempty"`
|
||||||
TestCaseResult []TestCaseResult `yaml:"testCaseResult,omitempty" json:"testCaseResult,omitempty"`
|
TestCaseResult []TestCaseResult `yaml:"testCaseResult,omitempty" json:"testCaseResult,omitempty"`
|
||||||
Data HistoryTestCase `yaml:"data,omitempty" json:"data,omitempty"`
|
Data HistoryTestCase `yaml:"data,omitempty" json:"data,omitempty"`
|
||||||
CreateTime time.Time `yaml:"createTime,omitempty" json:"createTime,omitempty"`
|
CreateTime time.Time `yaml:"createTime,omitempty" json:"createTime,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RPCDesc struct {
|
type RPCDesc struct {
|
||||||
ImportPath []string `yaml:"import,omitempty" json:"import,omitempty"`
|
ImportPath []string `yaml:"import,omitempty" json:"import,omitempty"`
|
||||||
ServerReflection bool `yaml:"serverReflection,omitempty" json:"serverReflection,omitempty"`
|
ServerReflection bool `yaml:"serverReflection,omitempty" json:"serverReflection,omitempty"`
|
||||||
ProtoFile string `yaml:"protofile,omitempty" json:"protofile,omitempty"`
|
ProtoFile string `yaml:"protofile,omitempty" json:"protofile,omitempty"`
|
||||||
ProtoSet string `yaml:"protoset,omitempty" json:"protoset,omitempty"`
|
ProtoSet string `yaml:"protoset,omitempty" json:"protoset,omitempty"`
|
||||||
Raw string `yaml:"raw,omitempty" json:"raw,omitempty"`
|
Raw string `yaml:"raw,omitempty" json:"raw,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Secure struct {
|
type Secure struct {
|
||||||
Insecure bool `yaml:"insecure,omitempty" json:"insecure,omitempty"`
|
Insecure bool `yaml:"insecure,omitempty" json:"insecure,omitempty"`
|
||||||
CertFile string `yaml:"cert,omitempty" json:"cert,omitempty"`
|
CertFile string `yaml:"cert,omitempty" json:"cert,omitempty"`
|
||||||
CAFile string `yaml:"ca,omitempty" json:"ca,omitempty"`
|
CAFile string `yaml:"ca,omitempty" json:"ca,omitempty"`
|
||||||
KeyFile string `yaml:"key,omitempty" json:"key,omitempty"`
|
KeyFile string `yaml:"key,omitempty" json:"key,omitempty"`
|
||||||
ServerName string `yaml:"serverName,omitempty" json:"serverName,omitempty"`
|
ServerName string `yaml:"serverName,omitempty" json:"serverName,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
Type string `yaml:"type,omitempty" json:"type,omitempty"`
|
Type string `yaml:"type,omitempty" json:"type,omitempty"`
|
||||||
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestCase represents a test case
|
// TestCase represents a test case
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
ID string `yaml:"id,omitempty" json:"id,omitempty"`
|
ID string `yaml:"id,omitempty" json:"id,omitempty"`
|
||||||
Name string `yaml:"name,omitempty" json:"name,omitempty"`
|
Name string `yaml:"name,omitempty" json:"name,omitempty"`
|
||||||
Group string `yaml:"group,omitempty" json:"group,omitempty"`
|
Group string `yaml:"group,omitempty" json:"group,omitempty"`
|
||||||
Before *Job `yaml:"before,omitempty" json:"before,omitempty"`
|
Before *Job `yaml:"before,omitempty" json:"before,omitempty"`
|
||||||
After *Job `yaml:"after,omitempty" json:"after,omitempty"`
|
After *Job `yaml:"after,omitempty" json:"after,omitempty"`
|
||||||
Request Request `yaml:"request" json:"request"`
|
Request Request `yaml:"request" json:"request"`
|
||||||
Expect Response `yaml:"expect,omitempty" json:"expect,omitempty"`
|
Expect Response `yaml:"expect,omitempty" json:"expect,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// InScope returns true if the test case is in scope with the given items.
|
// InScope returns true if the test case is in scope with the given items.
|
||||||
// Returns true if the items is empty.
|
// Returns true if the items is empty.
|
||||||
func (c *TestCase) InScope(items []string) bool {
|
func (c *TestCase) InScope(items []string) bool {
|
||||||
if len(items) == 0 {
|
if len(items) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if item == c.Name {
|
if item == c.Name {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Job contains a list of jobs
|
// Job contains a list of jobs
|
||||||
type Job struct {
|
type Job struct {
|
||||||
Items []string `yaml:"items,omitempty" json:"items,omitempty"`
|
Items []string `yaml:"items,omitempty" json:"items,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request represents a HTTP request
|
// Request represents a HTTP request
|
||||||
type Request struct {
|
type Request struct {
|
||||||
API string `yaml:"api" json:"api"`
|
API string `yaml:"api" json:"api"`
|
||||||
Method string `yaml:"method,omitempty" json:"method,omitempty" jsonschema:"enum=GET,enum=POST,enum=PUT,enum=DELETE"`
|
Method string `yaml:"method,omitempty" json:"method,omitempty" jsonschema:"enum=GET,enum=POST,enum=PUT,enum=DELETE"`
|
||||||
Query SortedKeysStringMap `yaml:"query,omitempty" json:"query,omitempty"`
|
Query SortedKeysStringMap `yaml:"query,omitempty" json:"query,omitempty"`
|
||||||
Header map[string]string `yaml:"header,omitempty" json:"header,omitempty"`
|
Header map[string]string `yaml:"header,omitempty" json:"header,omitempty"`
|
||||||
Cookie map[string]string `yaml:"cookie,omitempty" json:"cookie,omitempty"`
|
Cookie map[string]string `yaml:"cookie,omitempty" json:"cookie,omitempty"`
|
||||||
Form map[string]string `yaml:"form,omitempty" json:"form,omitempty"`
|
Form map[string]string `yaml:"form,omitempty" json:"form,omitempty"`
|
||||||
Body RequestBody `yaml:"body,omitempty" json:"body,omitempty"`
|
Body RequestBody `yaml:"body,omitempty" json:"body,omitempty"`
|
||||||
BodyFromFile string `yaml:"bodyFromFile,omitempty" json:"bodyFromFile,omitempty"`
|
BodyFromFile string `yaml:"bodyFromFile,omitempty" json:"bodyFromFile,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestBody struct {
|
type RequestBody struct {
|
||||||
Value string `json:"value" yaml:"value"`
|
Value string `json:"value" yaml:"value"`
|
||||||
isJson bool
|
isJson bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestBody(val string) RequestBody {
|
func NewRequestBody(val string) RequestBody {
|
||||||
return RequestBody{Value: val}
|
return RequestBody{Value: val}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *RequestBody) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
|
func (e *RequestBody) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
|
||||||
gql := &GraphQLRequestBody{}
|
gql := &GraphQLRequestBody{}
|
||||||
err = unmarshal(gql)
|
err = unmarshal(gql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
val := ""
|
val := ""
|
||||||
if err = unmarshal(&val); err == nil {
|
if err = unmarshal(&val); err == nil {
|
||||||
e.Value = val
|
e.Value = val
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var data []byte
|
var data []byte
|
||||||
if data, err = json.Marshal(gql); err == nil {
|
if data, err = json.Marshal(gql); err == nil {
|
||||||
e.Value = string(data)
|
e.Value = string(data)
|
||||||
e.isJson = true
|
e.isJson = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e RequestBody) MarshalYAML() (val interface{}, err error) {
|
func (e RequestBody) MarshalYAML() (val interface{}, err error) {
|
||||||
val = e.Value
|
val = e.Value
|
||||||
if e.isJson {
|
if e.isJson {
|
||||||
gql := &GraphQLRequestBody{}
|
gql := &GraphQLRequestBody{}
|
||||||
if err = json.Unmarshal([]byte(e.Value), gql); err == nil {
|
if err = json.Unmarshal([]byte(e.Value), gql); err == nil {
|
||||||
val = gql
|
val = gql
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ yaml.Marshaler = &RequestBody{}
|
var _ yaml.Marshaler = &RequestBody{}
|
||||||
|
@ -176,134 +176,136 @@ var _ yaml.Marshaler = &RequestBody{}
|
||||||
// var _ yaml.Unmarshaler = &RequestBody{}
|
// var _ yaml.Unmarshaler = &RequestBody{}
|
||||||
|
|
||||||
func (e RequestBody) String() string {
|
func (e RequestBody) String() string {
|
||||||
return e.Value
|
return e.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e RequestBody) IsEmpty() bool {
|
func (e RequestBody) IsEmpty() bool {
|
||||||
return e.Value == ""
|
return e.Value == ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e RequestBody) Bytes() (data []byte) {
|
func (e RequestBody) Bytes() (data []byte) {
|
||||||
var err error
|
var err error
|
||||||
if strings.HasPrefix(e.Value, util.ImageBase64Prefix) {
|
if strings.HasPrefix(e.Value, util.ImageBase64Prefix) {
|
||||||
data, err = decodeBase64Body(e.Value, util.ImageBase64Prefix)
|
data, err = decodeBase64Body(e.Value, util.ImageBase64Prefix)
|
||||||
} else if strings.HasPrefix(e.Value, util.PDFBase64Prefix) {
|
} else if strings.HasPrefix(e.Value, util.PDFBase64Prefix) {
|
||||||
data, err = decodeBase64Body(e.Value, util.PDFBase64Prefix)
|
data, err = decodeBase64Body(e.Value, util.PDFBase64Prefix)
|
||||||
} else if strings.HasPrefix(e.Value, util.ZIPBase64Prefix) {
|
} else if strings.HasPrefix(e.Value, util.ZIPBase64Prefix) {
|
||||||
data, err = decodeBase64Body(e.Value, util.ZIPBase64Prefix)
|
data, err = decodeBase64Body(e.Value, util.ZIPBase64Prefix)
|
||||||
} else {
|
} else if strings.HasPrefix(e.Value, util.BinaryBase64Prefix) {
|
||||||
data = []byte(e.Value)
|
data, err = decodeBase64Body(e.Value, util.BinaryBase64Prefix)
|
||||||
}
|
} else {
|
||||||
|
data = []byte(e.Value)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error decoding: %v", err)
|
log.Printf("Error decoding: %v", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeBase64Body(raw, prefix string) ([]byte, error) {
|
func decodeBase64Body(raw, prefix string) ([]byte, error) {
|
||||||
rawStr := strings.TrimPrefix(raw, prefix)
|
rawStr := strings.TrimPrefix(raw, prefix)
|
||||||
return base64.StdEncoding.DecodeString(rawStr)
|
return base64.StdEncoding.DecodeString(rawStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
type GraphQLRequestBody struct {
|
type GraphQLRequestBody struct {
|
||||||
Query string `yaml:"query" json:"query"`
|
Query string `yaml:"query" json:"query"`
|
||||||
OperationName string `yaml:"operationName" json:"operationName"`
|
OperationName string `yaml:"operationName" json:"operationName"`
|
||||||
Variables map[string]string `yaml:"variables" json:"variables"`
|
Variables map[string]string `yaml:"variables" json:"variables"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response is the expected response
|
// Response is the expected response
|
||||||
type Response struct {
|
type Response struct {
|
||||||
StatusCode int `yaml:"statusCode,omitempty" json:"statusCode,omitempty"`
|
StatusCode int `yaml:"statusCode,omitempty" json:"statusCode,omitempty"`
|
||||||
Body string `yaml:"body,omitempty" json:"body,omitempty"`
|
Body string `yaml:"body,omitempty" json:"body,omitempty"`
|
||||||
Header map[string]string `yaml:"header,omitempty" json:"header,omitempty"`
|
Header map[string]string `yaml:"header,omitempty" json:"header,omitempty"`
|
||||||
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect,omitempty" json:"bodyFieldsExpect,omitempty"`
|
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect,omitempty" json:"bodyFieldsExpect,omitempty"`
|
||||||
Verify []string `yaml:"verify,omitempty" json:"verify,omitempty"`
|
Verify []string `yaml:"verify,omitempty" json:"verify,omitempty"`
|
||||||
ConditionalVerify []ConditionalVerify `yaml:"conditionalVerify,omitempty" json:"conditionalVerify,omitempty"`
|
ConditionalVerify []ConditionalVerify `yaml:"conditionalVerify,omitempty" json:"conditionalVerify,omitempty"`
|
||||||
Schema string `yaml:"schema,omitempty" json:"schema,omitempty"`
|
Schema string `yaml:"schema,omitempty" json:"schema,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Response) GetBody() string {
|
func (r Response) GetBody() string {
|
||||||
return r.Body
|
return r.Body
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Response) GetBodyFieldsExpect() map[string]interface{} {
|
func (r Response) GetBodyFieldsExpect() map[string]interface{} {
|
||||||
return r.BodyFieldsExpect
|
return r.BodyFieldsExpect
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConditionalVerify struct {
|
type ConditionalVerify struct {
|
||||||
Condition []string `yaml:"condition,omitempty" json:"condition,omitempty"`
|
Condition []string `yaml:"condition,omitempty" json:"condition,omitempty"`
|
||||||
Verify []string `yaml:"verify,omitempty" json:"verify,omitempty"`
|
Verify []string `yaml:"verify,omitempty" json:"verify,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SortedKeysStringMap map[string]interface{}
|
type SortedKeysStringMap map[string]interface{}
|
||||||
|
|
||||||
func (m SortedKeysStringMap) Keys() (keys []string) {
|
func (m SortedKeysStringMap) Keys() (keys []string) {
|
||||||
for k := range m {
|
for k := range m {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m SortedKeysStringMap) GetValue(key string) string {
|
func (m SortedKeysStringMap) GetValue(key string) string {
|
||||||
val := m[key]
|
val := m[key]
|
||||||
|
|
||||||
switch o := any(val).(type) {
|
switch o := any(val).(type) {
|
||||||
case string:
|
case string:
|
||||||
return val.(string)
|
return val.(string)
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
verifier := convertToVerifier(o)
|
verifier := convertToVerifier(o)
|
||||||
return verifier.Value
|
return verifier.Value
|
||||||
case *Verifier:
|
case *Verifier:
|
||||||
return o.Value
|
return o.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m SortedKeysStringMap) GetVerifier(key string) (verifier *Verifier) {
|
func (m SortedKeysStringMap) GetVerifier(key string) (verifier *Verifier) {
|
||||||
val := m[key]
|
val := m[key]
|
||||||
|
|
||||||
switch o := any(val).(type) {
|
switch o := any(val).(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
verifier = convertToVerifier(o)
|
verifier = convertToVerifier(o)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertToVerifier(data map[string]interface{}) (verifier *Verifier) {
|
func convertToVerifier(data map[string]interface{}) (verifier *Verifier) {
|
||||||
verifier = &Verifier{}
|
verifier = &Verifier{}
|
||||||
|
|
||||||
if data, err := yaml.Marshal(data); err == nil {
|
if data, err := yaml.Marshal(data); err == nil {
|
||||||
if err = yaml.Unmarshal(data, verifier); err != nil {
|
if err = yaml.Unmarshal(data, verifier); err != nil {
|
||||||
verifier = nil
|
verifier = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Verifier struct {
|
type Verifier struct {
|
||||||
Value string `yaml:"value,omitempty" json:"value,omitempty"`
|
Value string `yaml:"value,omitempty" json:"value,omitempty"`
|
||||||
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
|
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
|
||||||
Max int `yaml:"max"`
|
Max int `yaml:"max"`
|
||||||
Min int `yaml:"min"`
|
Min int `yaml:"min"`
|
||||||
MaxLength int `yaml:"maxLength"`
|
MaxLength int `yaml:"maxLength"`
|
||||||
MinLength int `yaml:"minLength"`
|
MinLength int `yaml:"minLength"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestResult struct {
|
type TestResult struct {
|
||||||
Message string `yaml:"message,omitempty" json:"message,omitempty"`
|
Message string `yaml:"message,omitempty" json:"message,omitempty"`
|
||||||
Error string `yaml:"error,omitempty" json:"error,omitempty"`
|
Error string `yaml:"error,omitempty" json:"error,omitempty"`
|
||||||
TestCaseResult []*TestCaseResult `yaml:"testCaseResult,omitempty" json:"testCaseResult,omitempty"`
|
TestCaseResult []*TestCaseResult `yaml:"testCaseResult,omitempty" json:"testCaseResult,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestCaseResult struct {
|
type TestCaseResult struct {
|
||||||
StatusCode int `yaml:"statusCode,omitempty" json:"statusCode,omitempty"`
|
StatusCode int `yaml:"statusCode,omitempty" json:"statusCode,omitempty"`
|
||||||
Body string `yaml:"body,omitempty" json:"body,omitempty"`
|
Body string `yaml:"body,omitempty" json:"body,omitempty"`
|
||||||
Header map[string]string `yaml:"header,omitempty" json:"header,omitempty"`
|
Header map[string]string `yaml:"header,omitempty" json:"header,omitempty"`
|
||||||
Error string `yaml:"error,omitempty" json:"error,omitempty"`
|
Error string `yaml:"error,omitempty" json:"error,omitempty"`
|
||||||
Id string `yaml:"id,omitempty" json:"id,omitempty"`
|
Id string `yaml:"id,omitempty" json:"id,omitempty"`
|
||||||
Output string `yaml:"output,omitempty" json:"output,omitempty"`
|
Output string `yaml:"output,omitempty" json:"output,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,24 +16,25 @@ limitations under the License.
|
||||||
package testing_test
|
package testing_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"github.com/linuxsuren/api-testing/pkg/util"
|
||||||
|
"testing"
|
||||||
|
|
||||||
atesting "github.com/linuxsuren/api-testing/pkg/testing"
|
atesting "github.com/linuxsuren/api-testing/pkg/testing"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInScope(t *testing.T) {
|
func TestInScope(t *testing.T) {
|
||||||
testCase := &atesting.TestCase{Name: "foo"}
|
testCase := &atesting.TestCase{Name: "foo"}
|
||||||
assert.True(t, testCase.InScope(nil))
|
assert.True(t, testCase.InScope(nil))
|
||||||
assert.True(t, testCase.InScope([]string{"foo"}))
|
assert.True(t, testCase.InScope([]string{"foo"}))
|
||||||
assert.False(t, testCase.InScope([]string{"bar"}))
|
assert.False(t, testCase.InScope([]string{"bar"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequestBody(t *testing.T) {
|
func TestRequestBody(t *testing.T) {
|
||||||
req := &atesting.Request{}
|
req := &atesting.Request{}
|
||||||
graphqlBody := `api: /api
|
graphqlBody := `api: /api
|
||||||
body:
|
body:
|
||||||
query: query
|
query: query
|
||||||
operationName: ""
|
operationName: ""
|
||||||
|
@ -41,47 +42,88 @@ body:
|
||||||
name: rick
|
name: rick
|
||||||
`
|
`
|
||||||
|
|
||||||
err := yaml.Unmarshal([]byte(graphqlBody), req)
|
err := yaml.Unmarshal([]byte(graphqlBody), req)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, `{"query":"query","operationName":"","variables":{"name":"rick"}}`, req.Body.String())
|
assert.Equal(t, `{"query":"query","operationName":"","variables":{"name":"rick"}}`, req.Body.String())
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
data, err = yaml.Marshal(req)
|
data, err = yaml.Marshal(req)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, graphqlBody, string(data))
|
assert.Equal(t, graphqlBody, string(data))
|
||||||
|
|
||||||
err = yaml.Unmarshal([]byte(`body: plain`), req)
|
err = yaml.Unmarshal([]byte(`body: plain`), req)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "plain", req.Body.String())
|
assert.Equal(t, "plain", req.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponse(t *testing.T) {
|
func TestResponse(t *testing.T) {
|
||||||
resp := &atesting.Response{
|
resp := &atesting.Response{
|
||||||
Body: "body",
|
Body: "body",
|
||||||
BodyFieldsExpect: map[string]interface{}{
|
BodyFieldsExpect: map[string]interface{}{
|
||||||
"name": "rick",
|
"name": "rick",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
assert.Equal(t, "body", resp.GetBody())
|
assert.Equal(t, "body", resp.GetBody())
|
||||||
assert.Equal(t, map[string]interface{}{"name": "rick"}, resp.GetBodyFieldsExpect())
|
assert.Equal(t, map[string]interface{}{"name": "rick"}, resp.GetBodyFieldsExpect())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortedKeysStringMap(t *testing.T) {
|
func TestSortedKeysStringMap(t *testing.T) {
|
||||||
obj := atesting.SortedKeysStringMap{
|
obj := atesting.SortedKeysStringMap{
|
||||||
"c": "d",
|
"c": "d",
|
||||||
"f": map[string]interface{}{
|
"f": map[string]interface{}{
|
||||||
"value": "f",
|
"value": "f",
|
||||||
},
|
},
|
||||||
"e": &atesting.Verifier{
|
"e": &atesting.Verifier{
|
||||||
Value: "e",
|
Value: "e",
|
||||||
},
|
},
|
||||||
"a": "b",
|
"a": "b",
|
||||||
}
|
}
|
||||||
assert.Equal(t, []string{"a", "c", "e", "f"}, obj.Keys())
|
assert.Equal(t, []string{"a", "c", "e", "f"}, obj.Keys())
|
||||||
assert.Equal(t, "b", obj.GetValue("a"))
|
assert.Equal(t, "b", obj.GetValue("a"))
|
||||||
assert.Nil(t, obj.GetVerifier("b"))
|
assert.Nil(t, obj.GetVerifier("b"))
|
||||||
assert.Equal(t, "e", obj.GetValue("e"))
|
assert.Equal(t, "e", obj.GetValue("e"))
|
||||||
assert.Equal(t, "f", obj.GetValue("f"))
|
assert.Equal(t, "f", obj.GetValue("f"))
|
||||||
assert.Equal(t, "f", obj.GetVerifier("f").Value)
|
assert.Equal(t, "f", obj.GetVerifier("f").Value)
|
||||||
assert.Empty(t, obj.GetValue("not-found"))
|
assert.Empty(t, obj.GetValue("not-found"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBodyBytes(t *testing.T) {
|
||||||
|
const defaultPlainText = "hello"
|
||||||
|
const defaultBase64Text = "aGVsbG8="
|
||||||
|
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
rawBody string
|
||||||
|
expect []byte
|
||||||
|
}{{
|
||||||
|
name: "image base64",
|
||||||
|
rawBody: util.ImageBase64Prefix + defaultBase64Text,
|
||||||
|
expect: []byte(defaultPlainText),
|
||||||
|
}, {
|
||||||
|
name: "pdf",
|
||||||
|
rawBody: util.PDFBase64Prefix + defaultBase64Text,
|
||||||
|
expect: []byte(defaultPlainText),
|
||||||
|
}, {
|
||||||
|
name: "zip",
|
||||||
|
rawBody: util.ZIPBase64Prefix + defaultBase64Text,
|
||||||
|
expect: []byte(defaultPlainText),
|
||||||
|
}, {
|
||||||
|
name: "binary",
|
||||||
|
rawBody: util.BinaryBase64Prefix + defaultBase64Text,
|
||||||
|
expect: []byte(defaultPlainText),
|
||||||
|
}, {
|
||||||
|
name: "raw",
|
||||||
|
rawBody: defaultPlainText,
|
||||||
|
expect: []byte(defaultPlainText),
|
||||||
|
}}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
body := atesting.RequestBody{
|
||||||
|
Value: tc.rawBody,
|
||||||
|
}
|
||||||
|
data := body.Bytes()
|
||||||
|
assert.Equal(t, tc.expect, data)
|
||||||
|
assert.False(t, body.IsEmpty())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue