| Conditions | 8 | 
| Total Lines | 88 | 
| Code Lines | 61 | 
| 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 | ||
| 40 | func TestURLCredentialsProvider_getCredentials(t *testing.T) { | ||
| 41 | originHttpDo := httpDo | ||
| 42 | 	defer func() { httpDo = originHttpDo }() | ||
| 43 | p, err := NewURLCredentialsProviderBuilderBuilder(). | ||
| 44 | 		WithUrl("http://localhost:8080"). | ||
| 45 | Build() | ||
| 46 | assert.Nil(t, err) | ||
| 47 | |||
| 48 | // case 1: server error | ||
| 49 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 50 | 		err = errors.New("mock server error") | ||
| 51 | return | ||
| 52 | } | ||
| 53 | _, err = p.getCredentials() | ||
| 54 | assert.NotNil(t, err) | ||
| 55 | assert.Equal(t, "mock server error", err.Error()) | ||
| 56 | |||
| 57 | // case 2: 4xx error | ||
| 58 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 59 | 		res = &httputil.Response{ | ||
| 60 | StatusCode: 400, | ||
| 61 | 			Body:       []byte("4xx error"), | ||
| 62 | } | ||
| 63 | return | ||
| 64 | } | ||
| 65 | |||
| 66 | _, err = p.getCredentials() | ||
| 67 | assert.NotNil(t, err) | ||
| 68 | assert.Equal(t, "get credentials from GET http://localhost:8080 failed: 4xx error", err.Error()) | ||
| 69 | |||
| 70 | // case 3: invalid json | ||
| 71 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 72 | 		res = &httputil.Response{ | ||
| 73 | StatusCode: 200, | ||
| 74 | 			Body:       []byte("invalid json"), | ||
| 75 | } | ||
| 76 | return | ||
| 77 | } | ||
| 78 | _, err = p.getCredentials() | ||
| 79 | assert.NotNil(t, err) | ||
| 80 | assert.Equal(t, "get credentials from GET http://localhost:8080 failed with error, json unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) | ||
| 81 | |||
| 82 | // case 4: empty response json | ||
| 83 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 84 | 		res = &httputil.Response{ | ||
| 85 | StatusCode: 200, | ||
| 86 | 			Body:       []byte("null"), | ||
| 87 | } | ||
| 88 | return | ||
| 89 | } | ||
| 90 | _, err = p.getCredentials() | ||
| 91 | assert.NotNil(t, err) | ||
| 92 | assert.Equal(t, "refresh credentials from GET http://localhost:8080 failed: null", err.Error()) | ||
| 93 | |||
| 94 | // case 5: empty session ak response json | ||
| 95 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 96 | 		res = &httputil.Response{ | ||
| 97 | StatusCode: 200, | ||
| 98 | 			Body:       []byte(`{}`), | ||
| 99 | } | ||
| 100 | return | ||
| 101 | } | ||
| 102 | _, err = p.getCredentials() | ||
| 103 | assert.NotNil(t, err) | ||
| 104 | 	assert.Equal(t, "refresh credentials from GET http://localhost:8080 failed: {}", err.Error()) | ||
| 105 | |||
| 106 | // case 6: mock ok value | ||
| 107 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 108 | 		res = &httputil.Response{ | ||
| 109 | StatusCode: 200, | ||
| 110 | 			Body:       []byte(`{"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token"}`), | ||
| 111 | } | ||
| 112 | return | ||
| 113 | } | ||
| 114 | creds, err := p.getCredentials() | ||
| 115 | assert.Nil(t, err) | ||
| 116 | assert.Equal(t, "saki", creds.AccessKeyId) | ||
| 117 | assert.Equal(t, "saks", creds.AccessKeySecret) | ||
| 118 | assert.Equal(t, "token", creds.SecurityToken) | ||
| 119 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) | ||
| 120 | |||
| 121 | // needUpdateCredential | ||
| 122 | assert.True(t, p.needUpdateCredential()) | ||
| 123 | p.expirationTimestamp = time.Now().Unix() | ||
| 124 | assert.True(t, p.needUpdateCredential()) | ||
| 125 | |||
| 126 | p.expirationTimestamp = time.Now().Unix() + 300 | ||
| 127 | assert.False(t, p.needUpdateCredential()) | ||
| 128 | } | ||
| 200 |