| Conditions | 19 |
| Total Lines | 130 |
| Code Lines | 90 |
| 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.TestRAMRoleARNCredentialsProvider_getCredentials 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 |
||
| 83 | func TestRAMRoleARNCredentialsProvider_getCredentials(t *testing.T) { |
||
| 84 | akProvider, err := NewStaticAKCredentialsProviderBuilder(). |
||
| 85 | WithAccessKeyId("akid"). |
||
| 86 | WithAccessKeySecret("aksecret"). |
||
| 87 | Build() |
||
| 88 | assert.Nil(t, err) |
||
| 89 | p, err := NewRAMRoleARNCredentialsProviderBuilder(). |
||
| 90 | WithCredentialsProvider(akProvider). |
||
| 91 | WithRoleArn("roleArn"). |
||
| 92 | WithRoleSessionName("rsn"). |
||
| 93 | WithDurationSeconds(1000). |
||
| 94 | Build() |
||
| 95 | assert.Nil(t, err) |
||
| 96 | |||
| 97 | cc, err := akProvider.GetCredentials() |
||
| 98 | assert.Nil(t, err) |
||
| 99 | |||
| 100 | originNewRequest := hookNewRequest |
||
| 101 | defer func() { hookNewRequest = originNewRequest }() |
||
| 102 | |||
| 103 | // case 1: mock new http request failed |
||
| 104 | hookNewRequest = func(fn newReuqest) newReuqest { |
||
| 105 | return func(method, url string, body io.Reader) (*http.Request, error) { |
||
| 106 | return nil, errors.New("new http request failed") |
||
| 107 | } |
||
| 108 | } |
||
| 109 | _, err = p.getCredentials(cc) |
||
| 110 | assert.NotNil(t, err) |
||
| 111 | assert.Equal(t, "new http request failed", err.Error()) |
||
| 112 | // reset new request |
||
| 113 | hookNewRequest = originNewRequest |
||
| 114 | |||
| 115 | originDo := hookDo |
||
| 116 | defer func() { hookDo = originDo }() |
||
| 117 | |||
| 118 | // case 2: server error |
||
| 119 | hookDo = func(fn do) do { |
||
| 120 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 121 | err = errors.New("mock server error") |
||
| 122 | return |
||
| 123 | } |
||
| 124 | } |
||
| 125 | _, err = p.getCredentials(cc) |
||
| 126 | assert.NotNil(t, err) |
||
| 127 | assert.Equal(t, "mock server error", err.Error()) |
||
| 128 | |||
| 129 | // case 3: mock read response error |
||
| 130 | hookDo = func(fn do) do { |
||
| 131 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 132 | status := strconv.Itoa(200) |
||
| 133 | res = &http.Response{ |
||
| 134 | Proto: "HTTP/1.1", |
||
| 135 | ProtoMajor: 1, |
||
| 136 | Header: map[string][]string{}, |
||
| 137 | StatusCode: 200, |
||
| 138 | Status: status + " " + http.StatusText(200), |
||
| 139 | } |
||
| 140 | res.Body = ioutil.NopCloser(&errorReader{}) |
||
| 141 | return |
||
| 142 | } |
||
| 143 | } |
||
| 144 | _, err = p.getCredentials(cc) |
||
| 145 | assert.NotNil(t, err) |
||
| 146 | assert.Equal(t, "read failed", err.Error()) |
||
| 147 | |||
| 148 | // case 4: 4xx error |
||
| 149 | hookDo = func(fn do) do { |
||
| 150 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 151 | res = mockResponse(400, "4xx error") |
||
| 152 | return |
||
| 153 | } |
||
| 154 | } |
||
| 155 | _, err = p.getCredentials(cc) |
||
| 156 | assert.NotNil(t, err) |
||
| 157 | assert.Equal(t, "refresh session token failed: 4xx error", err.Error()) |
||
| 158 | |||
| 159 | // case 5: invalid json |
||
| 160 | hookDo = func(fn do) do { |
||
| 161 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 162 | res = mockResponse(200, "invalid json") |
||
| 163 | return |
||
| 164 | } |
||
| 165 | } |
||
| 166 | _, err = p.getCredentials(cc) |
||
| 167 | assert.NotNil(t, err) |
||
| 168 | assert.Equal(t, "refresh RoleArn sts token err, json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
| 169 | |||
| 170 | // case 6: empty response json |
||
| 171 | hookDo = func(fn do) do { |
||
| 172 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 173 | res = mockResponse(200, "null") |
||
| 174 | return |
||
| 175 | } |
||
| 176 | } |
||
| 177 | _, err = p.getCredentials(cc) |
||
| 178 | assert.NotNil(t, err) |
||
| 179 | assert.Equal(t, "refresh RoleArn sts token err, fail to get credentials", err.Error()) |
||
| 180 | |||
| 181 | // case 7: empty session ak response json |
||
| 182 | hookDo = func(fn do) do { |
||
| 183 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 184 | res = mockResponse(200, `{"Credentials": {}}`) |
||
| 185 | return |
||
| 186 | } |
||
| 187 | } |
||
| 188 | _, err = p.getCredentials(cc) |
||
| 189 | assert.NotNil(t, err) |
||
| 190 | assert.Equal(t, "refresh RoleArn sts token err, fail to get credentials", err.Error()) |
||
| 191 | |||
| 192 | // case 8: mock ok value |
||
| 193 | hookDo = func(fn do) do { |
||
| 194 | return func(req *http.Request) (res *http.Response, err error) { |
||
| 195 | res = mockResponse(200, `{"Credentials": {"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token"}}`) |
||
| 196 | return |
||
| 197 | } |
||
| 198 | } |
||
| 199 | creds, err := p.getCredentials(cc) |
||
| 200 | assert.Nil(t, err) |
||
| 201 | assert.Equal(t, "saki", creds.AccessKeyId) |
||
| 202 | assert.Equal(t, "saks", creds.AccessKeySecret) |
||
| 203 | assert.Equal(t, "token", creds.SecurityToken) |
||
| 204 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) |
||
| 205 | |||
| 206 | // needUpdateCredential |
||
| 207 | assert.True(t, p.needUpdateCredential()) |
||
| 208 | p.expirationTimestamp = time.Now().Unix() |
||
| 209 | assert.True(t, p.needUpdateCredential()) |
||
| 210 | |||
| 211 | p.expirationTimestamp = time.Now().Unix() + 300 |
||
| 212 | assert.False(t, p.needUpdateCredential()) |
||
| 213 | } |
||
| 356 |