| Conditions | 11 |
| Total Lines | 67 |
| Code Lines | 51 |
| 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 providers.TestCloudSSOCredentialsProviderGetCredentials 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 providers |
||
| 159 | func TestCloudSSOCredentialsProviderGetCredentials(t *testing.T) { |
||
| 160 | |||
| 161 | p, err := NewCloudSSOCredentialsProviderBuilder(). |
||
| 162 | WithSignInUrl("https://signin-cn-shanghai.alibabacloudsso.com/a/login"). |
||
| 163 | WithAccountId("uid"). |
||
| 164 | WithAccessConfig("config"). |
||
| 165 | WithAccessToken("token"). |
||
| 166 | WithAccessTokenExpire(time.Now().Unix() + 1000). |
||
| 167 | WithHttpOptions(&HttpOptions{ |
||
| 168 | ConnectTimeout: 10000, |
||
| 169 | }). |
||
| 170 | Build() |
||
| 171 | |||
| 172 | assert.Nil(t, err) |
||
| 173 | assert.Equal(t, 10000, p.httpOptions.ConnectTimeout) |
||
| 174 | _, err = p.GetCredentials() |
||
| 175 | assert.NotNil(t, err) |
||
| 176 | // Network-dependent test: accept expected error or any network-related error |
||
| 177 | errMsg := err.Error() |
||
| 178 | validError := contains(errMsg, "InvalidParameter.AccountId.InvalidChars") || |
||
| 179 | contains(errMsg, "timeout") || |
||
| 180 | contains(errMsg, "TLS handshake") || |
||
| 181 | contains(errMsg, "dial tcp") || |
||
| 182 | contains(errMsg, "lookup") || |
||
| 183 | contains(errMsg, "connection refused") || |
||
| 184 | contains(errMsg, "no such host") |
||
| 185 | assert.True(t, validError, "Expected error about invalid account ID or network error, got: %s", errMsg) |
||
| 186 | |||
| 187 | originHttpDo := httpDo |
||
| 188 | defer func() { httpDo = originHttpDo }() |
||
| 189 | |||
| 190 | // case 1: mock new http request failed |
||
| 191 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 192 | err = errors.New("mock server error") |
||
| 193 | return |
||
| 194 | } |
||
| 195 | _, err = p.GetCredentials() |
||
| 196 | assert.NotNil(t, err) |
||
| 197 | assert.Equal(t, "mock server error", err.Error()) |
||
| 198 | |||
| 199 | // case 2: get invalid expiration |
||
| 200 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 201 | res = &httputil.Response{ |
||
| 202 | StatusCode: 200, |
||
| 203 | Body: []byte(`{"CloudCredential": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"invalidexpiration","SecurityToken":"ststoken"}}`), |
||
| 204 | } |
||
| 205 | return |
||
| 206 | } |
||
| 207 | _, err = p.GetCredentials() |
||
| 208 | assert.NotNil(t, err) |
||
| 209 | assert.Equal(t, "parsing time \"invalidexpiration\" as \"2006-01-02T15:04:05Z\": cannot parse \"invalidexpiration\" as \"2006\"", err.Error()) |
||
| 210 | |||
| 211 | // case 3: happy result |
||
| 212 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 213 | res = &httputil.Response{ |
||
| 214 | StatusCode: 200, |
||
| 215 | Body: []byte(`{"CloudCredential": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"ststoken"}}`), |
||
| 216 | } |
||
| 217 | return |
||
| 218 | } |
||
| 219 | cc, err := p.GetCredentials() |
||
| 220 | assert.Nil(t, err) |
||
| 221 | assert.Equal(t, "akid", cc.AccessKeyId) |
||
| 222 | assert.Equal(t, "aksecret", cc.AccessKeySecret) |
||
| 223 | assert.Equal(t, "ststoken", cc.SecurityToken) |
||
| 224 | assert.Equal(t, "cloud_sso", cc.ProviderName) |
||
| 225 | assert.True(t, p.needUpdateCredential()) |
||
| 226 | } |
||
| 247 |