-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce new matchers feature
It is now possible to create a Responder based on Request content (header or body for example), so not only on method and URL as before. Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
- Loading branch information
Showing
9 changed files
with
2,000 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package httpmock | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"reflect" | ||
"sync/atomic" | ||
|
||
"github.com/jarcoal/httpmock/internal" | ||
) | ||
|
||
var ( | ||
GetPackage = getPackage | ||
ExtractPackage = extractPackage | ||
CalledFrom = calledFrom | ||
) | ||
|
||
type ( | ||
MatchResponder = matchResponder | ||
MatchResponders = matchResponders | ||
) | ||
|
||
func init() { | ||
atomic.AddInt64(&matcherID, 0xabcdef) | ||
} | ||
|
||
func GetIgnorePackages() map[string]bool { | ||
return ignorePackages | ||
} | ||
|
||
// bodyCopyOnRead | ||
|
||
func NewBodyCopyOnRead(body io.ReadCloser) *bodyCopyOnRead { //nolint: revive | ||
return &bodyCopyOnRead{body: body} | ||
} | ||
|
||
func (b *bodyCopyOnRead) Body() io.ReadCloser { | ||
return b.body | ||
} | ||
|
||
func (b *bodyCopyOnRead) Buf() []byte { | ||
return b.buf | ||
} | ||
|
||
func (b *bodyCopyOnRead) Rearm() { | ||
b.rearm() | ||
} | ||
|
||
// matchRouteKey | ||
|
||
func NewMatchRouteKey(rk internal.RouteKey, name string) matchRouteKey { //nolint: revive | ||
return matchRouteKey{RouteKey: rk, name: name} | ||
} | ||
|
||
// matchResponder | ||
|
||
func NewMatchResponder(matcher Matcher, resp Responder) matchResponder { //nolint: revive | ||
return matchResponder{matcher: matcher, responder: resp} | ||
} | ||
|
||
func (mr matchResponder) ResponderPointer() uintptr { | ||
return reflect.ValueOf(mr.responder).Pointer() | ||
} | ||
|
||
func (mr matchResponder) Matcher() Matcher { | ||
return mr.matcher | ||
} | ||
|
||
// matchResponders | ||
|
||
func (mrs matchResponders) Add(mr matchResponder) matchResponders { | ||
return mrs.add(mr) | ||
} | ||
|
||
func (mrs matchResponders) Remove(name string) matchResponders { | ||
return mrs.remove(name) | ||
} | ||
|
||
func (mrs matchResponders) FindMatchResponder(req *http.Request) *matchResponder { | ||
return mrs.findMatchResponder(req) | ||
} | ||
|
||
// Matcher | ||
|
||
func (m Matcher) FnPointer() uintptr { | ||
return reflect.ValueOf(m.fn).Pointer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.