From a8efab0b1a01eb7f1afbe5fd1e782f2957bb87bd Mon Sep 17 00:00:00 2001 From: Simon Frey Date: Tue, 14 May 2019 15:31:53 +0200 Subject: [PATCH 1/2] Remove typing for Response data, as it did break the "@response.json" notation --- api_testcase.go | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/api_testcase.go b/api_testcase.go index 8466579..9219550 100644 --- a/api_testcase.go +++ b/api_testcase.go @@ -24,10 +24,9 @@ const ( // 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 @@ -65,22 +64,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 +76,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)) @@ -423,10 +406,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) } From 9318b378d8414ac25b8bea7a7b8b4a66f5b95f87 Mon Sep 17 00:00:00 2001 From: Martin Rode Date: Thu, 6 Jun 2019 09:49:54 +0200 Subject: [PATCH 2/2] Added wait_before_ms and wait_after_ms as additional parameters in test. --- api_testcase.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/api_testcase.go b/api_testcase.go index 9219550..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,10 +18,6 @@ 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 { @@ -32,6 +29,8 @@ type Case struct { 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"` @@ -305,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) @@ -392,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 }