Written   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 59
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 43
nop 1
dl 0
loc 59
rs 8.8478
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
package responder
2
3
import (
4
	"context"
5
	"errors"
6
	"net/http"
7
	"net/http/httptest"
8
	"testing"
9
10
	apperrors "github.com/muonsoft/openapi-mock/internal/errors"
11
	"github.com/muonsoft/openapi-mock/internal/openapi/generator"
12
	serializermock "github.com/muonsoft/openapi-mock/test/mocks/openapi/responder/serializer"
13
	"github.com/stretchr/testify/assert"
14
)
15
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
		})
76
	}
77
}
78
79
func TestCoordinatingResponder_WriteResponse_NoContentResponse_EmptyBodyWritten(t *testing.T) {
80
	response := &generator.Response{
81
		StatusCode:  http.StatusNoContent,
82
		ContentType: "",
83
		Data:        "",
84
	}
85
	serializer := &serializermock.Serializer{}
86
	serializer.On("Serialize", response.Data, "raw").Return([]byte(""), nil).Once()
87
	recorder := httptest.NewRecorder()
88
	responder := New().(*coordinatingResponder)
89
	responder.serializer = serializer
90
91
	responder.WriteResponse(context.Background(), recorder, response)
92
93
	serializer.AssertExpectations(t)
94
	assert.Equal(t, "", recorder.Header().Get("Content-Type"))
95
	assert.Equal(t, http.StatusNoContent, recorder.Code)
96
	assert.Equal(t, "", recorder.Body.String())
97
}
98
99
func TestCoordinatingResponder_WriteResponse_SerializationError_UnexpectedErrorWritten(t *testing.T) {
100
	response := &generator.Response{
101
		StatusCode:  http.StatusOK,
102
		ContentType: "content/type",
103
		Data:        "data",
104
	}
105
	serializer := &serializermock.Serializer{}
106
	serializer.
107
		On("Serialize", response.Data, "raw").
108
		Return(nil, errors.New("error")).
109
		Once()
110
	recorder := httptest.NewRecorder()
111
	responder := New().(*coordinatingResponder)
112
	responder.serializer = serializer
113
114
	responder.WriteResponse(context.Background(), recorder, response)
115
116
	serializer.AssertExpectations(t)
117
	assert.Equal(t, "text/html; charset=utf-8", recorder.Header().Get("Content-Type"))
118
	assert.Equal(t, http.StatusInternalServerError, recorder.Code)
119
	assert.Contains(t, recorder.Body.String(), "<h1>Unexpected error</h1>")
120
	assert.Contains(t, recorder.Body.String(), "An unexpected error occurred:<br>error")
121
}
122
123
func TestCoordinatingResponder_WriteError_UnsupportedFeatureError_UnsupportedPage(t *testing.T) {
124
	recorder := httptest.NewRecorder()
125
	responder := New()
126
	notSupported := &apperrors.NotSupported{Message: "unsupported feature description"}
127
128
	responder.WriteError(context.Background(), recorder, notSupported)
129
	response := recorder.Body.String()
130
131
	assert.Equal(t, "text/html; charset=utf-8", recorder.Header().Get("Content-Type"))
132
	assert.Equal(t, http.StatusInternalServerError, recorder.Code)
133
	assert.Contains(t, response, "<h1>Feature is not supported</h1>")
134
	assert.Contains(t, response, "An error occurred: unsupported feature description.")
135
	assert.Contains(t, response, "If you want this feature to be supported, please make an issue at the project page")
136
}
137