Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:

env:
GO_VERSION: "1.19.x"
GO_VERSION: "1.20.x"

jobs:
lint:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ on:

env:
GO111MODULE: "on"
GO_LATEST_VERSION: "1.19.x"
GO_LATEST_VERSION: "1.20.x"

jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, macos-latest ]
go-version: [ 1.18.x, 1.19.x ]
go-version: [ 1.18.x, 1.19.x , 1.20.x ]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ MODULE_NAME=httpmock

VENDOR_DIR = vendor

GOLANGCI_LINT_VERSION ?= v1.50.1
GOLANGCI_LINT_VERSION ?= v1.51.1

GO ?= go
GOLANGCI_LINT ?= $(shell go env GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION)
Expand Down
59 changes: 33 additions & 26 deletions expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ type Expectation interface {
After(d time.Duration) Expectation
}

// ExpectationHandler handles the expectation.
type ExpectationHandler interface {
Handle(http.ResponseWriter, *http.Request, map[string]string) error
}

var (
_ Expectation = (*requestExpectation)(nil)
_ planner.Expectation = (*requestExpectation)(nil)
Expand Down Expand Up @@ -162,7 +167,7 @@ type requestExpectation struct {
fulfilledTimes uint
repeatTimes uint

delay delayer
delayer delayer
}

func (e *requestExpectation) lock() {
Expand Down Expand Up @@ -453,7 +458,7 @@ func (e *requestExpectation) WaitUntil(w <-chan time.Time) Expectation {
e.lock()
defer e.unlock()

e.delay = waitForSignal(w)
e.delayer = waitForSignal(w)

return e
}
Expand All @@ -469,7 +474,7 @@ func (e *requestExpectation) After(d time.Duration) Expectation {
e.lock()
defer e.unlock()

e.delay = waitForDuration(d)
e.delayer = waitForDuration(d)

return e
}
Expand All @@ -479,7 +484,7 @@ func (e *requestExpectation) Handle(w http.ResponseWriter, req *http.Request, de
e.lock()
defer e.unlock()

if err := e.delay(req.Context()); err != nil {
if err := e.delayer.Delay(req.Context()); err != nil {
return err
}

Expand All @@ -501,35 +506,37 @@ func (e *requestExpectation) Handle(w http.ResponseWriter, req *http.Request, de
return err
}

type delayer func(ctx context.Context) error
type delayer interface {
Delay(ctx context.Context) error
}

func noWait() delayer {
return func(ctx context.Context) error {
return ctx.Err()
}
type noWait struct{}

func (noWait) Delay(ctx context.Context) error {
return ctx.Err()
}

func waitForSignal(c <-chan time.Time) delayer {
return func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
type waitForSignal <-chan time.Time

func (t waitForSignal) Delay(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()

case <-c:
return nil
}
case <-t:
return nil
}
}

func waitForDuration(d time.Duration) delayer {
return func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
type waitForDuration time.Duration

func (d waitForDuration) Delay(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()

case <-time.After(d):
return nil
}
case <-time.After(time.Duration(d)):
return nil
}
}

Expand All @@ -541,7 +548,7 @@ func newRequestExpectation(method string, requestURI any) *requestExpectation {
responseCode: http.StatusOK,
requestURIMatcher: matcher.Match(requestURI),
repeatTimes: 0,
delay: noWait(),
delayer: noWait{},
handle: func(r *http.Request) ([]byte, error) {
return nil, nil
},
Expand Down
2 changes: 2 additions & 0 deletions mock/httpmock/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package httpmock provides functionalities for mocking the httpmock components.
package httpmock
Loading