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
Guess the scheme if r.URL.Scheme is unset #474
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc6bf41
Guess the scheme if r.URL.Scheme is unset
euank 3d555f2
[doc] Add more detail to Schemes and URL godocs
euank a038e0d
Add route url test for schemes
euank 7b0a1dc
Make httpserver test use more specific scheme matchers
euank 6a87310
Update test to have different responses per route
euank File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// +build go1.9 | ||
|
||
package mux | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestSchemeMatchers(t *testing.T) { | ||
httpRouter := NewRouter() | ||
httpRouter.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { | ||
rw.Write([]byte("hello world")) | ||
}).Schemes("http") | ||
httpsRouter := NewRouter() | ||
httpsRouter.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { | ||
rw.Write([]byte("hello world")) | ||
}).Schemes("https") | ||
|
||
assertHelloWorldResponse := func(t *testing.T, s *httptest.Server) { | ||
resp, err := s.Client().Get(s.URL) | ||
if err != nil { | ||
t.Fatalf("unexpected error getting from server: %v", err) | ||
} | ||
if resp.StatusCode != 200 { | ||
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode) | ||
} | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatalf("unexpected error reading body: %v", err) | ||
} | ||
if !bytes.Equal(body, []byte("hello world")) { | ||
t.Fatalf("response should be hello world, was: %q", string(body)) | ||
} | ||
} | ||
|
||
t.Run("httpServer", func(t *testing.T) { | ||
s := httptest.NewServer(httpRouter) | ||
defer s.Close() | ||
assertHelloWorldResponse(t, s) | ||
}) | ||
t.Run("httpsServer", func(t *testing.T) { | ||
s := httptest.NewTLSServer(httpsRouter) | ||
defer s.Close() | ||
assertHelloWorldResponse(t, s) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would it make sense for this test to have different responses for the different schemes so we can use that to differentiate? I believe this test would pass if the httpsServer subtest was matching on the http route.
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.
I think you're right. I made it so there's one router that has both matchers, and that each matcher has a different response. I think that should better test for what we care about.
Thanks for the review!