Skip to content

Commit

Permalink
feat: stub with delay
Browse files Browse the repository at this point in the history
  • Loading branch information
le-yams committed Nov 22, 2024
1 parent 5dd4a00 commit 396d539
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
20 changes: 17 additions & 3 deletions stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@ import (
"encoding/json"
"log"
"net/http"
"time"
)

type StubBuilder struct {
api *APIMock
call *HTTPCall
api *APIMock
call *HTTPCall
delay time.Duration
}

func (stub *StubBuilder) With(handler http.HandlerFunc) *APIMock {
stub.api.calls[*stub.call] = handler
if stub.delay > 0 {
stub.api.calls[*stub.call] = func(writer http.ResponseWriter, request *http.Request) {
time.Sleep(stub.delay)
handler(writer, request)
}
} else {
stub.api.calls[*stub.call] = handler
}
return stub.api
}

func (stub *StubBuilder) WithDelay(delay time.Duration) *StubBuilder {
stub.delay = delay
return stub
}

func (stub *StubBuilder) WithStatusCode(statusCode int) *APIMock {
return stub.With(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(statusCode)
Expand Down
62 changes: 40 additions & 22 deletions stubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package mockhttp
import (
"net/http"
"testing"
"time"

"github.com/gavv/httpexpect/v2"
assertions "github.com/stretchr/testify/assert"
)

func (mockedAPI *APIMock) testCall(method, path string, t *testing.T) *httpexpect.Response {

Check failure on line 11 in stubs_test.go

View workflow job for this annotation

GitHub Actions / lint

`(*APIMock).testCall` - `path` always receives `"/endpoint"` (unparam)
e := httpexpect.Default(t, mockedAPI.GetURL().String())
return e.Request(method, path).Expect()
}

func TestApiNotStubbedEndpoint(t *testing.T) {
t.Parallel()
// Arrange
Expand All @@ -16,14 +21,11 @@ func TestApiNotStubbedEndpoint(t *testing.T) {
defer func() { mockedAPI.Close() }()

// Act
client := http.Client{}
response, err := client.Get(mockedAPI.GetURL().String() + "/endpoint")
call := mockedAPI.testCall(http.MethodGet, "/endpoint", t)

// Assert
assert := assertions.New(t)
assert.NoError(err)
testState.assertFailedWithFatal()
assert.Equal(404, response.StatusCode)
call.Status(http.StatusNotFound)
}

func TestApiStubbedEndpoint(t *testing.T) {
Expand All @@ -45,11 +47,10 @@ func TestApiStubbedEndpoint(t *testing.T) {
})

// Act
e := httpexpect.Default(t, mockedAPI.GetURL().String())
call := mockedAPI.testCall(http.MethodGet, "/endpoint", t)

// Assert
e.GET("/endpoint").
Expect().
call.
Status(http.StatusCreated).
Body().IsEqual("Hello")

Expand All @@ -70,25 +71,22 @@ func TestApiStubbedEndpointWithJson(t *testing.T) {
}{Value: "Hello"})

// Act
e := httpexpect.Default(t, mockedAPI.GetURL().String())
call := mockedAPI.testCall(http.MethodGet, "/endpoint", t)

// Assert
testState.assertDidNotFailed()

e.GET("/endpoint").
Expect().
Header("Content-Type").IsEqual("application/json")
call.Header("Content-Type").IsEqual("application/json")

responseObject := e.GET("/endpoint").
Expect().
responseObject := call.
Status(http.StatusOK).
JSON().Object()

responseObject.Value("value").IsEqual("Hello")
}

func TestApiStubbedEndpointWithBody(t *testing.T) {
t.Parallel()
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
Expand All @@ -100,17 +98,37 @@ func TestApiStubbedEndpointWithBody(t *testing.T) {
WithBody(http.StatusOK, body, "text/plain")

// Act
e := httpexpect.Default(t, mockedAPI.GetURL().String())
call := mockedAPI.testCall(http.MethodGet, "/endpoint", t)

// Assert
testState.assertDidNotFailed()

e.GET("/endpoint").
Expect().
Header("Content-Type").IsEqual("text/plain")
call.Header("Content-Type").IsEqual("text/plain")

e.GET("/endpoint").
Expect().
call.
Status(http.StatusOK).
Body().IsEqual("Hello!")
}

func TestApiStubbedEndpointWithDelay(t *testing.T) {
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
defer func() { mockedAPI.Close() }()
body := []byte("Hello!")

stubbedDelay := 500 * time.Millisecond

mockedAPI.
Stub(http.MethodPost, "/endpoint").
WithDelay(stubbedDelay).
WithBody(http.StatusOK, body, "text/plain")

// Act
call := mockedAPI.testCall(http.MethodPost, "/endpoint", t)

// Assert
testState.assertDidNotFailed()
call.RoundTripTime().Ge(stubbedDelay)
}

0 comments on commit 396d539

Please sign in to comment.