6
6
"testing"
7
7
8
8
"github.com/stretchr/testify/assert"
9
+ "github.com/stretchr/testify/require"
9
10
)
10
11
11
12
type testEnv struct {
@@ -39,6 +40,9 @@ func TestClient_Base(t *testing.T) {
39
40
env .Mux .HandleFunc ("/ok" , func (w http.ResponseWriter , r * http.Request ) {
40
41
w .Write ([]byte ("ok" ))
41
42
})
43
+ env .Mux .HandleFunc ("/sanitized" , func (w http.ResponseWriter , r * http.Request ) {
44
+ w .Write ([]byte ("sanitized \n " ))
45
+ })
42
46
env .Mux .HandleFunc ("/not-found" , func (w http.ResponseWriter , r * http.Request ) {
43
47
w .WriteHeader (404 )
44
48
w .Write ([]byte ("not found" ))
@@ -47,6 +51,9 @@ func TestClient_Base(t *testing.T) {
47
51
if body , err := env .Client .get ("/ok" ); assert .NoError (t , err ) {
48
52
assert .Equal (t , "ok" , body )
49
53
}
54
+ if body , err := env .Client .get ("/sanitized" ); assert .NoError (t , err ) {
55
+ assert .Equal (t , "sanitized" , body )
56
+ }
50
57
if body , err := env .Client .get ("/not-found" ); assert .EqualError (t , err , "response status was 404" ) {
51
58
assert .Equal (t , "not found" , body )
52
59
}
@@ -99,33 +106,51 @@ func TestClient_Hostname(t *testing.T) {
99
106
}
100
107
101
108
func TestClient_InstanceID (t * testing.T ) {
102
- env := newTestEnv ()
103
- env .Mux .HandleFunc ("/instance-id" , func (w http.ResponseWriter , r * http.Request ) {
104
- w .Write ([]byte ("123456" ))
109
+ t .Run ("success" , func (t * testing.T ) {
110
+ env := newTestEnv ()
111
+ env .Mux .HandleFunc ("/instance-id" , func (w http.ResponseWriter , r * http.Request ) {
112
+ w .Write ([]byte ("123456" ))
113
+ })
114
+
115
+ instanceID , err := env .Client .InstanceID ()
116
+ require .NoError (t , err )
117
+ require .Equal (t , int64 (123456 ), instanceID )
105
118
})
106
119
107
- instanceID , err := env .Client .InstanceID ()
108
- if err != nil {
109
- t .Fatal (err )
110
- }
111
- if instanceID != 123456 {
112
- t .Fatalf ("Unexpected instanceID %d" , instanceID )
113
- }
120
+ t .Run ("success sanitized" , func (t * testing.T ) {
121
+ env := newTestEnv ()
122
+ env .Mux .HandleFunc ("/instance-id" , func (w http.ResponseWriter , r * http.Request ) {
123
+ w .Write ([]byte ("123456\n " ))
124
+ })
125
+
126
+ instanceID , err := env .Client .InstanceID ()
127
+ require .NoError (t , err )
128
+ require .Equal (t , int64 (123456 ), instanceID )
129
+ })
114
130
}
115
131
116
132
func TestClient_PublicIPv4 (t * testing.T ) {
117
- env := newTestEnv ()
118
- env .Mux .HandleFunc ("/public-ipv4" , func (w http.ResponseWriter , r * http.Request ) {
119
- w .Write ([]byte ("127.0.0.1" ))
133
+ t .Run ("success" , func (t * testing.T ) {
134
+ env := newTestEnv ()
135
+ env .Mux .HandleFunc ("/public-ipv4" , func (w http.ResponseWriter , r * http.Request ) {
136
+ w .Write ([]byte ("127.0.0.1" ))
137
+ })
138
+
139
+ publicIPv4 , err := env .Client .PublicIPv4 ()
140
+ require .NoError (t , err )
141
+ require .Equal (t , "127.0.0.1" , publicIPv4 .String ())
120
142
})
121
143
122
- publicIPv4 , err := env .Client .PublicIPv4 ()
123
- if err != nil {
124
- t .Fatal (err )
125
- }
126
- if publicIPv4 .String () != "127.0.0.1" {
127
- t .Fatalf ("Unexpected PublicIPv4 %s" , publicIPv4 .String ())
128
- }
144
+ t .Run ("success sanitized" , func (t * testing.T ) {
145
+ env := newTestEnv ()
146
+ env .Mux .HandleFunc ("/public-ipv4" , func (w http.ResponseWriter , r * http.Request ) {
147
+ w .Write ([]byte ("127.0.0.1\n " ))
148
+ })
149
+
150
+ publicIPv4 , err := env .Client .PublicIPv4 ()
151
+ require .NoError (t , err )
152
+ require .Equal (t , "127.0.0.1" , publicIPv4 .String ())
153
+ })
129
154
}
130
155
131
156
func TestClient_Region (t * testing.T ) {
0 commit comments