-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks_common_test.go
108 lines (91 loc) · 2.55 KB
/
mocks_common_test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package awsmocker_test
import (
"context"
"crypto/tls"
"io"
"net/http"
"net/url"
"testing"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/stretchr/testify/require"
"github.com/webdestroya/awsmocker"
)
func TestMockResponse_Error(t *testing.T) {
info := awsmocker.Start(t, &awsmocker.MockerOptions{
SkipDefaultMocks: true,
Mocks: []*awsmocker.MockedEndpoint{
{
Request: &awsmocker.MockedRequest{
Hostname: "test.com",
Path: "/err400",
},
Response: awsmocker.MockResponse_Error(400, "SomeCode", "SomeMessage"),
},
{
Request: &awsmocker.MockedRequest{
Hostname: "test.com",
Path: "/err401",
},
Response: awsmocker.MockResponse_Error(401, "SomeCode2", "SomeMessage"),
},
{
Request: &awsmocker.MockedRequest{
Hostname: "test.com",
Path: "/err0",
},
Response: awsmocker.MockResponse_Error(0, "SomeCode0", "SomeMessage"),
},
},
})
client := &http.Client{
Transport: &http.Transport{
Proxy: func(r *http.Request) (*url.URL, error) {
return url.Parse(info.ProxyURL)
},
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
tables := []struct {
method string
url string
statusCode int
expErrorCode string
}{
{http.MethodGet, "https://test.com/err400", 400, "SomeCode"},
{http.MethodGet, "https://test.com/err401", 401, "SomeCode2"},
{http.MethodGet, "https://test.com/err0", 400, "SomeCode0"},
}
for _, table := range tables {
req, err := http.NewRequest(table.method, table.url, nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, table.statusCode, resp.StatusCode)
respBodyRaw, err := io.ReadAll(resp.Body)
require.NoError(t, err)
respBody := string(respBodyRaw)
if table.expErrorCode != "" {
require.Contains(t, respBody, table.expErrorCode)
}
}
}
func TestMock_Failure(t *testing.T) {
info := awsmocker.Start(t, &awsmocker.MockerOptions{
SkipDefaultMocks: true,
ReturnAwsConfig: true,
Mocks: []*awsmocker.MockedEndpoint{
awsmocker.Mock_Failure("ecs", "ListClusters"),
awsmocker.Mock_Failure_WithCode(403, "ecs", "ListServices", "SomeCode", "SomeMessage"),
},
})
ecsClient := ecs.NewFromConfig(*info.AwsConfig)
_, err := ecsClient.ListClusters(context.TODO(), &ecs.ListClustersInput{})
require.Error(t, err)
require.ErrorContains(t, err, "AccessDenied")
_, err = ecsClient.ListServices(context.TODO(), &ecs.ListServicesInput{})
require.Error(t, err)
require.ErrorContains(t, err, "SomeCode")
}