Conditions | 3 |
Total Lines | 59 |
Code Lines | 43 |
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 responder |
||
16 | func TestWriteResponse_GivenResponse_SerializedDataWritten(t *testing.T) { |
||
17 | tests := []struct { |
||
18 | name string |
||
19 | contentType string |
||
20 | expectedSerializationFormat string |
||
21 | }{ |
||
22 | { |
||
23 | "json response", |
||
24 | "application/json", |
||
25 | "json", |
||
26 | }, |
||
27 | { |
||
28 | "json ld response", |
||
29 | "application/ld+json", |
||
30 | "json", |
||
31 | }, |
||
32 | { |
||
33 | "xml response", |
||
34 | "application/xml", |
||
35 | "xml", |
||
36 | }, |
||
37 | { |
||
38 | "xml response", |
||
39 | "application/xml", |
||
40 | "xml", |
||
41 | }, |
||
42 | { |
||
43 | "soap xml response", |
||
44 | "application/soap+xml", |
||
45 | "xml", |
||
46 | }, |
||
47 | { |
||
48 | "text html response", |
||
49 | "text/html", |
||
50 | "raw", |
||
51 | }, |
||
52 | } |
||
53 | for _, test := range tests { |
||
54 | t.Run(test.name, func(t *testing.T) { |
||
55 | response := &generator.Response{ |
||
56 | StatusCode: http.StatusOK, |
||
57 | ContentType: test.contentType, |
||
58 | Data: "data", |
||
59 | } |
||
60 | serializer := &serializermock.Serializer{} |
||
61 | serializer. |
||
62 | On("Serialize", response.Data, test.expectedSerializationFormat). |
||
63 | Return([]byte("serialized"), nil). |
||
64 | Once() |
||
65 | recorder := httptest.NewRecorder() |
||
66 | responder := New().(*coordinatingResponder) |
||
67 | responder.serializer = serializer |
||
68 | |||
69 | responder.WriteResponse(context.Background(), recorder, response) |
||
70 | |||
71 | serializer.AssertExpectations(t) |
||
72 | assert.Equal(t, response.ContentType+"; charset=utf-8", recorder.Header().Get("Content-Type")) |
||
73 | assert.Equal(t, response.StatusCode, recorder.Code) |
||
74 | assert.Equal(t, "serialized", recorder.Body.String()) |
||
75 | }) |
||
137 |