Skip to content

Commit

Permalink
add contexts to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrodriges committed Jun 6, 2024
1 parent 0fc3b71 commit f8878c2
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/metricstest/iteration9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"syscall"
"time"

"github.com/Yandex-Practicum/go-autotests/internal/fork"
"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/suite"

"github.com/Yandex-Practicum/go-autotests/internal/fork"
)

type Iteration9Suite struct {
Expand Down Expand Up @@ -165,13 +164,17 @@ func (suite *Iteration9Suite) TestCounterHandlers() {
var storage int64

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

value1, value2 := int64(suite.rnd.Int31()), int64(suite.rnd.Int31())
req := httpc.R().
SetHeader("Content-Type", "application/json")

// Вдруг на сервере уже есть значение, на всякий случай запросим.
var result Metrics
resp, err := req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "counter",
Expand Down Expand Up @@ -199,6 +202,7 @@ func (suite *Iteration9Suite) TestCounterHandlers() {
}

resp, err = req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "counter",
Expand All @@ -212,6 +216,7 @@ func (suite *Iteration9Suite) TestCounterHandlers() {
"Несоответствие статус кода ответа ожидаемому в хендлере %q: %q ", req.Method, req.URL)

resp, err = req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "counter",
Expand All @@ -225,6 +230,7 @@ func (suite *Iteration9Suite) TestCounterHandlers() {
"Несоответствие статус кода ответа ожидаемому в хендлере %q: %q ", req.Method, req.URL)

resp, err = req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "counter",
Expand Down Expand Up @@ -262,12 +268,16 @@ func (suite *Iteration9Suite) TestCounterHandlers() {
})

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

req := httpc.R().
SetHeader("Content-Type", "application/json")

// Вдруг на сервере уже есть значение, на всякий случай запросим.
var result Metrics
resp, err := req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "counter",
Expand Down Expand Up @@ -307,11 +317,15 @@ func (suite *Iteration9Suite) TestGaugeHandlers() {
var storage float64

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

value := suite.rnd.Float64() * 1e6
req := httpc.R().
SetHeader("Content-Type", "application/json")

resp, err := req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "gauge",
Expand All @@ -325,6 +339,7 @@ func (suite *Iteration9Suite) TestGaugeHandlers() {

var result Metrics
resp, err = req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "gauge",
Expand Down Expand Up @@ -363,12 +378,16 @@ func (suite *Iteration9Suite) TestGaugeHandlers() {
})

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

req := httpc.R().
SetHeader("Content-Type", "application/json")

// Вдруг на сервере уже есть значение, на всякий случай запросим.
var result Metrics
resp, err := req.
SetContext(ctx).
SetBody(&Metrics{
ID: id,
MType: "gauge",
Expand Down

0 comments on commit f8878c2

Please sign in to comment.