Skip to content
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
87 changes: 41 additions & 46 deletions precode.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
52 changes: 52 additions & 0 deletions precode_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"
)

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, ",")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

сначала стоит проверить код ответа потом уже с телом работать


assert.Equal(t, http.StatusOK, responseRecorder.Code)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

require

assert.Len(t, list, totalCount)
// здесь нужно добавить необходимые проверки
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

лишний коммент

}

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

require

assert.NotEmpty(t, body)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

assert.NotEmpty(t, responseRecorder.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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

require

assert.Equal(t, "wrong city value", body)
}