Conditions | 16 |
Total Lines | 101 |
Code Lines | 72 |
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 | auth := newEcsRAMRoleCredential("go sdk", nil) |
||
13 | origTestHookDo := hookDo |
||
14 | defer func() { hookDo = origTestHookDo }() |
||
15 | |||
16 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
17 | return func(req *http.Request) (*http.Response, error) { |
||
18 | return mockResponse(300, ``, errors.New("sdk test")) |
||
19 | } |
||
20 | } |
||
21 | accesskeyID, err := auth.GetAccessKeyID() |
||
22 | assert.NotNil(t, err) |
||
23 | assert.Equal(t, "refresh Ecs sts token err: sdk test", err.Error()) |
||
24 | assert.Equal(t, "", accesskeyID) |
||
25 | |||
26 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
27 | return func(req *http.Request) (*http.Response, error) { |
||
28 | return mockResponse(300, ``, nil) |
||
29 | } |
||
30 | } |
||
31 | accesskeyID, err = auth.GetAccessKeyID() |
||
32 | assert.NotNil(t, err) |
||
33 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 300, message = ", err.Error()) |
||
34 | assert.Equal(t, "", accesskeyID) |
||
35 | |||
36 | accesskeySecret, err := auth.GetAccessSecret() |
||
37 | assert.NotNil(t, err) |
||
38 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 300, message = ", err.Error()) |
||
39 | assert.Equal(t, "", accesskeySecret) |
||
40 | |||
41 | ststoken, err := auth.GetSecurityToken() |
||
42 | assert.NotNil(t, err) |
||
43 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 300, message = ", err.Error()) |
||
44 | assert.Equal(t, "", ststoken) |
||
45 | |||
46 | assert.Equal(t, "", auth.GetBearerToken()) |
||
47 | |||
48 | assert.Equal(t, "ecs_ram_role", auth.GetType()) |
||
49 | |||
50 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
51 | return func(req *http.Request) (*http.Response, error) { |
||
52 | return mockResponse(200, `"AccessKeyID":"accessKeyID","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"`, nil) |
||
53 | } |
||
54 | } |
||
55 | accesskeyID, err = auth.GetAccessKeyID() |
||
56 | assert.NotNil(t, err) |
||
57 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character ':' after top-level value", err.Error()) |
||
58 | assert.Equal(t, "", accesskeyID) |
||
59 | |||
60 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
61 | return func(req *http.Request) (*http.Response, error) { |
||
62 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"fail"}`, nil) |
||
63 | } |
||
64 | } |
||
65 | accesskeyID, err = auth.GetAccessKeyID() |
||
66 | assert.NotNil(t, err) |
||
67 | assert.Equal(t, "refresh Ecs sts token err: Code is not Success", err.Error()) |
||
68 | assert.Equal(t, "", accesskeyID) |
||
69 | |||
70 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
71 | return func(req *http.Request) (*http.Response, error) { |
||
72 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"Success"}`, nil) |
||
73 | } |
||
74 | } |
||
75 | accesskeyID, err = auth.GetAccessKeyID() |
||
76 | assert.NotNil(t, err) |
||
77 | assert.Equal(t, "refresh Ecs sts token err: AccessKeyID: , AccessKeySecret: accessKeySecret, SecurityToken: securitytoken, Expiration: expiration", err.Error()) |
||
78 | assert.Equal(t, "", accesskeyID) |
||
79 | |||
80 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
81 | return func(req *http.Request) (*http.Response, error) { |
||
82 | return mockResponse(200, `{"AccessKeyID":"accessKeyID","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"2018-01-02T15:04:05Z","Code":"Success"}`, nil) |
||
83 | } |
||
84 | } |
||
85 | |||
86 | accesskeyID, err = auth.GetAccessKeyID() |
||
87 | assert.Nil(t, err) |
||
88 | assert.Equal(t, "accessKeyID", accesskeyID) |
||
89 | |||
90 | accesskeySecret, err = auth.GetAccessSecret() |
||
91 | assert.Nil(t, err) |
||
92 | assert.Equal(t, "accessKeySecret", accesskeySecret) |
||
93 | |||
94 | ststoken, err = auth.GetSecurityToken() |
||
95 | assert.Nil(t, err) |
||
96 | assert.Equal(t, "securitytoken", ststoken) |
||
97 | |||
98 | err = errors.New("credentials") |
||
99 | err = hookParse(err) |
||
100 | assert.Equal(t, "credentials", err.Error()) |
||
101 | |||
102 | originHookParse := hookParse |
||
103 | hookParse = func(err error) error { |
||
104 | return errors.New("error parse") |
||
105 | } |
||
106 | defer func() { |
||
107 | hookParse = originHookParse |
||
108 | }() |
||
109 | accesskeyID, err = auth.GetAccessKeyID() |
||
110 | assert.Equal(t, "refresh Ecs sts token err: error parse", err.Error()) |
||
111 | assert.Equal(t, "", accesskeyID) |
||
112 | } |
||
113 |