Skip to content

Commit

Permalink
add wait function
Browse files Browse the repository at this point in the history
  • Loading branch information
trapajim committed Jul 24, 2024
1 parent 3af3f10 commit 8921873
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
)

type Request struct {
Expand All @@ -20,6 +21,7 @@ type Request struct {
plan *TestPlan
expect *Expect
key string
d time.Duration
}

// Body sets the request body
Expand Down
16 changes: 16 additions & 0 deletions testpilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"strconv"
"strings"
"testing"
"time"
)

var placeholderRegex = regexp.MustCompile(`{\.?([\w\.\d]+)}`)
var findPlaceholderRegex = regexp.MustCompile(`{[^}]+}`)

const waitMethod = "TESTPILOT_WAIT"

type TestPlan struct {
t *testing.T
name string
Expand Down Expand Up @@ -50,6 +53,10 @@ func (p *TestPlan) Run() {
p.runCalled = true
p.t.Run(p.name, func(t *testing.T) {
for _, request := range p.requests {
if request.method == waitMethod {
time.Sleep(request.d)
continue
}
url, err := normalizeUrl(request.url, p.lastResponseBody, p.responseStore)
if err != nil {
t.Errorf(err.Error())
Expand All @@ -74,6 +81,15 @@ func (p *TestPlan) Request(method, url string) *Request {
return request
}

// Wait pauses the test plan for a given duration
func (p *TestPlan) Wait(d time.Duration) {
request := &Request{
method: waitMethod,
d: d,
}
p.requests = append(p.requests, request)
}

// Response returns the last response body
func (p *TestPlan) Response() []byte {
return p.lastResponseBody
Expand Down
2 changes: 2 additions & 0 deletions testpilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"strconv"
"testing"
"time"
)

func Test_TestPlan(t *testing.T) {
Expand Down Expand Up @@ -39,6 +40,7 @@ func Test_TestPlan(t *testing.T) {
p.Request("GET", server.URL+"/{user.id}").Expect().Status(200).Body(ResponseComparer(p))
p.Request("GET", server.URL+"/{user.id}").Expect().Status(200).Body(AssertExists("id"))
p.Request("GET", server.URL+"/2").Expect().Status(404)
p.Wait(20 * time.Second)
p.Request("GET", server.URL+"/ping").Expect().Status(200).Body(AssertEqual("pong"))
p.Run()

Expand Down

0 comments on commit 8921873

Please sign in to comment.