-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_helpers.go
59 lines (47 loc) · 1.36 KB
/
test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package client
import (
"io"
"net/http"
"net/url"
"os"
"strings"
"testing"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
)
type roundTripFunc func(r *http.Request) (*http.Response, error)
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return s(r)
}
// GetFakeOctopusClient -
func GetFakeOctopusClient(t *testing.T, apiPath string, statusCode int, responseBody string) (*Client, error) {
httpClient := http.Client{}
httpClient.Transport = roundTripFunc(func(request *http.Request) (*http.Response, error) {
if request.URL.Path == "/api" {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(constants.ApiReplyRoot)),
}, nil
}
return &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(strings.NewReader(responseBody)),
}, nil
})
host := os.Getenv(constants.EnvironmentVariableOctopusHost)
apiKey := os.Getenv(constants.EnvironmentVariableOctopusApiKey)
if len(host) == 0 {
host = os.Getenv(constants.ClientURLEnvironmentVariable)
}
if len(apiKey) == 0 {
apiKey = os.Getenv(constants.ClientAPIKeyEnvironmentVariable)
}
url, err := url.Parse(host)
if err != nil {
return nil, err
}
octopusClient, err := NewClientForTool(&httpClient, url, apiKey, "", "test")
if err != nil {
return nil, err
}
return octopusClient, nil
}