| Conditions | 10 |
| Total Lines | 74 |
| Code Lines | 52 |
| 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.TestURLCredentialsProvider_updateCredential 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 |
||
| 11 | func TestURLCredentialsProvider_updateCredential(t *testing.T) { |
||
| 12 | provider := newURLCredential("http://127.0.0.1") |
||
| 13 | |||
| 14 | origTestHookDo := hookDo |
||
| 15 | defer func() { hookDo = origTestHookDo }() |
||
| 16 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 17 | return func(req *http.Request) (*http.Response, error) { |
||
| 18 | return mockResponse(300, ``, errors.New("sdk test")) |
||
| 19 | } |
||
| 20 | } |
||
| 21 | |||
| 22 | cred, err := provider.GetCredential() |
||
| 23 | assert.NotNil(t, err) |
||
| 24 | assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error()) |
||
| 25 | assert.Nil(t, cred) |
||
| 26 | |||
| 27 | _, err = provider.GetAccessKeyId() |
||
| 28 | assert.NotNil(t, err) |
||
| 29 | assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error()) |
||
| 30 | |||
| 31 | _, err = provider.GetAccessKeySecret() |
||
| 32 | assert.NotNil(t, err) |
||
| 33 | assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error()) |
||
| 34 | |||
| 35 | _, err = provider.GetSecurityToken() |
||
| 36 | assert.NotNil(t, err) |
||
| 37 | assert.Equal(t, "get credentials from http://127.0.0.1 failed with error: sdk test", err.Error()) |
||
| 38 | |||
| 39 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 40 | return func(req *http.Request) (*http.Response, error) { |
||
| 41 | return mockResponse(200, `invalid json`, nil) |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | cred, err = provider.GetCredential() |
||
| 46 | assert.NotNil(t, err) |
||
| 47 | assert.Equal(t, "get credentials from http://127.0.0.1 failed with error, json unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
| 48 | assert.Nil(t, cred) |
||
| 49 | |||
| 50 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 51 | return func(req *http.Request) (*http.Response, error) { |
||
| 52 | return mockResponse(200, `{}`, nil) |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | cred, err = provider.GetCredential() |
||
| 57 | assert.NotNil(t, err) |
||
| 58 | assert.Equal(t, "get credentials failed: AccessKeyId: , AccessKeySecret: , SecurityToken: , Expiration: ", err.Error()) |
||
| 59 | assert.Nil(t, cred) |
||
| 60 | |||
| 61 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 62 | return func(req *http.Request) (*http.Response, error) { |
||
| 63 | return mockResponse(200, `{"AccessKeyId":"akid", "AccessKeySecret":"aksecret","SecurityToken":"sts","Expiration":"2006-01-02T15:04:05Z"}`, nil) |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | cred, err = provider.GetCredential() |
||
| 68 | assert.Nil(t, err) |
||
| 69 | assert.NotNil(t, cred) |
||
| 70 | assert.Equal(t, "akid", *cred.AccessKeyId) |
||
| 71 | assert.Equal(t, "aksecret", *cred.AccessKeySecret) |
||
| 72 | assert.Equal(t, "sts", *cred.SecurityToken) |
||
| 73 | |||
| 74 | akid, err := provider.GetAccessKeyId() |
||
| 75 | assert.Nil(t, err) |
||
| 76 | assert.Equal(t, "akid", *akid) |
||
| 77 | |||
| 78 | aksecret, err := provider.GetAccessKeySecret() |
||
| 79 | assert.Nil(t, err) |
||
| 80 | assert.Equal(t, "aksecret", *aksecret) |
||
| 81 | |||
| 82 | sts, err := provider.GetSecurityToken() |
||
| 83 | assert.Nil(t, err) |
||
| 84 | assert.Equal(t, "sts", *sts) |
||
| 85 | } |
||
| 96 |