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

MinTimes and MaxTimes responder invocations #148

Open
ilya-hontarau opened this issue Aug 29, 2023 · 5 comments
Open

MinTimes and MaxTimes responder invocations #148

ilya-hontarau opened this issue Aug 29, 2023 · 5 comments

Comments

@ilya-hontarau
Copy link

As I can see, there is no way to set upper bound and lower bound of responder invocations, only Once() and exact amount with Times().
What do you think about adding these helper functions, like MinTimes() and MaxTimes()?

@maxatome
Copy link
Collaborator

maxatome commented Sep 5, 2023

Hello, and what should reply httpmock before MinTimes() invocations are reached?

@ilya-hontarau
Copy link
Author

@maxatome I would expect the same behavior it has now for Times(), throwing panic

@maxatome
Copy link
Collaborator

maxatome commented Sep 6, 2023

Hello, do you have a use case for this behavior?
As you will receive errors for the first MinTimes()-1 calls before receiving the first good response.

@ilya-hontarau
Copy link
Author

As you will receive errors for the first MinTimes()-1 calls before receiving the first good response.

What I thought, this check can be placed in a defer function, so it can assert in the end of the execution that min times isn't reached.

do you have a use case

I can see it useful, when testing logic with a undetermenistic behaivor, as example logic of pereodic pulling, retries.

@maxatome
Copy link
Collaborator

maxatome commented Sep 6, 2023

As a Responder is a function (for historical reasons) it is not possible without doing dirty things to know how many times a Responder has been called.

But you can have a specific function to "track" a responder:

func Track(calls *int, r Responder) Responder {
	return func(req *http.Request) (*http.Response, error) {
		*calls++
		return r(req)
	}
}

this way you can know how many times it has been called, but explicitly:

var num int
r := Track(&num, httpmock.NewStringResponder(200, "OK))
httpmock.RegisterResponder("GET", "/foo", r)
httpmock.RegisterResponder("GET", "/bar", r)
// do your calls
fmt.Printf("The responder has been called %d times\n", num)

note that if you want to know how many times a URL has been mocked, you can simply use GetCallCountInfo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants