From eadd5a5163d2bff2d60df53a66aa457cf1070afa Mon Sep 17 00:00:00 2001 From: Sergey Gaynetdinov Date: Fri, 6 Dec 2019 17:39:04 +0500 Subject: [PATCH] Add test 'telegram.getContentLength' --- app/proc/telegram_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/proc/telegram_test.go b/app/proc/telegram_test.go index 14ea3d4c..27446889 100644 --- a/app/proc/telegram_test.go +++ b/app/proc/telegram_test.go @@ -1,6 +1,9 @@ package proc import ( + "fmt" + "net/http" + "net/http/httptest" "strconv" "testing" "time" @@ -128,3 +131,38 @@ func TestGetFilenameByURL(t *testing.T) { }) } } + +func TestGetContentLengthNotFound(t *testing.T) { + cases := []struct { + statusCode int + contentLength int + expectedLength int + expectedError string + }{ + {http.StatusInternalServerError, 100500, 0, "non-200 status, 500"}, + {http.StatusMethodNotAllowed, 100500, 0, "non-200 status, 405"}, + {http.StatusOK, 4, 4, ""}, + } + + // nolint:scopelint + for i, tc := range cases { + t.Run(strconv.Itoa(i), func(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(tc.statusCode) + w.Header().Set("Content-Length", string(tc.contentLength)) + if tc.contentLength > 0 { + fmt.Fprint(w, "abcd") + } + })) + + defer ts.Close() + + length, err := getContentLength(ts.URL) + + assert.Equal(t, tc.expectedLength, length) + if err != nil { + assert.Equal(t, tc.expectedError, err.Error()) + } + }) + } +}