-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
208 lines (200 loc) · 5.26 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package govader_backend
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/jonreiter/govader"
echo "github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
type ResponseType struct {
Negative float64 `json:"Negative"`
Neutral float64 `json:"Neutral"`
Positive float64 `json:"Positive"`
Compound float64 `json:"Compound"`
}
func TestHandler_HandleGetRequest(t *testing.T) {
tests := []struct {
name string
h Handler
wantErr bool
}{
{
"Success",
Handler{analyzer: govader.NewSentimentIntensityAnalyzer()},
false,
},
{
"Missing text",
Handler{analyzer: govader.NewSentimentIntensityAnalyzer()},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Success" {
httpRecorder := httptest.NewRecorder()
router := echo.New()
router.GET("/", tt.h.HandleGetRequest)
request, err := http.NewRequest("GET", "/?text=I am happy", nil)
assert.NoError(t, err)
router.ServeHTTP(httpRecorder, request)
assert.Equal(t, http.StatusOK, httpRecorder.Code)
var response ResponseType
body, err := ioutil.ReadAll(httpRecorder.Result().Body)
if err != nil {
t.Error(err)
}
_ = json.Unmarshal(body, &response)
assert.Equal(t, 1, 1)
}
if tt.name == "Missing text" {
httpRecorder := httptest.NewRecorder()
router := echo.New()
router.GET("/", tt.h.HandleGetRequest)
request, err := http.NewRequest("GET", "/?text=", nil)
assert.NoError(t, err)
router.ServeHTTP(httpRecorder, request)
assert.Equal(t, http.StatusBadRequest, httpRecorder.Code)
var response map[string]string
body, err := ioutil.ReadAll(httpRecorder.Result().Body)
if err != nil {
t.Error(err)
}
_ = json.Unmarshal(body, &response)
assert.Equal(t, response["error"], "?text= parameter is required")
}
})
}
}
func TestHandler_HandlePostRequest(t *testing.T) {
tests := []struct {
name string
h Handler
wantErr bool
}{
{
"Success",
Handler{analyzer: govader.NewSentimentIntensityAnalyzer()},
false,
},
{
"Missing text",
Handler{analyzer: govader.NewSentimentIntensityAnalyzer()},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Success" {
httpRecorder := httptest.NewRecorder()
router := echo.New()
router.POST("/", tt.h.HandlePostRequest)
reqBody := map[string]string{"text": "I am happy"}
jsonValue, _ := json.Marshal(reqBody)
//requestBody := bytes.NewBuffer([]byte(`{"text": "I am happy"}`))
request, err := http.NewRequest("POST", "/", bytes.NewBuffer(jsonValue))
request.Header.Add("Content-Type", "application/json")
assert.NoError(t, err)
router.ServeHTTP(httpRecorder, request)
assert.Equal(t, http.StatusOK, httpRecorder.Code)
var response map[string]interface{}
body, err := ioutil.ReadAll(httpRecorder.Result().Body)
if err != nil {
t.Error(err)
}
_ = json.Unmarshal(body, &response)
assert.Equal(t, response["Negative"], 0.0)
}
if tt.name == "Missing text" {
httpRecorder := httptest.NewRecorder()
router := echo.New()
router.POST("/", tt.h.HandlePostRequest)
_ = bytes.NewBuffer([]byte(`{"text": ""}`))
request, err := http.NewRequest("POST", "/", nil)
assert.NoError(t, err)
router.ServeHTTP(httpRecorder, request)
assert.Equal(t, http.StatusBadRequest, httpRecorder.Code)
var response map[string]interface{}
body, err := ioutil.ReadAll(httpRecorder.Result().Body)
if err != nil {
t.Error(err)
}
_ = json.Unmarshal(body, &response)
assert.Equal(t, response["error"], "text required")
}
})
}
}
func TestServe(t *testing.T) {
type args struct {
portNumber string
}
tests := []struct {
name string
h Handler
args args
}{
{"success", Handler{analyzer: govader.NewSentimentIntensityAnalyzer()}, args{portNumber: "8080"}},
{"fail", Handler{analyzer: govader.NewSentimentIntensityAnalyzer()}, args{portNumber: "8080"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "success" {
e := echo.New()
err := Serve(e, tt.args.portNumber)
if err != nil {
t.Error(err)
}
e.Listener.Close()
assert.Equal(t, 1, 1)
}
if tt.name == "fail" {
e1 := echo.New()
err1 := Serve(e1, tt.args.portNumber)
if err1 != nil {
t.Error(err1)
}
e2 := echo.New()
err2 := Serve(e2, tt.args.portNumber)
if err2 != nil {
assert.Equal(t, 1, 1)
}
e1.Listener.Close()
}
})
}
}
func TestHandler_HandleHealthCheck(t *testing.T) {
type args struct {
c echo.Context
}
tests := []struct {
name string
h Handler
args args
wantErr bool
}{
{"Success",
Handler{analyzer: govader.NewSentimentIntensityAnalyzer()},
args{c: echo.New().NewContext(nil, nil)},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Success" {
httpRecorder := httptest.NewRecorder()
router := echo.New()
router.GET("/health", tt.h.HandleHealthCheck)
request, err := http.NewRequest("GET", "/health", nil)
assert.NoError(t, err)
router.ServeHTTP(httpRecorder, request)
assert.Equal(t, http.StatusOK, httpRecorder.Code)
}
})
}
}