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

Add test for HandleHTTP #9

Merged
merged 6 commits into from Apr 3, 2016
Merged

Conversation

groyoh
Copy link
Member

@groyoh groyoh commented Apr 2, 2016

@h2non I'm not sure if these tests were properly handled. IMO it properly tests every part (writer, request, interception) of the method but there may be a cleaner/better way to do that.

I'll start working on the filters for the request once this is merged.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 1e1f7b4 on groyoh:add_test_for_handle_http into * on vinxi:master*.

req := &http.Request{Method: "OPTIONS", Header: make(http.Header)}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hi")
st.Expect(t, r.Header.Get("foo"), "")
Copy link
Member

Choose a reason for hiding this comment

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

Just one point: it's not supposed that you have intercepted the request adding the foo header? Why then the test asserts it as empty string? It shouldn't be present?

The response handler is called since the body is properly defined, but the header is not.

Copy link
Member Author

Choose a reason for hiding this comment

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

When a Header key is not present, calling Get on it, returns an empty string. Ref. https://golang.org/pkg/net/http/#Header.Get

I just realized that Header is actually a map so I could probably use _, ok := Header["foo"] and check the value of ok. Does that makes more sense to you?

Copy link
Member

Choose a reason for hiding this comment

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

No no, it's fine as it is. This is the expected behavior since Go it's strict in terms of type enforcement.

I was pointing that out just because I think the test is not working, since the request should be intercepted and the header should be added, but seems like it doesn't. In other words, seems like modifierFunc is not called.

Maybe I'm wrong because it's a bit late and my brain could not work very well :P

Copy link
Member Author

Choose a reason for hiding this comment

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

That's the expected behavior for HEAD and OPTIONS (or at least the behavior you wrote :D) https://github.com/vinxi/intercept/blob/master/request.go#L185

Copy link
Member

Choose a reason for hiding this comment

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

Damn, I suppose I was forgetting something 👍 .

In any case, this opened another discussion. Do you think it makes sense to ignore those request methods? HEAD method is not too much used, but useful in REST or RPC models to perform health checks or similar verification only requests. OPTIONS could be useful to modify CORS headers and perform further checks. Since now the devs can add custom filters or even they can use mux package to compose middleware based on specific filters in order to ignore certain traffic, so this could be useless and limiting for some devs. Opinions?

Copy link
Member Author

Choose a reason for hiding this comment

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

I also think that we should not ignore these methods by default. I actually planned to open an issue for this but I wanted to take a look again at the HTTP specs to make sure how these two methods work 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the PR to remove the HEAD/OPTIONS check.

@h2non
Copy link
Member

h2non commented Apr 3, 2016

I guess this is only for Request interceptor. It would be nice to have tests for the Response as well. But we can do it in separated PRs.

@groyoh
Copy link
Member Author

groyoh commented Apr 3, 2016

Yes, that's only for the request. I don't want to add too much at one cause I'm not always sure how to handle things.

m.Header.Set("foo", "bar")
})
w := utils.NewWriterStub()
req := &http.Request{Method: "HEAD", Header: make(http.Header)}
interceptor.HandleHTTP(w, req, handler)
st.Expect(t, string(w.Body), "Hi")
}

func TestHandleHTTPOPTIONSRequest(t *testing.T) {
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 test functions into TestHandleHTTPOptionsRequest.
It might be just a personal style preference, but for me the HTTP verb name is upper case mostly inside the HTTP protocol domain & spec, not strictly required in program semantics, such as methods/function names.
HTTP keyword instead is an acronym and the idiomatic Go also points about keeping acronyms with original case notations.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I agree. I thought about it when I was reviewing the PR but decided against because I didn't know what's the idiomatic way to write it in Go.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 58123eb on groyoh:add_test_for_handle_http into * on vinxi:master*.

@h2non h2non merged commit 923913c into vinxi:master Apr 3, 2016
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