You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The provided DefaultMatcher only compares the method and URL of the request. In the general case, there are various conditions where this isn't enough to uniquely identify a request:
Two different PUT requests with different bodies. Let's say the server expects a YAML, one has a valid YAML body, the other does not, the cassettes should differentiate both.
The request uses headers to define the expected response type, eg Accept: application/json or Accept: application/x-yaml, the cassettes should give back different responses.
While in some particular cases, a permissive matcher may work, in the general case, it is bound to yield inconsistent test signal.
I have written the following FullRequestMatcher matcher below, which compares "the whole request", so there's no chance of a mismatch on the cassette. Arguably, this should replace DefaultMatcher (with a majon release), but there could still be value on adding it alongside DefaultMatcher.
WDYT?
// Similar to reflect.DeepEqual, but considers the contents of collections, so {} and nil would be// considered equal. works with Array, Map, Slice, or pointer to Array.funcdeepEqualContents(x, yany) bool {
ifreflect.ValueOf(x).IsNil() {
ifreflect.ValueOf(y).IsNil() {
returntrue
} else {
returnreflect.ValueOf(y).Len() ==0
}
} else {
ifreflect.ValueOf(y).IsNil() {
returnreflect.ValueOf(x).Len() ==0
} else {
returnreflect.DeepEqual(x, y)
}
}
}
funcbodyMatches(request*http.Request, cassetteRequest cassette.Request) bool {
ifrequest.Body!=nil {
varbuffer bytes.Bufferif_, err:=buffer.ReadFrom(request.Body); err!=nil {
panic(fmt.Sprintf("failed to read %s %s body: %s", request.Method, request.URL.String(), err))
}
request.Body=io.NopCloser(bytes.NewBuffer(buffer.Bytes()))
ifbuffer.String() !=cassetteRequest.Body {
returnfalse
}
} else {
iflen(cassetteRequest.Body) !=0 {
returnfalse
}
}
returntrue
}
// The DefaultMatcher only cherks for method and URL, which isn't enough to uniquely identify// requsets. Thus function provides a matcher that matches the whole request, preventing any// request/response mismatches from happening.funcFullRequestMatcher(request*http.Request, cassetteRequest cassette.Request) bool {
ifrequest.Method!=cassetteRequest.Method {
returnfalse
}
ifrequest.URL.String() !=cassetteRequest.URL {
returnfalse
}
ifrequest.ProtoMajor!=cassetteRequest.ProtoMajor {
returnfalse
}
ifrequest.ProtoMinor!=cassetteRequest.ProtoMinor {
returnfalse
}
if!deepEqualContents(request.Header, cassetteRequest.Headers) {
returnfalse
}
if!bodyMatches(request, cassetteRequest) {
returnfalse
}
ifrequest.ContentLength!=cassetteRequest.ContentLength {
returnfalse
}
if!deepEqualContents(request.TransferEncoding, cassetteRequest.TransferEncoding) {
returnfalse
}
ifrequest.Host!=cassetteRequest.Host {
returnfalse
}
if!deepEqualContents(request.Trailer, cassetteRequest.Trailer) {
returnfalse
}
ifrequest.RemoteAddr!=cassetteRequest.RemoteAddr {
returnfalse
}
ifrequest.RequestURI!=cassetteRequest.RequestURI {
returnfalse
}
returntrue
}
The text was updated successfully, but these errors were encountered:
The provided
DefaultMatcher
only compares the method and URL of the request. In the general case, there are various conditions where this isn't enough to uniquely identify a request:Accept: application/json
orAccept: application/x-yaml
, the cassettes should give back different responses.While in some particular cases, a permissive matcher may work, in the general case, it is bound to yield inconsistent test signal.
I have written the following
FullRequestMatcher
matcher below, which compares "the whole request", so there's no chance of a mismatch on the cassette. Arguably, this should replaceDefaultMatcher
(with a majon release), but there could still be value on adding it alongsideDefaultMatcher
.WDYT?
The text was updated successfully, but these errors were encountered: