| Conditions | 5 |
| Total Lines | 54 |
| Code Lines | 40 |
| 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:
| 1 | package providers |
||
| 130 | func TestURLCredentialsProvider_GetCredentials(t *testing.T) { |
||
| 131 | originHttpDo := httpDo |
||
| 132 | defer func() { httpDo = originHttpDo }() |
||
| 133 | |||
| 134 | // case 0: get previous credentials failed |
||
| 135 | p, err := NewURLCredentialsProviderBuilderBuilder(). |
||
| 136 | WithUrl("http://localhost:8080"). |
||
| 137 | Build() |
||
| 138 | assert.Nil(t, err) |
||
| 139 | |||
| 140 | // case 1: get credentials failed |
||
| 141 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 142 | err = errors.New("mock server error") |
||
| 143 | return |
||
| 144 | } |
||
| 145 | _, err = p.GetCredentials() |
||
| 146 | assert.NotNil(t, err) |
||
| 147 | assert.Equal(t, "mock server error", err.Error()) |
||
| 148 | |||
| 149 | // case 2: get invalid expiration |
||
| 150 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 151 | res = &httputil.Response{ |
||
| 152 | StatusCode: 200, |
||
| 153 | Body: []byte(`{"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"invalidexpiration","SecurityToken":"ststoken"}`), |
||
| 154 | } |
||
| 155 | return |
||
| 156 | } |
||
| 157 | _, err = p.GetCredentials() |
||
| 158 | assert.NotNil(t, err) |
||
| 159 | assert.Equal(t, "parsing time \"invalidexpiration\" as \"2006-01-02T15:04:05Z\": cannot parse \"invalidexpiration\" as \"2006\"", err.Error()) |
||
| 160 | |||
| 161 | // case 3: happy result |
||
| 162 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 163 | res = &httputil.Response{ |
||
| 164 | StatusCode: 200, |
||
| 165 | Body: []byte(`{"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"ststoken"}`), |
||
| 166 | } |
||
| 167 | return |
||
| 168 | } |
||
| 169 | cc, err := p.GetCredentials() |
||
| 170 | assert.Nil(t, err) |
||
| 171 | assert.Equal(t, "akid", cc.AccessKeyId) |
||
| 172 | assert.Equal(t, "aksecret", cc.AccessKeySecret) |
||
| 173 | assert.Equal(t, "ststoken", cc.SecurityToken) |
||
| 174 | assert.Equal(t, "credential_uri", cc.ProviderName) |
||
| 175 | assert.True(t, p.needUpdateCredential()) |
||
| 176 | // get credentials again |
||
| 177 | cc, err = p.GetCredentials() |
||
| 178 | assert.Nil(t, err) |
||
| 179 | assert.Equal(t, "akid", cc.AccessKeyId) |
||
| 180 | assert.Equal(t, "aksecret", cc.AccessKeySecret) |
||
| 181 | assert.Equal(t, "ststoken", cc.SecurityToken) |
||
| 182 | assert.Equal(t, "credential_uri", cc.ProviderName) |
||
| 183 | assert.True(t, p.needUpdateCredential()) |
||
| 184 | } |
||
| 200 |