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

Let the Doer Do it #115

Merged
merged 2 commits into from
Sep 27, 2021
Merged

Let the Doer Do it #115

merged 2 commits into from
Sep 27, 2021

Conversation

StevenACoffman
Copy link
Member

@StevenACoffman StevenACoffman commented Sep 27, 2021

This PR introduces switches to using the common interface Doer instead of the concrete *http.Client to be more flexible for consumers test construction.

Prior to this PR, genqlient has not supported one of the three common testing techniques by consumers. See Let the Doer Do it for more details.


Generally, there are three common ways to test HTTP client requests:

  1. By Replacing http.Transport
    Transport specifies the mechanism by which individual HTTP requests are made. Instead of using the default http.Transport, we’ll replace it with our own implementation. To implement a transport, we’ll have to implement http.RoundTripper interface.
  2. Using httptest.Server:
    httptest.Server allows us to create a local HTTP server and listen for any requests. When starting, the server chooses any available open port and uses that. So we need to get the URL of the test server and use it instead of the actual service URL.
  3. Accept a Doer as a parameter instead of *http.Client
    The Doer is a single-method interface, as is often the case in Go:
type Doer interface {
    Do(*http.Request) (*http.Response, error)
}

Note: It does not really exist - it is not defined anywhere in the stdlib - but it is trivial to summon it from thin air in a package. Many people do as I did above, and because Go implicitly satisfies interfaces, any type that implements this method will be a valid Doer.

Signed-off-by: Steve Coffman <[email protected]>
@StevenACoffman StevenACoffman changed the title Let the Doer do it Let the Doer Do it Sep 27, 2021
Copy link
Collaborator

@benjaminjkraft benjaminjkraft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok -- I'm not sure I agree that this is the best way to mock, but I don't think this is a place for me to be a stickler :-) .

graphql/client.go Outdated Show resolved Hide resolved
graphql/client.go Show resolved Hide resolved
Signed-off-by: Steve Coffman <[email protected]>
@StevenACoffman StevenACoffman merged commit 47e9cea into main Sep 27, 2021
@StevenACoffman StevenACoffman deleted the let_the_doer_do_it branch September 27, 2021 17:48
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

Successfully merging this pull request may close these issues.

2 participants