Skip to content

Commit

Permalink
Add header to target containing the sequence number (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques-Olivier D. Bernier authored Feb 16, 2020
1 parent 23128bb commit c9aafc7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync"
"time"

"strconv"

"golang.org/x/net/http2"
)

Expand Down Expand Up @@ -344,7 +346,7 @@ func (a *Attacker) attack(tr Targeter, name string, workers *sync.WaitGroup, tic
func (a *Attacker) hit(tr Targeter, name string) *Result {
var (
res = Result{Attack: name}
tgt Target
tgt = Target{Header: make(http.Header)}
err error
)

Expand All @@ -361,6 +363,8 @@ func (a *Attacker) hit(tr Targeter, name string) *Result {
}
}()

tgt.Header.Set("X-Vegeta-Seq", strconv.FormatUint(res.Seq, 10))

if err = tr(&tgt); err != nil {
a.Stop()
return &res
Expand Down
19 changes: 19 additions & 0 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -358,3 +359,21 @@ func TestClient(t *testing.T) {
t.Errorf("Expected timeout error")
}
}

func TestSeqHeader(t *testing.T) {
t.Parallel()

var got http.Header

atk := NewAttacker()
for i := 0; i < 5; i++ {
res := atk.hit(func(trt *Target) error {
got = trt.Header
return nil
}, "")

if got.Get("X-Vegeta-Seq") != strconv.FormatUint(res.Seq, 10) {
t.Fatalf("got: %v, want: %v", got.Get("X-Vegeta-Seq"), res.Seq)
}
}
}

0 comments on commit c9aafc7

Please sign in to comment.