-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_test.go
More file actions
78 lines (64 loc) · 1.75 KB
/
response_test.go
File metadata and controls
78 lines (64 loc) · 1.75 KB
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
package awsmocker_test
import (
"crypto/tls"
"io"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"github.com/webdestroya/awsmocker"
)
func TestResponseDebugLogging(t *testing.T) {
info := awsmocker.Start(t, awsmocker.WithoutDefaultMocks(),
awsmocker.WithMocks(&awsmocker.MockedEndpoint{
Request: &awsmocker.MockedRequest{
Hostname: "httptest.com",
},
Response: awsmocker.MockResponse_Error(400, "SomeCode_HTTP", "SomeMessage"),
}),
awsmocker.WithMocks(&awsmocker.MockedEndpoint{
Request: &awsmocker.MockedRequest{
Hostname: "httpstest.com",
},
Response: awsmocker.MockResponse_Error(401, "SomeCode_HTTPS", "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, "http://httptest.com/err400", 400, "SomeCode_HTTP"},
{http.MethodGet, "https://httpstest.com/err401", 401, "SomeCode_HTTPS"},
}
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()
// dump, err := httputil.DumpResponse(resp, true)
// if err != nil {
// t.Error(err)
// }
// fmt.Printf("RECEIVED:\n%s\n", dump)
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)
}
}
}