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
RedirectTo matcher #341
Comments
Hi @mapreal19, can you show me what your custom matcher code looks like? It seems like we'd have to be quite opinionated on the type that we expect i.e. Are there other structures common in the Go ecosystem to represent responses? Do they have |
Sure: package matcher
import (
"fmt"
"github.com/onsi/gomega/types"
"net/http/httptest"
"reflect"
)
type redirectToMatcher struct {
expected interface{}
}
func RedirectTo(expected interface{}) types.GomegaMatcher {
return &redirectToMatcher{
expected: expected,
}
}
func (matcher *redirectToMatcher) Match(actual interface{}) (success bool, err error) {
response, ok := actual.(*httptest.ResponseRecorder)
if !ok {
return false, fmt.Errorf("RedirectTo matcher expects an http.Response")
}
location := getLocation(response)
success = reflect.DeepEqual(location, matcher.expected)
return success, nil
}
func (matcher *redirectToMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf(
"Expected response to redirect to %#v, recieved: %#v",
matcher.expected,
getLocation(actual.(*httptest.ResponseRecorder)),
)
}
func (matcher *redirectToMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf(
"Expected response not to redirect to %#v, recieved: %#v",
matcher.expected,
getLocation(actual.(*httptest.ResponseRecorder)),
)
}
func getLocation(response *httptest.ResponseRecorder) string {
return response.Header().Get("Location")
} I'm assuming |
@williammartin what if for now we support only |
@mapreal19 thanks for opening the issue and sending the code. I have a couple of comments:
|
For 2. maybe, in all my tests I only used a For me it was just convenient, it's a minimal implementation. My hope is that others will contribute with more complex edge cases. |
Inspired by: https://relishapp.com/rspec/rspec-rails/docs/matchers/redirect-to-matcher
Does it make sense to have this matcher? Currently in my tests:
But I was able to use a custom matcher so the code becomes simpler:
The text was updated successfully, but these errors were encountered: