From 0f439fc2cdf9c5032dc1c16f51e25b9ddc63f21c Mon Sep 17 00:00:00 2001 From: Sitymay Date: Fri, 11 Apr 2025 17:53:53 +0300 Subject: [PATCH 1/2] firest commit --- precode.go | 58 ------------------------------- precode_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 58 deletions(-) delete mode 100644 precode.go create mode 100644 precode_test.go diff --git a/precode.go b/precode.go deleted file mode 100644 index 5139755e..00000000 --- a/precode.go +++ /dev/null @@ -1,58 +0,0 @@ -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) - - // здесь нужно добавить необходимые проверки -} diff --git a/precode_test.go b/precode_test.go new file mode 100644 index 00000000..a90bba82 --- /dev/null +++ b/precode_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +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 := 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) +} From aeaa28bb2320d1ca8d9a104a7a925b0f55efe486 Mon Sep 17 00:00:00 2001 From: Sitymay Date: Sat, 12 Apr 2025 19:19:15 +0300 Subject: [PATCH 2/2] second --- precode.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ precode_test.go | 39 ------------------------------------ 2 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 precode.go diff --git a/precode.go b/precode.go new file mode 100644 index 00000000..b06ea93f --- /dev/null +++ b/precode.go @@ -0,0 +1,53 @@ +package main + +import ( + "net/http" + "strconv" + "strings" +) + +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 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 index a90bba82..2d257b03 100644 --- a/precode_test.go +++ b/precode_test.go @@ -3,51 +3,12 @@ package main import ( "net/http" "net/http/httptest" - "strconv" "strings" "testing" "github.com/stretchr/testify/assert" ) -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 := httptest.NewRequest("GET", "/cafe?count=10&city=moscow", nil) // здесь нужно создать запрос к сервису