Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stub with delay #9

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
61 changes: 39 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 {
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,36 @@ 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() }()

stubbedDelay := 500 * time.Millisecond

mockedAPI.
Stub(http.MethodPost, "/delayed").
WithDelay(stubbedDelay).
WithStatusCode(http.StatusOK)

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

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