Conditions | 20 |
Total Lines | 125 |
Code Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like credentials.Test_EcsRAmRoleCredentialEnableIMDSv2 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package credentials |
||
146 | func Test_EcsRAmRoleCredentialEnableIMDSv2(t *testing.T) { |
||
147 | auth := newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", false, 0, 0.5, nil) |
||
148 | origTestHookDo := hookDo |
||
149 | defer func() { hookDo = origTestHookDo }() |
||
150 | |||
151 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
152 | return func(req *http.Request) (*http.Response, error) { |
||
153 | return mockResponse(300, ``, errors.New("sdk test")) |
||
154 | } |
||
155 | } |
||
156 | accesskeyId, err := auth.GetAccessKeyId() |
||
157 | assert.NotNil(t, err) |
||
158 | assert.Equal(t, "refresh Ecs sts token err: sdk test", err.Error()) |
||
159 | assert.Nil(t, accesskeyId) |
||
160 | |||
161 | auth = newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", true, 0, 0.5, nil) |
||
162 | accesskeyId, err = auth.GetAccessKeyId() |
||
163 | assert.NotNil(t, err) |
||
164 | assert.Equal(t, "failed to get token from ECS Metadata Service: sdk test", err.Error()) |
||
165 | assert.Nil(t, accesskeyId) |
||
166 | |||
167 | auth = newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", true, 180, 0.5, nil) |
||
168 | accesskeyId, err = auth.GetAccessKeyId() |
||
169 | assert.NotNil(t, err) |
||
170 | assert.Equal(t, "failed to get token from ECS Metadata Service: sdk test", err.Error()) |
||
171 | assert.Nil(t, accesskeyId) |
||
172 | |||
173 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
174 | return func(req *http.Request) (*http.Response, error) { |
||
175 | return mockResponse(300, ``, nil) |
||
176 | } |
||
177 | } |
||
178 | accesskeyId, err = auth.GetAccessKeyId() |
||
179 | assert.NotNil(t, err) |
||
180 | assert.Equal(t, "failed to get token from ECS Metadata Service: httpStatus: 300, message = ", err.Error()) |
||
181 | assert.Nil(t, accesskeyId) |
||
182 | |||
183 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
184 | return func(req *http.Request) (*http.Response, error) { |
||
185 | return mockResponse(400, `role`, nil) |
||
186 | } |
||
187 | } |
||
188 | auth.RoleName = "" |
||
189 | _, err = auth.GetAccessKeyId() |
||
190 | assert.NotNil(t, err) |
||
191 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 400, message = role", err.Error()) |
||
192 | |||
193 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
194 | return func(req *http.Request) (*http.Response, error) { |
||
195 | return mockResponse(200, `role`, nil) |
||
196 | } |
||
197 | } |
||
198 | _, err = auth.GetAccessKeyId() |
||
199 | assert.NotNil(t, err) |
||
200 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character 'r' looking for beginning of value", err.Error()) |
||
201 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
202 | return func(req *http.Request) (*http.Response, error) { |
||
203 | return mockResponse(200, `"AccessKeyId":"accessKeyId","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"`, nil) |
||
204 | } |
||
205 | } |
||
206 | auth.RoleName = "role" |
||
207 | accesskeyId, err = auth.GetAccessKeyId() |
||
208 | assert.NotNil(t, err) |
||
209 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character ':' after top-level value", err.Error()) |
||
210 | assert.Nil(t, accesskeyId) |
||
211 | |||
212 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
213 | return func(req *http.Request) (*http.Response, error) { |
||
214 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"fail"}`, nil) |
||
215 | } |
||
216 | } |
||
217 | accesskeyId, err = auth.GetAccessKeyId() |
||
218 | assert.NotNil(t, err) |
||
219 | assert.Equal(t, "refresh Ecs sts token err: Code is not Success", err.Error()) |
||
220 | assert.Nil(t, accesskeyId) |
||
221 | |||
222 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
223 | return func(req *http.Request) (*http.Response, error) { |
||
224 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"Success"}`, nil) |
||
225 | } |
||
226 | } |
||
227 | accesskeyId, err = auth.GetAccessKeyId() |
||
228 | assert.NotNil(t, err) |
||
229 | assert.Equal(t, "refresh Ecs sts token err: AccessKeyId: , AccessKeySecret: accessKeySecret, SecurityToken: securitytoken, Expiration: expiration", err.Error()) |
||
230 | assert.Nil(t, accesskeyId) |
||
231 | |||
232 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
233 | return func(req *http.Request) (*http.Response, error) { |
||
234 | return mockResponse(200, `{"AccessKeyId":"accessKeyId","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"2018-01-02T15:04:05Z","Code":"Success"}`, nil) |
||
235 | } |
||
236 | } |
||
237 | accesskeyId, err = auth.GetAccessKeyId() |
||
238 | assert.Nil(t, err) |
||
239 | assert.Equal(t, "accessKeyId", *accesskeyId) |
||
240 | |||
241 | accesskeySecret, err := auth.GetAccessKeySecret() |
||
242 | assert.Nil(t, err) |
||
243 | assert.Equal(t, "accessKeySecret", *accesskeySecret) |
||
244 | |||
245 | ststoken, err := auth.GetSecurityToken() |
||
246 | assert.Nil(t, err) |
||
247 | assert.Equal(t, "securitytoken", *ststoken) |
||
248 | |||
249 | err = errors.New("credentials") |
||
250 | err = hookParse(err) |
||
251 | assert.Equal(t, "credentials", err.Error()) |
||
252 | |||
253 | cred, err := auth.GetCredential() |
||
254 | assert.Nil(t, err) |
||
255 | assert.Equal(t, "accessKeyId", *cred.AccessKeyId) |
||
256 | assert.Equal(t, "accessKeySecret", *cred.AccessKeySecret) |
||
257 | assert.Equal(t, "securitytoken", *cred.SecurityToken) |
||
258 | assert.Nil(t, cred.BearerToken) |
||
259 | assert.Equal(t, "ecs_ram_role", *cred.Type) |
||
260 | |||
261 | originHookParse := hookParse |
||
262 | hookParse = func(err error) error { |
||
263 | return errors.New("error parse") |
||
264 | } |
||
265 | defer func() { |
||
266 | hookParse = originHookParse |
||
267 | }() |
||
268 | accesskeyId, err = auth.GetAccessKeyId() |
||
269 | assert.Equal(t, "refresh Ecs sts token err: error parse", err.Error()) |
||
270 | assert.Nil(t, accesskeyId) |
||
271 | } |
||
272 |