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
15 changes: 6 additions & 9 deletions precode.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ func mainHandle(w http.ResponseWriter, req *http.Request) {
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)
}
}
48 changes: 48 additions & 0 deletions precode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tests

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// Проверка статуса ответа и тела ответа
func TestMainHandler_StatusOkAndBodyNotEmpty(t *testing.T) {
req := httptest.NewRequest("GET", "/cafe?count=2&city=moscow", nil)

responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(mainHandle)
handler.ServeHTTP(responseRecorder, req)

assert.Equal(t, http.StatusOK, responseRecorder.Code)
assert.NotEmpty(t, responseRecorder.Body.String())
}
// Проверка неподдерживаемого города
func TestMainHandlerWrongCity(t *testing.T) {
req := httptest.NewRequest("GET", "/cafe?count=2&city=spb", nil)

responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(mainHandle)
handler.ServeHTTP(responseRecorder, req)

require.Equal(t, http.StatusBadRequest, responseRecorder.Code)
assert.Contains(t, responseRecorder.Body.String(), "wrong city value")
}
// Проверка количества кафе
func TestMainHandler_WhenCountMoreThanTotal(t *testing.T) {
totalCount := 4
req := httptest.NewRequest("GET", "/cafe?count=100&city=moscow", nil)

responseRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(mainHandle)
handler.ServeHTTP(responseRecorder, req)

checkCafeString := "Мир кофе,Сладкоежка,Кофе и завтраки,Сытый студент"

list := strings.Split(responseRecorder.Body.String(), ",")

assert.GreaterOrEqual(t, totalCount, (len(list)))
assert.Equal(t, responseRecorder.Body.String(), checkCafeString)
}