}
}
+// Issue 23351: document and test behavior of ServeMux with ports
+func TestStripPortFromHost(t *testing.T) {
+ mux := NewServeMux()
+
+ mux.HandleFunc("example.com/", func(w ResponseWriter, r *Request) {
+ fmt.Fprintf(w, "OK")
+ })
+ mux.HandleFunc("example.com:9000/", func(w ResponseWriter, r *Request) {
+ fmt.Fprintf(w, "uh-oh!")
+ })
+
+ req := httptest.NewRequest("GET", "http://example.com:9000/", nil)
+ rw := httptest.NewRecorder()
+
+ mux.ServeHTTP(rw, req)
+
+ response := rw.Body.String()
+ if response != "OK" {
+ t.Errorf("Response gotten was %q", response)
+ }
+}
+
func BenchmarkResponseStatusLine(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
// "/codesearch" and "codesearch.google.com/" without also taking over
// requests for "http://www.google.com/".
//
-// ServeMux also takes care of sanitizing the URL request path,
-// redirecting any request containing . or .. elements or repeated slashes
-// to an equivalent, cleaner URL.
+// ServeMux also takes care of sanitizing the URL request path and the Host
+// header, stripping the port number and redirecting any request containing . or
+// .. elements or repeated slashes to an equivalent, cleaner URL.
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry