diff --git a/api_testcase.go b/api_testcase.go index 8466579..5795199 100644 --- a/api_testcase.go +++ b/api_testcase.go @@ -4,9 +4,10 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/programmfabrik/fylr-apitest/lib/datastore" "time" + "github.com/programmfabrik/fylr-apitest/lib/datastore" + "github.com/programmfabrik/fylr-apitest/lib/cjson" "github.com/programmfabrik/fylr-apitest/lib/api" @@ -17,22 +18,19 @@ import ( log "github.com/sirupsen/logrus" ) -const ( - defaultTimeout int = 10 -) - // Case defines the structure of our single testcase // It gets read in by our config reader at the moment the mainfest.json gets parsed type Case struct { - Name string `json:"name"` - RequestData *util.GenericJson `json:"request"` - TypedResponseData *CaseResponse `json:"response"` - responseData util.GenericJson + Name string `json:"name"` + RequestData *util.GenericJson `json:"request"` + ResponseData util.GenericJson `json:"response"` ContinueOnFailure bool `json:"continue_on_failure"` Store map[string]interface{} `json:"store"` // init datastore before testrun StoreResponse map[string]string `json:"store_response_qjson"` // store qjson parsed response in datastore Timeout int `json:"timeout_ms"` + WaitBefore *int `json:"wait_before_ms"` + WaitAfter *int `json:"wait_after_ms"` Delay *int `json:"delay_ms"` BreakResponse []util.GenericJson `json:"break_response"` CollectResponse util.GenericJson `json:"collect_response"` @@ -65,22 +63,6 @@ func (testCase Case) runAPITestCase() (success bool) { } log.Infof(" [%2d] '%s'", testCase.index, testCase.Name) - //Marshal external datastructure in our internal used reponseData - jsonResponse, err := json.Marshal(testCase.TypedResponseData) - if err != nil { - err = fmt.Errorf("error marshaling TypedResponseData: %s", err) - r.SaveToReportLog(fmt.Sprintf("Error during execution: %s", err)) - log.Errorf(" [%2d] %s", testCase.index, err) - return false - } - err = json.Unmarshal(jsonResponse, &testCase.responseData) - if err != nil { - err = fmt.Errorf("error unmarshaling into responseData: %s", err) - r.SaveToReportLog(fmt.Sprintf("Error during execution: %s", err)) - log.Errorf(" [%2d] %s", testCase.index, err) - return false - } - r.NewChild(testCase.Name) start := time.Now() @@ -93,7 +75,7 @@ func (testCase Case) runAPITestCase() (success bool) { return false } - err = testCase.dataStore.SetMap(testCase.Store) + err := testCase.dataStore.SetMap(testCase.Store) if err != nil { err = fmt.Errorf("error setting datastore map:%s", err) r.SaveToReportLog(fmt.Sprintf("Error during execution: %s", err)) @@ -322,13 +304,16 @@ func (testCase Case) run() (success bool, err error) { collectPresent := testCase.CollectResponse != nil + if testCase.WaitBefore != nil { + log.Infof("wait_before_ms: %d", *testCase.WaitBefore) + time.Sleep(time.Duration(*testCase.WaitBefore) * time.Millisecond) + } + //Poll repeats the request until the right response is found, or a timeout triggers for { // delay between repeating a request if testCase.Delay != nil { time.Sleep(time.Duration(*testCase.Delay) * time.Millisecond) - } else { - time.Sleep(time.Duration(defaultTimeout) * time.Millisecond) } responsesMatch, request, apiResponse, err = testCase.executeRequest(requestCounter) @@ -409,6 +394,11 @@ func (testCase Case) run() (success bool, err error) { return false, nil } + if testCase.WaitAfter != nil { + log.Infof("wait_after_ms: %d", *testCase.WaitAfter) + time.Sleep(time.Duration(*testCase.WaitAfter) * time.Millisecond) + } + return true, nil } @@ -423,10 +413,10 @@ func (testCase Case) loadRequest() (req api.Request, err error) { func (testCase Case) loadResponse() (res api.Response, err error) { // unspecified response is interpreted as status_code 200 - if testCase.responseData == nil { + if testCase.ResponseData == nil { return api.NewResponse(200, nil, bytes.NewReader([]byte(""))) } - spec, err := testCase.loadResponseSerialization(testCase.responseData) + spec, err := testCase.loadResponseSerialization(testCase.ResponseData) if err != nil { return res, fmt.Errorf("error loading response spec: %s", err) }