-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Mock transport with pc frames #2008
Mock transport with pc frames #2008
Conversation
I've added the code generation to all methods now. Let me know your thoughts @gmlewis 👍 . |
Codecov Report
@@ Coverage Diff @@
## master #2008 +/- ##
==========================================
- Coverage 97.84% 97.44% -0.40%
==========================================
Files 105 108 +3
Lines 6809 6968 +159
==========================================
+ Hits 6662 6790 +128
- Misses 80 110 +30
- Partials 67 68 +1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @migueleliasweb . I'm liking this approach.
You will also want to add a new generator to this PR here:
https://github.com/google/go-github/blob/master/github/github.go#L6-L7
|
||
func MustMarshal(v interface{}) []byte { | ||
b, err := json.Marshal(v) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this blank line.
} | ||
} | ||
|
||
func NewMockHttpClient(options ...MockHttpClientOption) *http.Client { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add godoc comment.
func NewMockHttpClient(options ...MockHttpClientOption) *http.Client { | |
func NewMockHTTPClient(options ...MockHTTPClientOption) *http.Client { |
|
||
type MockHttpClientOption func(*MockRoundTripper) | ||
|
||
func WithRequestMatch( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add godoc comment.
|
||
var _ http.RoundTripper = &MockRoundTripper{} | ||
|
||
type MockHttpClientOption func(*MockRoundTripper) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add godoc comment.
type MockHttpClientOption func(*MockRoundTripper) | |
type MockHTTPClientOption func(*MockRoundTripper) |
RequestMocks map[RequestMatch][][]byte | ||
} | ||
|
||
// RoundTrip implements http.RoundTripper interface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// RoundTrip implements http.RoundTripper interface | |
// RoundTrip implements http.RoundTripper interface. |
|
||
if receiverType, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok { | ||
if receiverTypeIndent, ok := receiverType.X.(*ast.Ident); ok { | ||
if receiverTypeIndent.Name != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can then be deleted if you take my suggestions above.
if receiverTypeIndent.Name != "" { | ||
varName := fmt.Sprintf( | ||
"RequestMatch%s%s", | ||
strings.Replace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the reduction in indentation above, this strings.Replace
could all fit nicely on a single line.
return requestMatches[i].Value < requestMatches[j].Value | ||
}) | ||
|
||
// fmt.Println("requestMatches:", requestMatches) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this debugging line.
|
||
// fmt.Println("requestMatches:", requestMatches) | ||
|
||
buf := bytes.NewBufferString("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buf := bytes.NewBufferString("") | |
var buf bytes.Buffer |
buf := bytes.NewBufferString("") | ||
|
||
template.Must(template.New("source").Parse(source)).Execute( | ||
buf, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buf, | |
&buf, |
After further consideration, I think that @willnorris is correct in his comment that this would be a great utility to place in an external helper repo. So my recommendation is that we close this PR and also close #1800 and point to a new helper repo in this repo's README.md once it has been implemented. |
An attempt to address #1800
runtime.Frames
to get a the caller method in the SDK and use it to mock a response