DevOpsGOServer

Форк
0
/
service_test.go 
209 строк · 4.5 Кб
1
package main
2

3
import (
4
	"net/http/httptest"
5
	"strings"
6

7
	"testing"
8

9
	"github.com/dailymotion/allure-go"
10
)
11

12
func TestUppercase(t *testing.T) {
13
	allure.Test(t, allure.Action(func() {
14
		strService := stringService{}
15
		str := "hello"
16
		expected := "HELLO"
17

18
		actual, err := strService.Uppercase(str)
19
		if err != nil {
20
			t.Error(err)
21
		}
22

23
		if actual != expected {
24
			t.Errorf("Expected %s, got %s", expected, actual)
25
		}
26
	}))
27
}
28

29
func TestUppercaseEmpty(t *testing.T) {
30
	allure.Test(t, allure.Action(func() {
31
		strService := stringService{}
32
		str := ""
33
		expected := ""
34

35
		actual, err := strService.Uppercase(str)
36
		if err == nil {
37
			t.Error("Expected error, got nil")
38
		}
39

40
		if err.Error() != "empty string" {
41
			t.Errorf("Expected error %s, got %s", "empty string", err.Error())
42
		}
43

44
		if actual != expected {
45
			t.Errorf("Expected %s, got %s", expected, actual)
46
		}
47
	}))
48
}
49

50
func TestCount(t *testing.T) {
51
	allure.Test(t, allure.Action(func() {
52
		strService := stringService{}
53
		str := "hello"
54
		expected := 5
55

56
		actual := strService.Count(str)
57

58
		if actual != expected {
59
			t.Errorf("Expected %d, got %d", expected, actual)
60
		}
61
	}))
62
}
63

64
func TestMakeUppercaseEndpoint(t *testing.T) {
65
	allure.Test(t, allure.Action(func() {
66
		strService := stringService{}
67
		endpoint := makeUppercaseEndpoint(strService)
68
		str := "hello"
69
		expected := "HELLO"
70

71
		actual, err := endpoint(nil, uppercaseRequest{str})
72
		if err != nil {
73
			t.Error(err)
74
		}
75

76
		if actual.(uppercaseResponse).V != expected {
77
			t.Errorf("Expected %s, got %s", expected, actual)
78
		}
79
	}))
80
}
81

82
func TestMakeUppercaseEndpointEmpty(t *testing.T) {
83
	allure.Test(t, allure.Action(func() {
84
		strService := stringService{}
85
		endpoint := makeUppercaseEndpoint(strService)
86
		str := ""
87
		expected := ""
88

89
		actual, err := endpoint(nil, uppercaseRequest{str})
90
		if err != nil {
91
			t.Error(err)
92
		}
93

94
		if actual.(uppercaseResponse).Err != "empty string" {
95
			t.Errorf("Expected error %s, got %s", "empty string", actual.(uppercaseResponse).Err)
96
		}
97

98
		if actual.(uppercaseResponse).V != expected {
99
			t.Errorf("Expected %s, got %s", expected, actual)
100
		}
101
	}))
102
}
103

104
func TestMakeCountEndpoint(t *testing.T) {
105
	allure.Test(t, allure.Action(func() {
106
		strService := stringService{}
107
		endpoint := makeCountEndpoint(strService)
108
		str := "hello"
109
		expected := 5
110

111
		actual, err := endpoint(nil, countRequest{str})
112
		if err != nil {
113
			t.Error(err)
114
		}
115

116
		if actual.(countResponse).V != expected {
117
			t.Errorf("Expected %d, got %d", expected, actual)
118
		}
119
	}))
120
}
121

122
func TestDecodeUppercaseRequest(t *testing.T) {
123
	allure.Test(t, allure.Action(func() {
124
		str := "hello"
125
		expected := uppercaseRequest{str}
126

127
		req := httptest.NewRequest("POST", "/uppercase", strings.NewReader(`{"s":"`+str+`"}`))
128

129
		actual, err := decodeUppercaseRequest(nil, req)
130
		if err != nil {
131
			t.Error(err)
132
		}
133

134
		if actual.(uppercaseRequest).S != expected.S {
135
			t.Errorf("Expected %s, got %s", expected, actual)
136
		}
137
	}))
138
}
139

140
func TestDecodeUppercaseRequestWrongJson(t *testing.T) {
141
	allure.Test(t, allure.Action(func() {
142
		str := "hello"
143
		expected := uppercaseRequest{str}
144

145
		req := httptest.NewRequest("POST", "/uppercase", strings.NewReader(`{"s":"`+str+`"`))
146

147
		actual, err := decodeUppercaseRequest(nil, req)
148
		if err == nil {
149
			t.Error("Expected error, got nil")
150
		}
151

152
		if actual != nil {
153
			t.Errorf("Expected %s, got %s", expected, actual)
154
		}
155
	}))
156
}
157

158
func TestDecodeCountRequest(t *testing.T) {
159
	allure.Test(t, allure.Action(func() {
160
		str := "hello"
161
		expected := countRequest{str}
162

163
		req := httptest.NewRequest("POST", "/count", strings.NewReader(`{"s":"`+str+`"}`))
164

165
		actual, err := decodeCountRequest(nil, req)
166
		if err != nil {
167
			t.Error(err)
168
		}
169

170
		if actual.(countRequest).S != expected.S {
171
			t.Errorf("Expected %s, got %s", expected, actual)
172
		}
173
	}))
174
}
175

176
func TestDecodeCountRequestWrongJson(t *testing.T) {
177
	allure.Test(t, allure.Action(func() {
178
		str := "hello"
179
		expected := countRequest{str}
180

181
		req := httptest.NewRequest("POST", "/count", strings.NewReader(`{"s":"`+str+`"`))
182

183
		actual, err := decodeCountRequest(nil, req)
184
		if err == nil {
185
			t.Error("Expected error, got nil")
186
		}
187

188
		if actual != nil {
189
			t.Errorf("Expected %s, got %s", expected, actual)
190
		}
191
	}))
192
}
193

194
func TestEncodeResponse(t *testing.T) {
195
	allure.Test(t, allure.Action(func() {
196
		str := "hello"
197
		expected := "{\"v\":\"" + str + "\"}\n"
198

199
		res := httptest.NewRecorder()
200
		err := encodeResponse(nil, res, uppercaseResponse{str, ""})
201
		if err != nil {
202
			t.Error(err)
203
		}
204

205
		if res.Body.String() != expected {
206
			t.Errorf("Expected %s, got %s", expected, res.Body.String())
207
		}
208
	}))
209
}
210

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.