@@ -288,7 +288,7 @@ func testHostHandlers(t *testing.T, mode testMode) {
288288 if s != vt .expected {
289289 t .Errorf ("Get(%q) = %q, want %q" , vt .url , s , vt .expected )
290290 }
291- case StatusMovedPermanently :
291+ case StatusTemporaryRedirect :
292292 s := r .Header .Get ("Location" )
293293 if s != vt .expected {
294294 t .Errorf ("Get(%q) = %q, want %q" , vt .url , s , vt .expected )
@@ -340,7 +340,7 @@ var serveMuxTests = []struct {
340340 pattern string
341341}{
342342 {"GET" , "google.com" , "/" , 404 , "" },
343- {"GET" , "google.com" , "/dir" , 301 , "/dir/" },
343+ {"GET" , "google.com" , "/dir" , 307 , "/dir/" },
344344 {"GET" , "google.com" , "/dir/" , 200 , "/dir/" },
345345 {"GET" , "google.com" , "/dir/file" , 200 , "/dir/" },
346346 {"GET" , "google.com" , "/search" , 201 , "/search" },
@@ -354,14 +354,14 @@ var serveMuxTests = []struct {
354354 {"GET" , "images.google.com" , "/search" , 201 , "/search" },
355355 {"GET" , "images.google.com" , "/search/" , 404 , "" },
356356 {"GET" , "images.google.com" , "/search/foo" , 404 , "" },
357- {"GET" , "google.com" , "/../search" , 301 , "/search" },
358- {"GET" , "google.com" , "/dir/.." , 301 , "" },
359- {"GET" , "google.com" , "/dir/.." , 301 , "" },
360- {"GET" , "google.com" , "/dir/./file" , 301 , "/dir/" },
357+ {"GET" , "google.com" , "/../search" , 307 , "/search" },
358+ {"GET" , "google.com" , "/dir/.." , 307 , "" },
359+ {"GET" , "google.com" , "/dir/.." , 307 , "" },
360+ {"GET" , "google.com" , "/dir/./file" , 307 , "/dir/" },
361361
362362 // The /foo -> /foo/ redirect applies to CONNECT requests
363363 // but the path canonicalization does not.
364- {"CONNECT" , "google.com" , "/dir" , 301 , "/dir/" },
364+ {"CONNECT" , "google.com" , "/dir" , 307 , "/dir/" },
365365 {"CONNECT" , "google.com" , "/../search" , 404 , "" },
366366 {"CONNECT" , "google.com" , "/dir/.." , 200 , "/dir/" },
367367 {"CONNECT" , "google.com" , "/dir/.." , 200 , "/dir/" },
@@ -454,7 +454,7 @@ func TestServeMuxHandlerRedirects(t *testing.T) {
454454 h , _ := mux .Handler (r )
455455 rr := httptest .NewRecorder ()
456456 h .ServeHTTP (rr , r )
457- if rr .Code != 301 {
457+ if rr .Code != 307 {
458458 if rr .Code != tt .code {
459459 t .Errorf ("%s %s %s = %d, want %d" , tt .method , tt .host , tt .url , rr .Code , tt .code )
460460 }
@@ -473,6 +473,37 @@ func TestServeMuxHandlerRedirects(t *testing.T) {
473473 }
474474}
475475
476+ func TestServeMuxHandlerRedirectPost (t * testing.T ) {
477+ setParallel (t )
478+ mux := NewServeMux ()
479+ mux .HandleFunc ("POST /test/" , func (w ResponseWriter , r * Request ) {
480+ w .WriteHeader (200 )
481+ })
482+
483+ var code , retries int
484+ startURL := "http://example.com/test"
485+ reqURL := startURL
486+ for retries = 0 ; retries <= 1 ; retries ++ {
487+ r := httptest .NewRequest ("POST" , reqURL , strings .NewReader ("hello world" ))
488+ h , _ := mux .Handler (r )
489+ rr := httptest .NewRecorder ()
490+ h .ServeHTTP (rr , r )
491+ code = rr .Code
492+ switch rr .Code {
493+ case 307 :
494+ reqURL = rr .Result ().Header .Get ("Location" )
495+ continue
496+ case 200 :
497+ // ok
498+ default :
499+ t .Errorf ("unhandled response code: %v" , rr .Code )
500+ }
501+ }
502+ if code != 200 {
503+ t .Errorf ("POST %s = %d after %d retries, want = 200" , startURL , code , retries )
504+ }
505+ }
506+
476507// Tests for https://golang.org/issue/900
477508func TestMuxRedirectLeadingSlashes (t * testing.T ) {
478509 setParallel (t )
@@ -492,8 +523,8 @@ func TestMuxRedirectLeadingSlashes(t *testing.T) {
492523 return
493524 }
494525
495- if code , expected := resp .Code , StatusMovedPermanently ; code != expected {
496- t .Errorf ("Expected response code of StatusMovedPermanently ; got %d" , code )
526+ if code , expected := resp .Code , StatusTemporaryRedirect ; code != expected {
527+ t .Errorf ("Expected response code of StatusPermanentRedirect ; got %d" , code )
497528 return
498529 }
499530 }
@@ -579,18 +610,18 @@ func TestServeWithSlashRedirectForHostPatterns(t *testing.T) {
579610 want string
580611 }{
581612 {"GET" , "http://example.com/" , 404 , "" , "" },
582- {"GET" , "http://example.com/pkg/foo" , 301 , "/pkg/foo/" , "" },
613+ {"GET" , "http://example.com/pkg/foo" , 307 , "/pkg/foo/" , "" },
583614 {"GET" , "http://example.com/pkg/bar" , 200 , "" , "example.com/pkg/bar" },
584615 {"GET" , "http://example.com/pkg/bar/" , 200 , "" , "example.com/pkg/bar/" },
585- {"GET" , "http://example.com/pkg/baz" , 301 , "/pkg/baz/" , "" },
586- {"GET" , "http://example.com:3000/pkg/foo" , 301 , "/pkg/foo/" , "" },
616+ {"GET" , "http://example.com/pkg/baz" , 307 , "/pkg/baz/" , "" },
617+ {"GET" , "http://example.com:3000/pkg/foo" , 307 , "/pkg/foo/" , "" },
587618 {"CONNECT" , "http://example.com/" , 404 , "" , "" },
588619 {"CONNECT" , "http://example.com:3000/" , 404 , "" , "" },
589620 {"CONNECT" , "http://example.com:9000/" , 200 , "" , "example.com:9000/" },
590- {"CONNECT" , "http://example.com/pkg/foo" , 301 , "/pkg/foo/" , "" },
621+ {"CONNECT" , "http://example.com/pkg/foo" , 307 , "/pkg/foo/" , "" },
591622 {"CONNECT" , "http://example.com:3000/pkg/foo" , 404 , "" , "" },
592- {"CONNECT" , "http://example.com:3000/pkg/baz" , 301 , "/pkg/baz/" , "" },
593- {"CONNECT" , "http://example.com:3000/pkg/connect" , 301 , "/pkg/connect/" , "" },
623+ {"CONNECT" , "http://example.com:3000/pkg/baz" , 307 , "/pkg/baz/" , "" },
624+ {"CONNECT" , "http://example.com:3000/pkg/connect" , 307 , "/pkg/connect/" , "" },
594625 }
595626
596627 for i , tt := range tests {
@@ -6940,7 +6971,7 @@ func TestMuxRedirectRelative(t *testing.T) {
69406971 if got , want := resp .Header ().Get ("Location" ), "/" ; got != want {
69416972 t .Errorf ("Location header expected %q; got %q" , want , got )
69426973 }
6943- if got , want := resp .Code , StatusMovedPermanently ; got != want {
6974+ if got , want := resp .Code , StatusTemporaryRedirect ; got != want {
69446975 t .Errorf ("Expected response code %d; got %d" , want , got )
69456976 }
69466977}
0 commit comments