Follow-up for pull request #42: properly initialize Request.URL field

This commit is contained in:
Aliaksandr Valialkin
2016-01-28 11:31:53 +02:00
parent 4bc7f021b2
commit 2af858a7da
2 changed files with 16 additions and 3 deletions
+7 -3
View File
@@ -66,9 +66,13 @@ func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler {
})
r.Header = hdr
r.Body = &netHTTPBody{body}
// After Go1.5 http.ServeMux uses URL field to get the path for
// request routing purposes.
r.URL = &url.URL{Path: string(ctx.Path())}
rURL, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
ctx.Logger().Printf("cannot parse requestURI %q: %s", r.RequestURI, err)
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return
}
r.URL = rURL
var w netHTTPResponseWriter
h.ServeHTTP(&w, &r)
+9
View File
@@ -5,6 +5,8 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"reflect"
"testing"
"github.com/valyala/fasthttp"
@@ -25,6 +27,10 @@ func TestNewFastHTTPHandler(t *testing.T) {
"Abc": "defg",
"XXX-Remote-Addr": "123.43.4543.345",
}
expectedURL, err := url.ParseRequestURI(expectedRequestURI)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
callsCount := 0
nethttpH := func(w http.ResponseWriter, r *http.Request) {
@@ -61,6 +67,9 @@ func TestNewFastHTTPHandler(t *testing.T) {
if string(body) != expectedBody {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}
if !reflect.DeepEqual(r.URL, expectedURL) {
t.Fatalf("unexpected URL: %#v. Expecting %#v", r.URL, expectedURL)
}
for k, expectedV := range expectedHeader {
v := r.Header.Get(k)