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

is there any solution to generate response in the runtime? #107

Open
CodeplayerGm opened this issue Feb 3, 2023 · 1 comment
Open

is there any solution to generate response in the runtime? #107

CodeplayerGm opened this issue Feb 3, 2023 · 1 comment

Comments

@CodeplayerGm
Copy link

since the "ReplyFunc()" provide an entrance to return a response generated by custom function. however, the "mockGithubResponseHandler" executes when the mocker is registered.
is there any way to generate response in runtime, with parameter or options for "ReplyFunc()"?

gock.New("https://github.com").
  Post("/login/oauth/access_token").
  Persist().
  MatchParams(map[string]string{
    "client_id":     "Iv1.expire0000000000",
    "client_secret": "expire0000000000000000000000000000000000",
  }).
  ReplyFunc(mockGithubResponseHandler)

func mockGithubResponseHandler(r *gock.Response) {
  r.Status(200)
  r.SetHeader("Content-type", "application/json; charset=utf-8")
  if ExpireFlag {
    r.BodyString("{expire_at: 1670000000}")
  } else {
    r.BodyString("{expire_at: 0}")
  }
}
@h2non
Copy link
Owner

h2non commented Feb 3, 2023

You can use .Map() in order to update the *http.Response directly:
https://github.com/h2non/gock/blob/master/_examples/mapper/map.go

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