| Conditions | 20 |
| Total Lines | 133 |
| Code Lines | 98 |
| 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_EcsRAmRoleCredential 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 Test_EcsRAmRoleCredential(t *testing.T) { |
||
| 12 | credentialUpdater := new(credentialUpdater) |
||
| 13 | credentialUpdater.inAdvanceScale = 0.5 |
||
| 14 | auth := &ECSRAMRoleCredentialsProvider{ |
||
| 15 | RoleName: "go sdk", |
||
| 16 | credentialUpdater: credentialUpdater, |
||
| 17 | runtime: nil, |
||
| 18 | } |
||
| 19 | origTestHookDo := hookDo |
||
| 20 | defer func() { hookDo = origTestHookDo }() |
||
| 21 | |||
| 22 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 23 | return func(req *http.Request) (*http.Response, error) { |
||
| 24 | return mockResponse(300, ``, errors.New("sdk test")) |
||
| 25 | } |
||
| 26 | } |
||
| 27 | accesskeyId, err := auth.GetAccessKeyId() |
||
| 28 | assert.NotNil(t, err) |
||
| 29 | assert.Equal(t, "refresh Ecs sts token err: sdk test", err.Error()) |
||
| 30 | assert.Nil(t, accesskeyId) |
||
| 31 | |||
| 32 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 33 | return func(req *http.Request) (*http.Response, error) { |
||
| 34 | return mockResponse(300, ``, nil) |
||
| 35 | } |
||
| 36 | } |
||
| 37 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 38 | assert.NotNil(t, err) |
||
| 39 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 300, message = ", err.Error()) |
||
| 40 | assert.Nil(t, accesskeyId) |
||
| 41 | |||
| 42 | accesskeySecret, err := auth.GetAccessKeySecret() |
||
| 43 | assert.NotNil(t, err) |
||
| 44 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 300, message = ", err.Error()) |
||
| 45 | assert.Nil(t, accesskeySecret) |
||
| 46 | |||
| 47 | ststoken, err := auth.GetSecurityToken() |
||
| 48 | assert.NotNil(t, err) |
||
| 49 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 300, message = ", err.Error()) |
||
| 50 | assert.Nil(t, ststoken) |
||
| 51 | |||
| 52 | assert.Equal(t, "", *auth.GetBearerToken()) |
||
| 53 | |||
| 54 | assert.Equal(t, "ecs_ram_role", *auth.GetType()) |
||
| 55 | |||
| 56 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 57 | return func(req *http.Request) (*http.Response, error) { |
||
| 58 | return mockResponse(400, `role`, nil) |
||
| 59 | } |
||
| 60 | } |
||
| 61 | auth.RoleName = "" |
||
| 62 | _, err = auth.GetAccessKeyId() |
||
| 63 | assert.NotNil(t, err) |
||
| 64 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 400, message = role", err.Error()) |
||
| 65 | |||
| 66 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 67 | return func(req *http.Request) (*http.Response, error) { |
||
| 68 | return mockResponse(200, `role`, nil) |
||
| 69 | } |
||
| 70 | } |
||
| 71 | _, err = auth.GetAccessKeyId() |
||
| 72 | assert.NotNil(t, err) |
||
| 73 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character 'r' looking for beginning of value", err.Error()) |
||
| 74 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 75 | return func(req *http.Request) (*http.Response, error) { |
||
| 76 | return mockResponse(200, `"AccessKeyId":"accessKeyId","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"`, nil) |
||
| 77 | } |
||
| 78 | } |
||
| 79 | auth.RoleName = "role" |
||
| 80 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 81 | assert.NotNil(t, err) |
||
| 82 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character ':' after top-level value", err.Error()) |
||
| 83 | assert.Nil(t, accesskeyId) |
||
| 84 | |||
| 85 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 86 | return func(req *http.Request) (*http.Response, error) { |
||
| 87 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"fail"}`, nil) |
||
| 88 | } |
||
| 89 | } |
||
| 90 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 91 | assert.NotNil(t, err) |
||
| 92 | assert.Equal(t, "refresh Ecs sts token err: Code is not Success", err.Error()) |
||
| 93 | assert.Nil(t, accesskeyId) |
||
| 94 | |||
| 95 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 96 | return func(req *http.Request) (*http.Response, error) { |
||
| 97 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"Success"}`, nil) |
||
| 98 | } |
||
| 99 | } |
||
| 100 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 101 | assert.NotNil(t, err) |
||
| 102 | assert.Equal(t, "refresh Ecs sts token err: AccessKeyId: , AccessKeySecret: accessKeySecret, SecurityToken: securitytoken, Expiration: expiration", err.Error()) |
||
| 103 | assert.Nil(t, accesskeyId) |
||
| 104 | |||
| 105 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 106 | return func(req *http.Request) (*http.Response, error) { |
||
| 107 | return mockResponse(200, `{"AccessKeyId":"accessKeyId","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"2018-01-02T15:04:05Z","Code":"Success"}`, nil) |
||
| 108 | } |
||
| 109 | } |
||
| 110 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 111 | assert.Nil(t, err) |
||
| 112 | assert.Equal(t, "accessKeyId", *accesskeyId) |
||
| 113 | |||
| 114 | accesskeySecret, err = auth.GetAccessKeySecret() |
||
| 115 | assert.Nil(t, err) |
||
| 116 | assert.Equal(t, "accessKeySecret", *accesskeySecret) |
||
| 117 | |||
| 118 | ststoken, err = auth.GetSecurityToken() |
||
| 119 | assert.Nil(t, err) |
||
| 120 | assert.Equal(t, "securitytoken", *ststoken) |
||
| 121 | |||
| 122 | err = errors.New("credentials") |
||
| 123 | err = hookParse(err) |
||
| 124 | assert.Equal(t, "credentials", err.Error()) |
||
| 125 | |||
| 126 | cred, err := auth.GetCredential() |
||
| 127 | assert.Nil(t, err) |
||
| 128 | assert.Equal(t, "accessKeyId", *cred.AccessKeyId) |
||
| 129 | assert.Equal(t, "accessKeySecret", *cred.AccessKeySecret) |
||
| 130 | assert.Equal(t, "securitytoken", *cred.SecurityToken) |
||
| 131 | assert.Nil(t, cred.BearerToken) |
||
| 132 | assert.Equal(t, "ecs_ram_role", *cred.Type) |
||
| 133 | |||
| 134 | originHookParse := hookParse |
||
| 135 | hookParse = func(err error) error { |
||
| 136 | return errors.New("error parse") |
||
| 137 | } |
||
| 138 | defer func() { |
||
| 139 | hookParse = originHookParse |
||
| 140 | }() |
||
| 141 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 142 | assert.Equal(t, "refresh Ecs sts token err: error parse", err.Error()) |
||
| 143 | assert.Nil(t, accesskeyId) |
||
| 144 | } |
||
| 272 |