-
Notifications
You must be signed in to change notification settings - Fork 0
First #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
First #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } | ||
| } |
| 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, ",") | ||
|
|
||
| assert.Equal(t, http.StatusOK, responseRecorder.Code) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require |
||
| assert.Len(t, list, totalCount) | ||
| // здесь нужно добавить необходимые проверки | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require |
||
| assert.NotEmpty(t, body) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require |
||
| assert.Equal(t, "wrong city value", body) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
сначала стоит проверить код ответа потом уже с телом работать