Skip to content

Commit

Permalink
add context to requests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrodriges authored Aug 22, 2022
1 parent d9b7c36 commit 2b7f82b
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cmd/shortenertest/iteration10_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ func (suite *Iteration10Suite) TestPingHandler() {
suite.T().Error("Не удалось получить код ответа 200 от хендлера 'GET /ping' за отведенное время")
return
case <-ticker.C:
resp, _ := httpc.R().Get("/ping")
rctx, rcancel := context.WithTimeout(context.Background(), time.Second)
defer rcancel()

resp, _ := httpc.R().
SetContext(rctx).
Get("/ping")
if resp != nil && resp.StatusCode() == http.StatusOK {
return
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/shortenertest/iteration11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ func (suite *Iteration11Suite) TestInspectDatabase() {
SetHostURL(suite.serverAddress)

suite.Run("shorten", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
_, err := req.Post("/")
noRespErr := suite.Assert().NoError(err, "Ошибка при попытке сделать запрос для сокращения URL")
Expand Down
4 changes: 4 additions & 0 deletions cmd/shortenertest/iteration12_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ func (suite *Iteration12Suite) TestBatchShorten() {
SetRedirectPolicy(redirPolicy)

suite.Run("shorten_batch", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetBody(requestData).
SetResult(&responseData)
Expand Down
4 changes: 4 additions & 0 deletions cmd/shortenertest/iteration13_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ func (suite *Iteration13Suite) TestConflict() {
var shortenURL string

for attempt := 0; attempt <= 2; attempt++ {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down
30 changes: 28 additions & 2 deletions cmd/shortenertest/iteration14_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ func (suite *Iteration14Suite) TestDelete() {
for num := 0; num <= 10; num++ {
originalURL := generateTestURL(suite.T())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down Expand Up @@ -199,7 +203,11 @@ func (suite *Iteration14Suite) TestDelete() {
body = append(body, strings.Trim(u.Path, "/"))
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetBody(body)
resp, err := req.Delete("/api/user/urls")
Expand Down Expand Up @@ -230,7 +238,12 @@ func (suite *Iteration14Suite) TestDelete() {
case <-ticker.C:
var deletedCount int
for _, shorten := range shortenURLs {
resp, err := httpc.R().Get(shorten)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

resp, err := httpc.R().
SetContext(ctx).
Get(shorten)
if err == nil && resp != nil && resp.StatusCode() == http.StatusGone {
deletedCount++
}
Expand Down Expand Up @@ -268,7 +281,11 @@ func (suite *Iteration14Suite) TestDeleteConcurrent() {
{
originalURL := generateTestURL(suite.T())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down Expand Up @@ -299,7 +316,11 @@ func (suite *Iteration14Suite) TestDeleteConcurrent() {

body := []string{strings.Trim(u.Path, "/")}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetBody(body)
resp, err := req.Delete("/api/user/urls")
Expand Down Expand Up @@ -330,7 +351,12 @@ func (suite *Iteration14Suite) TestDeleteConcurrent() {
suite.T().Errorf("Не удалось дождаться удаления переданных URL в течении 60 секунд")
return
case <-ticker.C:
resp, err := httpc.R().Get(shortenURL)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

resp, err := httpc.R().
SetContext(ctx).
Get(shortenURL)
if err == nil && resp != nil && resp.StatusCode() == http.StatusGone {
return
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/shortenertest/iteration1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ func (suite *Iteration1Suite) TestHandlers() {
SetHostURL(suite.serverAddress).
SetRedirectPolicy(redirPolicy)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

suite.Run("shorten", func() {
req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand All @@ -132,9 +136,13 @@ func (suite *Iteration1Suite) TestHandlers() {
})

suite.Run("expand", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := resty.New().
SetRedirectPolicy(redirPolicy).
R()
R().
SetContext(ctx)
resp, err := req.Get(shortenURL)

noRespErr := true
Expand Down
4 changes: 4 additions & 0 deletions cmd/shortenertest/iteration4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ func (suite *Iteration4Suite) TestJSONHandler() {

var result shortenResponse

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetBody(&shortenRequest{
URL: originalURL,
Expand Down
8 changes: 8 additions & 0 deletions cmd/shortenertest/iteration5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func (suite *Iteration5Suite) TestEnvVars() {
suite.Run("shorten", func() {
originalURL := generateTestURL(suite.T())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down Expand Up @@ -161,7 +165,11 @@ func (suite *Iteration5Suite) TestEnvVars() {

var result shortenResponse

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetBody(&shortenRequest{
URL: originalURL,
Expand Down
4 changes: 4 additions & 0 deletions cmd/shortenertest/iteration6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ func (suite *Iteration6Suite) TestPersistentFile() {
SetHostURL(suite.serverAddress).
SetRedirectPolicy(redirPolicy)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

suite.Run("shorten", func() {
req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down
8 changes: 8 additions & 0 deletions cmd/shortenertest/iteration7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func (suite *Iteration7Suite) TestFlags() {
suite.Run("shorten", func() {
originalURL := generateTestURL(suite.T())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down Expand Up @@ -151,7 +155,11 @@ func (suite *Iteration7Suite) TestFlags() {

var result shortenResponse

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetBody(&shortenRequest{
URL: originalURL,
Expand Down
4 changes: 4 additions & 0 deletions cmd/shortenertest/iteration8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ func (suite *Iteration8Suite) TestGzipCompress() {
_, _ = zw.Write([]byte(originalURL))
_ = zw.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(buf.Bytes()).
SetHeader("Accept-Encoding", "gzip").
SetHeader("Content-Encoding", "gzip")
Expand Down
8 changes: 8 additions & 0 deletions cmd/shortenertest/iteration9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func (suite *Iteration9Suite) TestAuth() {
SetCookieJar(jar)

suite.Run("shorten", func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetBody(originalURL)
resp, err := req.Post("/")

Expand Down Expand Up @@ -137,7 +141,11 @@ func (suite *Iteration9Suite) TestAuth() {

var respBody []respPair

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req := httpc.R().
SetContext(ctx).
SetHeader("Accept-Encoding", "identity").
SetResult(&respBody)
resp, err := req.Get("/api/user/urls")
Expand Down

0 comments on commit 2b7f82b

Please sign in to comment.