DevOpsGOServer
/
service_test.go
209 строк · 4.5 Кб
1package main
2
3import (
4"net/http/httptest"
5"strings"
6
7"testing"
8
9"github.com/dailymotion/allure-go"
10)
11
12func TestUppercase(t *testing.T) {
13allure.Test(t, allure.Action(func() {
14strService := stringService{}
15str := "hello"
16expected := "HELLO"
17
18actual, err := strService.Uppercase(str)
19if err != nil {
20t.Error(err)
21}
22
23if actual != expected {
24t.Errorf("Expected %s, got %s", expected, actual)
25}
26}))
27}
28
29func TestUppercaseEmpty(t *testing.T) {
30allure.Test(t, allure.Action(func() {
31strService := stringService{}
32str := ""
33expected := ""
34
35actual, err := strService.Uppercase(str)
36if err == nil {
37t.Error("Expected error, got nil")
38}
39
40if err.Error() != "empty string" {
41t.Errorf("Expected error %s, got %s", "empty string", err.Error())
42}
43
44if actual != expected {
45t.Errorf("Expected %s, got %s", expected, actual)
46}
47}))
48}
49
50func TestCount(t *testing.T) {
51allure.Test(t, allure.Action(func() {
52strService := stringService{}
53str := "hello"
54expected := 5
55
56actual := strService.Count(str)
57
58if actual != expected {
59t.Errorf("Expected %d, got %d", expected, actual)
60}
61}))
62}
63
64func TestMakeUppercaseEndpoint(t *testing.T) {
65allure.Test(t, allure.Action(func() {
66strService := stringService{}
67endpoint := makeUppercaseEndpoint(strService)
68str := "hello"
69expected := "HELLO"
70
71actual, err := endpoint(nil, uppercaseRequest{str})
72if err != nil {
73t.Error(err)
74}
75
76if actual.(uppercaseResponse).V != expected {
77t.Errorf("Expected %s, got %s", expected, actual)
78}
79}))
80}
81
82func TestMakeUppercaseEndpointEmpty(t *testing.T) {
83allure.Test(t, allure.Action(func() {
84strService := stringService{}
85endpoint := makeUppercaseEndpoint(strService)
86str := ""
87expected := ""
88
89actual, err := endpoint(nil, uppercaseRequest{str})
90if err != nil {
91t.Error(err)
92}
93
94if actual.(uppercaseResponse).Err != "empty string" {
95t.Errorf("Expected error %s, got %s", "empty string", actual.(uppercaseResponse).Err)
96}
97
98if actual.(uppercaseResponse).V != expected {
99t.Errorf("Expected %s, got %s", expected, actual)
100}
101}))
102}
103
104func TestMakeCountEndpoint(t *testing.T) {
105allure.Test(t, allure.Action(func() {
106strService := stringService{}
107endpoint := makeCountEndpoint(strService)
108str := "hello"
109expected := 5
110
111actual, err := endpoint(nil, countRequest{str})
112if err != nil {
113t.Error(err)
114}
115
116if actual.(countResponse).V != expected {
117t.Errorf("Expected %d, got %d", expected, actual)
118}
119}))
120}
121
122func TestDecodeUppercaseRequest(t *testing.T) {
123allure.Test(t, allure.Action(func() {
124str := "hello"
125expected := uppercaseRequest{str}
126
127req := httptest.NewRequest("POST", "/uppercase", strings.NewReader(`{"s":"`+str+`"}`))
128
129actual, err := decodeUppercaseRequest(nil, req)
130if err != nil {
131t.Error(err)
132}
133
134if actual.(uppercaseRequest).S != expected.S {
135t.Errorf("Expected %s, got %s", expected, actual)
136}
137}))
138}
139
140func TestDecodeUppercaseRequestWrongJson(t *testing.T) {
141allure.Test(t, allure.Action(func() {
142str := "hello"
143expected := uppercaseRequest{str}
144
145req := httptest.NewRequest("POST", "/uppercase", strings.NewReader(`{"s":"`+str+`"`))
146
147actual, err := decodeUppercaseRequest(nil, req)
148if err == nil {
149t.Error("Expected error, got nil")
150}
151
152if actual != nil {
153t.Errorf("Expected %s, got %s", expected, actual)
154}
155}))
156}
157
158func TestDecodeCountRequest(t *testing.T) {
159allure.Test(t, allure.Action(func() {
160str := "hello"
161expected := countRequest{str}
162
163req := httptest.NewRequest("POST", "/count", strings.NewReader(`{"s":"`+str+`"}`))
164
165actual, err := decodeCountRequest(nil, req)
166if err != nil {
167t.Error(err)
168}
169
170if actual.(countRequest).S != expected.S {
171t.Errorf("Expected %s, got %s", expected, actual)
172}
173}))
174}
175
176func TestDecodeCountRequestWrongJson(t *testing.T) {
177allure.Test(t, allure.Action(func() {
178str := "hello"
179expected := countRequest{str}
180
181req := httptest.NewRequest("POST", "/count", strings.NewReader(`{"s":"`+str+`"`))
182
183actual, err := decodeCountRequest(nil, req)
184if err == nil {
185t.Error("Expected error, got nil")
186}
187
188if actual != nil {
189t.Errorf("Expected %s, got %s", expected, actual)
190}
191}))
192}
193
194func TestEncodeResponse(t *testing.T) {
195allure.Test(t, allure.Action(func() {
196str := "hello"
197expected := "{\"v\":\"" + str + "\"}\n"
198
199res := httptest.NewRecorder()
200err := encodeResponse(nil, res, uppercaseResponse{str, ""})
201if err != nil {
202t.Error(err)
203}
204
205if res.Body.String() != expected {
206t.Errorf("Expected %s, got %s", expected, res.Body.String())
207}
208}))
209}
210