Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net/http): provide an option to access /index.html directly with…
…out 301 redirect For: golang#53870 BREAKING CHANGES: http.ServeFile won't redirect /index.html
- Loading branch information
Showing
4 changed files
with
76 additions
and
5 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package httptest_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestHttpFileServerSkipPermanentRedirect(t *testing.T) { | ||
URL, _ := url.Parse("/index.html") | ||
fs := http.Dir("./") | ||
respRecorder := httptest.NewRecorder() | ||
req := &http.Request{ | ||
Method: "GET", | ||
Proto: "HTTP/1.1", | ||
ProtoMajor: 1, | ||
ProtoMinor: 1, | ||
URL: URL, | ||
} | ||
|
||
fileServer := http.FileServer(fs) | ||
|
||
// skip 301 redirect | ||
fileServer.(*http.FileHandler).SkipPermanentRedirect() | ||
|
||
fileServer.ServeHTTP(respRecorder, req) | ||
if respRecorder.Code != 200 { | ||
t.Fatal("Expect code 200, result code: ", respRecorder.Code, "body response:", respRecorder.Body.String()) | ||
} | ||
|
||
bodyString := respRecorder.Body.String() | ||
if !strings.Contains(bodyString, "<body>") { | ||
t.Fatalf("Unexpected body response: %s", respRecorder.Body.String()) | ||
} | ||
|
||
} | ||
|
||
func TestHttpFileServerPermanentRedirect(t *testing.T) { | ||
URL, _ := url.Parse("/index.html") | ||
fs := http.Dir("./") | ||
respRecorder := httptest.NewRecorder() | ||
req := &http.Request{ | ||
Method: "GET", | ||
Proto: "HTTP/1.1", | ||
ProtoMajor: 1, | ||
ProtoMinor: 1, | ||
URL: URL, | ||
} | ||
|
||
fileServer := http.FileServer(fs) | ||
|
||
fileServer.ServeHTTP(respRecorder, req) | ||
if respRecorder.Code != 301 { | ||
t.Fatal("Expect code 301, result code: ", respRecorder.Code, "body:", respRecorder.Body.String()) | ||
} | ||
} |
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 @@ | ||
<body>index.html page</body> |