diff --git a/precode.go b/precode.go index 5139755e..b06ea93f 100644 --- a/precode.go +++ b/precode.go @@ -1,58 +1,53 @@ package main import ( - "net/http" - "net/http/httptest" - "strconv" - "strings" - "testing" + "net/http" + "strconv" + "strings" ) var cafeList = map[string][]string{ - "moscow": []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)) + 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) - - // здесь нужно добавить необходимые проверки +func main() { + http.HandleFunc(`/cafe`, mainHandle) + err := http.ListenAndServe(":8080", nil) + if err != nil { + panic(err) + } } diff --git a/precode_test.go b/precode_test.go new file mode 100644 index 00000000..2d257b03 --- /dev/null +++ b/precode_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?count=10&city=moscow", nil) // здесь нужно создать запрос к сервису + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + body := responseRecorder.Body.String() + list := strings.Split(body, ",") + + assert.Equal(t, http.StatusOK, responseRecorder.Code) + assert.Len(t, list, totalCount) + // здесь нужно добавить необходимые проверки +} + +func TestMainHandlerNotEmpty(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=4&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + body := responseRecorder.Body.String() + + assert.Equal(t, http.StatusOK, responseRecorder.Code) + assert.NotEmpty(t, body) +} + +func TestMainHandlerWrongCity(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=2&city=london", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + body := responseRecorder.Body.String() + + assert.Equal(t, http.StatusBadRequest, responseRecorder.Code) + assert.Equal(t, "wrong city value", body) +}