1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

Improve HTTP test to cover more retry capabilities (#2528)

* FF disable fulltextsearch

# Conflicts:
#	pkg/fortify/fortify.go
#	pkg/fortify/fortify_test.go

* Improve test on HTTP retry capabilities

* Improve HTTP retry test
This commit is contained in:
Sven Merk 2021-01-22 09:56:56 +01:00 committed by GitHub
parent f149292374
commit 3e0a2835fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package http
import (
"bytes"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
@ -14,11 +15,9 @@ import (
"time"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"encoding/base64"
"github.com/sirupsen/logrus"
)
func TestSendRequest(t *testing.T) {
@ -325,10 +324,18 @@ func TestMaxRetries(t *testing.T) {
testCases := []struct {
client Client
countedCalls int
method string
responseCode int
errorText string
}{
{client: Client{maxRetries: 0}, countedCalls: 1},
{client: Client{maxRetries: 2}, countedCalls: 3},
{client: Client{maxRetries: 3}, countedCalls: 4},
{client: Client{maxRetries: 0}, countedCalls: 1, method: http.MethodGet, responseCode: 500, errorText: "Internal Server Error"},
{client: Client{maxRetries: 2}, countedCalls: 3, method: http.MethodGet, responseCode: 500, errorText: "Internal Server Error"},
{client: Client{maxRetries: 3}, countedCalls: 4, method: http.MethodPost, responseCode: 503, errorText: "Service Unavailable"},
{client: Client{maxRetries: 3}, countedCalls: 4, method: http.MethodPut, responseCode: 506, errorText: "Variant Also Negotiates"},
{client: Client{maxRetries: 3}, countedCalls: 4, method: http.MethodHead, responseCode: 502, errorText: "Bad Gateway"},
{client: Client{maxRetries: 3}, countedCalls: 1, method: http.MethodHead, responseCode: 404, errorText: "Not Found"},
{client: Client{maxRetries: 3}, countedCalls: 1, method: http.MethodHead, responseCode: 401, errorText: "Authentication Error"},
{client: Client{maxRetries: 3}, countedCalls: 1, method: http.MethodHead, responseCode: 403, errorText: "Authorization Error"},
}
for _, testCase := range testCases {
@ -336,14 +343,14 @@ func TestMaxRetries(t *testing.T) {
count := 0
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count++
w.WriteHeader(500)
w.WriteHeader(testCase.responseCode)
}))
defer svr.Close()
// test
_, err := testCase.client.SendRequest(http.MethodGet, svr.URL, &bytes.Buffer{}, nil, nil)
_, err := testCase.client.SendRequest(testCase.method, svr.URL, &bytes.Buffer{}, nil, nil)
// assert
assert.Error(t, err)
assert.Equal(t, testCase.countedCalls, count)
assert.Error(t, err, fmt.Sprintf("%v: %v", testCase.errorText, "Expected error but did not detect one"))
assert.Equal(t, testCase.countedCalls, count, fmt.Sprintf("%v: %v", testCase.errorText, "Number of invocations mismatch"))
}
}