Skip to content

Commit c2d7189

Browse files
radtristeMarianMacik
authored andcommitted
[KOGITO-1245] Corrected repetitive post calls if failing (apache#183)
1 parent 8640826 commit c2d7189

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

bddframework/framework/http.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ import (
1818
"bytes"
1919
"encoding/json"
2020
"fmt"
21-
"io"
2221
"net/http"
2322
"strings"
2423
"time"
2524
)
2625

2726
// WaitForSuccessfulHTTPRequest waits for an HTTP request to be successful
28-
func WaitForSuccessfulHTTPRequest(namespace, httpMethod, uri, path, bodyFormat string, body io.Reader, timeoutInMin int) error {
27+
func WaitForSuccessfulHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, bodyContent string, timeoutInMin int) error {
2928
return WaitFor(namespace, fmt.Sprintf("HTTP %s request on path '%s' to be successful", httpMethod, path), time.Duration(timeoutInMin)*time.Minute, func() (bool, error) {
30-
return IsHTTPRequestSuccessful(namespace, httpMethod, uri, path, bodyFormat, body)
29+
return IsHTTPRequestSuccessful(namespace, httpMethod, uri, path, bodyFormat, bodyContent)
3130
})
3231
}
3332

3433
// ExecuteHTTPRequest executes an HTTP request
35-
func ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat string, body io.Reader) (*http.Response, error) {
36-
request, err := http.NewRequest(httpMethod, uri+"/"+path, body)
37-
if body != nil && len(bodyFormat) > 0 {
34+
func ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, bodyContent string) (*http.Response, error) {
35+
GetLogger(namespace).Debugf("ExecuteHTTPRequest %s on uri %s, with path %s, %s bodyContent %s", httpMethod, uri, path, bodyFormat, bodyContent)
36+
37+
request, err := http.NewRequest(httpMethod, uri+"/"+path, strings.NewReader(bodyContent))
38+
if len(bodyContent) > 0 && len(bodyFormat) > 0 {
3839
switch bodyFormat {
3940
case "json":
4041
request.Header.Add("Content-Type", "application/json")
@@ -54,8 +55,8 @@ func ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat string, bod
5455
}
5556

5657
// IsHTTPRequestSuccessful makes and checks whether an http request is successful
57-
func IsHTTPRequestSuccessful(namespace, httpMethod, uri, path, bodyFormat string, body io.Reader) (bool, error) {
58-
response, err := ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, body)
58+
func IsHTTPRequestSuccessful(namespace, httpMethod, uri, path, bodyFormat, bodyContent string) (bool, error) {
59+
response, err := ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, bodyContent)
5960
if err != nil {
6061
return false, err
6162
}
@@ -73,8 +74,8 @@ func CheckHTTPResponseSuccessful(namespace string, response *http.Response) bool
7374
}
7475

7576
// IsHTTPResponseArraySize makes and checks whether an http request returns an array of a specific size
76-
func IsHTTPResponseArraySize(namespace, httpMethod, uri, path string, bodyFormat string, body io.Reader, arraySize int) (bool, error) {
77-
response, err := ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, body)
77+
func IsHTTPResponseArraySize(namespace, httpMethod, uri, path string, bodyFormat, bodyContent string, arraySize int) (bool, error) {
78+
response, err := ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, bodyContent)
7879
if err != nil {
7980
return false, err
8081
}
@@ -96,8 +97,8 @@ func IsHTTPResponseArraySize(namespace, httpMethod, uri, path string, bodyFormat
9697
}
9798

9899
// DoesHTTPResponseContain checks whether the response of an http request contains a certain string
99-
func DoesHTTPResponseContain(namespace, httpMethod, uri, path string, bodyFormat string, body io.Reader, responseContent string) (bool, error) {
100-
response, err := ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, body)
100+
func DoesHTTPResponseContain(namespace, httpMethod, uri, path string, bodyFormat, bodyContent string, responseContent string) (bool, error) {
101+
response, err := ExecuteHTTPRequest(namespace, httpMethod, uri, path, bodyFormat, bodyContent)
101102
if err != nil {
102103
return false, err
103104
}

bddframework/steps/http.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package steps
1616

1717
import (
1818
"fmt"
19-
"strings"
2019
"time"
2120

2221
"github.com/cucumber/godog"
@@ -38,7 +37,7 @@ func (data *Data) httpGetRequestOnServiceWithPathIsSuccessfulWithinMinutes(servi
3837
if err != nil {
3938
return err
4039
}
41-
return framework.WaitForSuccessfulHTTPRequest(data.Namespace, "GET", routeURI, path, "", nil, timeoutInMin)
40+
return framework.WaitForSuccessfulHTTPRequest(data.Namespace, "GET", routeURI, path, "", "", timeoutInMin)
4241
}
4342

4443
func (data *Data) httpPostRequestOnServiceWithPathAndBody(serviceName, path string, body *gherkin.DocString) error {
@@ -47,7 +46,7 @@ func (data *Data) httpPostRequestOnServiceWithPathAndBody(serviceName, path stri
4746
if err != nil {
4847
return err
4948
}
50-
if success, err := framework.IsHTTPRequestSuccessful(data.Namespace, "POST", routeURI, path, body.ContentType, strings.NewReader(body.Content)); err != nil {
49+
if success, err := framework.IsHTTPRequestSuccessful(data.Namespace, "POST", routeURI, path, body.ContentType, body.Content); err != nil {
5150
return err
5251
} else if !success {
5352
return fmt.Errorf("HTTP POST request to path %s was not successful", path)
@@ -61,7 +60,7 @@ func (data *Data) httpPostRequestOnServiceIsSuccessfulWithinMinutesWithPathAndBo
6160
if err != nil {
6261
return err
6362
}
64-
return framework.WaitForSuccessfulHTTPRequest(data.Namespace, "POST", routeURI, path, body.ContentType, strings.NewReader(body.Content), timeoutInMin)
63+
return framework.WaitForSuccessfulHTTPRequest(data.Namespace, "POST", routeURI, path, body.ContentType, body.Content, timeoutInMin)
6564
}
6665

6766
func (data *Data) httpGetRequestOnServiceWithPathShouldReturnAnArrayofSizeWithinMinutes(serviceName, path string, size, timeoutInMin int) error {
@@ -70,7 +69,7 @@ func (data *Data) httpGetRequestOnServiceWithPathShouldReturnAnArrayofSizeWithin
7069
return err
7170
}
7271
return framework.WaitFor(data.Namespace, fmt.Sprintf("GET request on path %s to return array of size %d", path, size), time.Duration(timeoutInMin)*time.Minute, func() (bool, error) {
73-
return framework.IsHTTPResponseArraySize(data.Namespace, "GET", routeURI, path, "", nil, size)
72+
return framework.IsHTTPResponseArraySize(data.Namespace, "GET", routeURI, path, "", "", size)
7473
})
7574
}
7675

@@ -81,6 +80,6 @@ func (data *Data) httpGetRequestOnServiceWithPathShouldContainAstringWithinMinut
8180
}
8281

8382
return framework.WaitFor(data.Namespace, fmt.Sprintf("GET request on path %s to return response content '%s'", path, responseContent), time.Duration(timeoutInMin)*time.Minute, func() (bool, error) {
84-
return framework.DoesHTTPResponseContain(data.Namespace, "GET", routeURI, path, "", nil, responseContent)
83+
return framework.DoesHTTPResponseContain(data.Namespace, "GET", routeURI, path, "", "", responseContent)
8584
})
8685
}

0 commit comments

Comments
 (0)