-
Notifications
You must be signed in to change notification settings - Fork 484
/
precode.go
58 lines (46 loc) · 1.41 KB
/
precode.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
package main
import (
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
)
var cafeList = map[string][]string{
"moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"},
}
func mainHandle(w http.ResponseWriter, req *http.Request) {
countStr := req.URL.Query().Get("count")
if countStr == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("count missing"))
return
}
count, err := strconv.Atoi(countStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("wrong count value"))
return
}
city := req.URL.Query().Get("city")
cafe, ok := cafeList[city]
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("wrong city value"))
return
}
if count > len(cafe) {
count = len(cafe)
}
answer := strings.Join(cafe[:count], ",")
w.WriteHeader(http.StatusOK)
w.Write([]byte(answer))
}
func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) {
totalCount := 4
req := ... // здесь нужно создать запрос к сервису
responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(mainHandle)
handler.ServeHTTP(responseRecorder, req)
// здесь нужно добавить необходимые проверки
}