Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module main.go

go 1.20

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
52 changes: 52 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMainHandlerWhenAllRight(t *testing.T) {
req := httptest.NewRequest("GET", "/cafe?count=4&city=moscow", nil)
responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(mainHandle)
handler.ServeHTTP(responseRecorder, req)

status := responseRecorder.Code
body := responseRecorder.Body
require.Equal(t, http.StatusOK, status)
assert.NotEmpty(t, body)
}

func TestMainHandWrongCity(t *testing.T) {
req := httptest.NewRequest("GET", "/cafe?count=4&city=mocsow", nil)
responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(mainHandle)
handler.ServeHTTP(responseRecorder, req)

status := responseRecorder.Code
body := responseRecorder.Body.String()
require.Equal(t, http.StatusBadRequest, status)
assert.Equal(t, "wrong city value", body)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки статуса ответа

}

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)

require.NotEmpty(t, responseRecorder.Code)

status := responseRecorder.Code
require.Equal(t, http.StatusOK, status)

body := responseRecorder.Body.String()
list := strings.Split(body, ",")
assert.Equal(t, totalCount, len(list))
}
58 changes: 0 additions & 58 deletions precode.go

This file was deleted.