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

FIXIT: refactor http testing functions to builder pattern. Fixes #426 #440

Merged
merged 1 commit into from
Jun 10, 2020

Conversation

banaag
Copy link
Collaborator

@banaag banaag commented Jun 10, 2020

Refactor http testing functions

func GetHH(t *testing.T, handler http.Handler, target string, host string, headers http.Header) *http.Response {
return GetBHH(t, handler, target, host, nil, headers)
// NewBody sets body related information for the request.
func (r *Request) NewBody(body io.Reader) *Request {
Copy link
Member

Choose a reason for hiding this comment

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

Ditto; rename SetBody or Body or AddBody.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

func GetH(t *testing.T, handler http.Handler, target string, headers http.Header) *http.Response {
return GetBHH(t, handler, target, "", nil, headers)
// NewHeader sets header related information for the request.
func (r *Request) NewHeader(host string, header http.Header) *Request {
Copy link
Member

Choose a reason for hiding this comment

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

I would rename SetHeaders or Headers. New makes me think it returns the headers. And plural because it is all the headers; not meant to be called incrementally for each one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@@ -90,17 +90,17 @@ func (this *SignerSuite) new(urlSets []util.URLSet) http.Handler {
}

func (this *SignerSuite) get(t *testing.T, handler http.Handler, target string) *http.Response {
return pkgt.GetH(t, handler, target, http.Header{
"AMP-Cache-Transform": {"google"}, "Accept": {"application/signed-exchange;v=" + accept.AcceptedSxgVersion}})
header := http.Header{"AMP-Cache-Transform": {"google"}, "Accept": {"application/signed-exchange;v=" + accept.AcceptedSxgVersion}}
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to get rid of these three cryptic get functions, too? Just make this header value a top-level var inside signer_test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done (whew!)

}

func GetHH(t *testing.T, handler http.Handler, target string, host string, headers http.Header) *http.Response {
return GetBHH(t, handler, target, host, nil, headers)
// NewBody sets body related information for the request.
Copy link
Member

Choose a reason for hiding this comment

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

"NewBody sets the body for the request. May be nil for an empty body."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

}

func GetH(t *testing.T, handler http.Handler, target string, headers http.Header) *http.Response {
return GetBHH(t, handler, target, "", nil, headers)
// NewHeader sets header related information for the request.
Copy link
Member

Choose a reason for hiding this comment

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

"NewHeader sets the headers for the request. Host may be empty for the default."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

}

func GetBHH(t *testing.T, handler http.Handler, target string, host string, body io.Reader, headers http.Header) *http.Response {
// Get returns the completed test request object.
func (r *Request) Get() *http.Response {
Copy link
Member

Choose a reason for hiding this comment

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

Rename this "Do", since it may do a post instead of a get.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

method = "POST"
headers.Set("Content-Type", "application/x-www-form-urlencoded")
r.NewHeader(r.Host, headers)
Copy link
Member

Choose a reason for hiding this comment

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

I think this'll overwrite any preexisting headers. e.g. if I call:

pkgt.NewRequest(t, handler, "/foo").
    NewHeader("", {{"foo": "bar"}}).
    NewBody(strings.NewReader("blah")).
    Get()

It won't send the foo header. I think you just want r.Header.Set(...) in the line above, and then you can get rid of the local headers variable and this NewHeader call.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@twifkak twifkak left a comment

Choose a reason for hiding this comment

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

Thanks!

func (this *SignerSuite) getB(t *testing.T, handler http.Handler, target string, body string) *http.Response {
return pkgt.GetBHH(t, handler, target, "", strings.NewReader(body), http.Header{
"AMP-Cache-Transform": {"google"}, "Accept": {"application/signed-exchange;v=" + accept.AcceptedSxgVersion}})
func (this *SignerSuite) getX(t *testing.T, handler http.Handler, target string) *http.Response {
Copy link
Member

Choose a reason for hiding this comment

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

Oops, I think you accidentally left this unused getX behind.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, removed.

@banaag banaag merged commit ec46ffd into master Jun 10, 2020
@banaag banaag deleted the fixit-test-refactor branch June 10, 2020 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants