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

refactored caddytest helpers #3285

Merged
merged 2 commits into from
Apr 27, 2020
Merged

Conversation

sarge
Copy link
Collaborator

@sarge sarge commented Apr 20, 2020

Refactored the caddytests helpers. I am shooting for making the simple cases easy, and the complex ones possible.

I have exposed a low level function

func AssertResponseCode(t *testing.T, req *http.Request, expectedStatusCode int) *http.Response {

And have build a some of the higher level functions for some of the common cases. Building wrappers for all of the http verbs seems unnecessary.

@greenpau would you mind having a look and seeing if these are suitable?

Copy link

@greenpau greenpau left a comment

Choose a reason for hiding this comment

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

@sarge , thank you for exposing CreateTestingTransport. I will contribute the AssertPostResponseBody in a bit.

Generally, I think client needs to be exposed to give full control of it to a tester. For example setting user agent ahead of time and keeping the client state as one progresses through tests.

I propose it to look something like this.

type TestClient struct {
  client *http.Client // not being exposed
  t *testing.T 
}

func NewTestClient(t *testing.T) *TestClient {
  return &TestClient{
    client: &http.Client{
      CheckRedirect: redirectPolicyFunc,
      Transport:     CreateTestingTransport(),
  }
}

Then, if a tester want to use AssertGetResponse as it currently stands, they can.

func AssertGetResponse(t *testing.T, requestURI string, statusCode int, expectedBody string) (*http.Response, string) {

The TestClient gets its own method that

func (*TestClient) AssertGetResponse( ...
.... the client already has `t *testing.T`.

This would be backwards compatible because the logic for client is actually in AssertGetResponseBody. You extend AssertGetResponseBody to include client *TestClient:

func AssertGetResponseBody(client *TestClient, requestURI string, expectedStatusCode int) (*http.Response, string) {

After that, AssertGetResponse would look like this:

func AssertGetResponse(t *testing.T, requestURI string, statusCode int, expectedBody string) (*http.Response, string) {
        client := NewTestClient(t *testing.T)
	resp, body := AssertGetResponseBody(client, requestURI, statusCode)
	if !strings.Contains(body, expectedBody) {
		t.Errorf("requesting \"%s\" expected response body \"%s\" but got \"%s\"", requestURI, expectedBody, body)
	}
	return resp, body
}

Please let me know your thoughts.

@mholt mholt added the under review 🧐 Review is pending before merging label Apr 20, 2020
@sarge
Copy link
Collaborator Author

sarge commented Apr 24, 2020

@greenpau thanks for the feedback and suggestions. Interesting your observations about control over the http client. I had been considering an approach similar to the http.DefaultClient adding that as something you can do but I suspect seldom need to. It is global state, but it is familiar.

Something I just needed to look up was about 'keeping the client state as one progresses through tests'. As I understand it cookies won't be remembered, what other state were you hoping to track? In general useragent should not be encouraged to drive behaviour, though I can see in practice it might be. caddytest.DefaultClient would give you that.

I also wonder how deep into http testing caddy really needs to get. It seems we will need to make a call about all the other httpverbs? I definitely wanted to shoulder the complexity of starting and loading configuration.

Not many answers here I know

@greenpau
Copy link

@sarge , i implemented it here. Please Review #3299

@greenpau
Copy link

cookies won't be remembered

@sarge , I think with this approach you can extend it to have a cookie jar (https://golang.org/pkg/net/http/cookiejar/)

@sarge
Copy link
Collaborator Author

sarge commented Apr 26, 2020

@greenpau I have taken another pass through these changes. I had added a cookie jar and a functioning test. I have also added few more verbs.

Take a look I think I have captured the spirit of what you needed.

@greenpau
Copy link

Take a look I think I have captured the spirit of what you needed.

@sarge , LGTM 👍

Copy link

@greenpau greenpau left a comment

Choose a reason for hiding this comment

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

@sarge , LGTM 👍

@greenpau
Copy link

@sarge , I approved the changes, but it looks like it does not count myself as an approver.

cc: @mholt

@sarge sarge merged commit 570d84f into caddyserver:master Apr 27, 2020
@sarge sarge deleted the refactor_caddytest branch April 27, 2020 01:24
@mholt mholt removed the under review 🧐 Review is pending before merging label Apr 27, 2020
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.

None yet

3 participants