Conditions | 4 |
Total Lines | 71 |
Code Lines | 49 |
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 client_test |
||
41 | func TestShouldForwardProvidedCookiesWhenUsingJar(t *testing.T) { |
||
42 | const ( |
||
43 | serverCookieName = "server_cookie_name" |
||
44 | serverCookieValue = "server_cookie_value" |
||
45 | ) |
||
46 | |||
47 | testServer, serverAssertion := test.NewServerWithAssertion( |
||
48 | http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||
49 | http.SetCookie( |
||
50 | w, |
||
51 | &http.Cookie{ |
||
52 | Name: serverCookieName, |
||
53 | Value: serverCookieValue, |
||
54 | Expires: time.Now().AddDate(0, 1, 0), |
||
55 | }, |
||
56 | ) |
||
57 | }), |
||
58 | ) |
||
59 | defer testServer.Close() |
||
60 | |||
61 | u, err := url.Parse(testServer.URL) |
||
62 | assert.NoError(t, err) |
||
63 | |||
64 | cookies := []*http.Cookie{ |
||
65 | { |
||
66 | Name: "a_cookie_name", |
||
67 | Value: "a_cookie_value", |
||
68 | }, |
||
69 | } |
||
70 | |||
71 | c, err := client.NewClientFromConfig( |
||
72 | 100, |
||
73 | nil, |
||
74 | "", |
||
75 | true, |
||
76 | cookies, |
||
77 | map[string]string{}, |
||
78 | false, |
||
79 | u, |
||
80 | ) |
||
81 | assert.NoError(t, err) |
||
82 | |||
83 | res, err := c.Get(testServer.URL) |
||
84 | assert.NoError(t, err) |
||
85 | assert.NotNil(t, res) |
||
86 | |||
87 | assert.Equal(t, 1, serverAssertion.Len()) |
||
88 | |||
89 | serverAssertion.At(0, func(r http.Request) { |
||
90 | assert.Equal(t, 1, len(r.Cookies())) |
||
91 | |||
92 | assert.Equal(t, r.Cookies()[0].Name, cookies[0].Name) |
||
93 | assert.Equal(t, r.Cookies()[0].Value, cookies[0].Value) |
||
94 | assert.Equal(t, r.Cookies()[0].Expires, cookies[0].Expires) |
||
95 | }) |
||
96 | |||
97 | res, err = c.Get(testServer.URL) |
||
98 | assert.NoError(t, err) |
||
99 | assert.NotNil(t, res) |
||
100 | |||
101 | assert.Equal(t, 2, serverAssertion.Len()) |
||
102 | |||
103 | serverAssertion.At(1, func(r http.Request) { |
||
104 | assert.Equal(t, 2, len(r.Cookies())) |
||
105 | |||
106 | assert.Equal(t, r.Cookies()[0].Name, cookies[0].Name) |
||
107 | assert.Equal(t, r.Cookies()[0].Value, cookies[0].Value) |
||
108 | assert.Equal(t, r.Cookies()[0].Expires, cookies[0].Expires) |
||
109 | |||
110 | assert.Equal(t, r.Cookies()[1].Name, serverCookieName) |
||
111 | assert.Equal(t, r.Cookies()[1].Value, serverCookieValue) |
||
112 | }) |
||
248 |