now making valid requests
This commit is contained in:
39
main.go
39
main.go
@@ -33,7 +33,8 @@ type TestCase struct {
|
||||
Expect ExpectOptions `json:"expect"`
|
||||
|
||||
Header map[string]string
|
||||
Body map[string]string
|
||||
//Body map[string]string
|
||||
Body []byte
|
||||
|
||||
// Something to store results in
|
||||
ResultStatusCode int
|
||||
@@ -136,6 +137,7 @@ func RunTest(testCase *TestCase) error {
|
||||
var err error
|
||||
var requestUrl string
|
||||
var requestType string
|
||||
var req *http.Request
|
||||
//var requestHeaders map[string]string
|
||||
//var requestBody map[string]string
|
||||
|
||||
@@ -159,6 +161,7 @@ func RunTest(testCase *TestCase) error {
|
||||
|
||||
// Populate request body
|
||||
|
||||
// Get request type, default to GET
|
||||
if len(testCase.Method) == 0 {
|
||||
//return errors.New("no test method specifed, must be either GET or POST")
|
||||
|
||||
@@ -169,7 +172,12 @@ func RunTest(testCase *TestCase) error {
|
||||
}
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest(requestType, requestUrl, nil)
|
||||
if len(testCase.Body) > 0 {
|
||||
req, err = http.NewRequest(requestType, requestUrl, bytes.NewBuffer(testCase.Body))
|
||||
} else {
|
||||
req, err = http.NewRequest(requestType, requestUrl, nil)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errMessage := fmt.Sprintf("error submitting request : '%s'\n", err)
|
||||
return errors.New(errMessage)
|
||||
@@ -180,14 +188,15 @@ func RunTest(testCase *TestCase) error {
|
||||
if len(testDefinitions.Headers) > 0 {
|
||||
fmt.Printf("Adding global headers to request\n")
|
||||
for k, v := range testDefinitions.Headers {
|
||||
fmt.Printf("Add Header %s = %s\n", k, v)
|
||||
fmt.Printf("Add global header %s = %s\n", k, v)
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// Then any test case specific headers
|
||||
for i, key := range testCase.Header {
|
||||
req.Header.Add(key, testDefinitions.Headers[i])
|
||||
for k, v := range testCase.Header {
|
||||
fmt.Printf("Add Header %s = %s\n", k, v)
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
|
||||
// Perform request
|
||||
@@ -199,6 +208,16 @@ func RunTest(testCase *TestCase) error {
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Store response
|
||||
fmt.Printf("Header response:\n%+v\n", resp.Header)
|
||||
fmt.Printf("http_status:\n'%+v'\n", resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Body response:\n%s\n", string(body))
|
||||
|
||||
// No errors if we reached this point
|
||||
return nil
|
||||
@@ -366,7 +385,15 @@ func ReadInput(data []byte, testDefinitions *TestDefinitions) {
|
||||
|
||||
// TODO : Support nested json instead of single level flat request
|
||||
|
||||
thisTestCase.Body = OrderedToStringMap(val.(orderedmap.OrderedMap))
|
||||
//thisTestCase.Body = OrderedToStringMap(val.(orderedmap.OrderedMap))
|
||||
|
||||
bytes, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
fmt.Printf("Error processing request body for test case : '%s'\n", err)
|
||||
} else {
|
||||
thisTestCase.Body = bytes
|
||||
//fmt.Printf("Body marshalled:\n%v\n", string(bytes))
|
||||
}
|
||||
}
|
||||
// Header
|
||||
if val, ok := thisTestCaseMap.Get("header"); ok {
|
||||
|
Reference in New Issue
Block a user