Fix error getting remote address in getRequestRemote

This commit is contained in:
Héctor Hurtado
2020-09-10 12:07:25 +02:00
parent 5c1a00e340
commit 99871de601
2 changed files with 6 additions and 9 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ func getRequestPath(w http.ResponseWriter, r *http.Request, h *model.Handler) {
func getRequestRemote(w http.ResponseWriter, r *http.Request, h *model.Handler) { func getRequestRemote(w http.ResponseWriter, r *http.Request, h *model.Handler) {
w.Header().Add("Content-Type", "application/octet-stream") w.Header().Add("Content-Type", "application/octet-stream")
_, _ = w.Write([]byte(r.RemoteAddr)) _, _ = w.Write([]byte(h.Request.RemoteAddr))
} }
func getRequestMatches(w http.ResponseWriter, r *http.Request, h *model.Handler) { func getRequestMatches(w http.ResponseWriter, r *http.Request, h *model.Handler) {
+5 -8
View File
@@ -26,7 +26,6 @@ import (
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"reflect" "reflect"
"regexp"
"strings" "strings"
"testing" "testing"
@@ -388,22 +387,18 @@ func TestGetRequestRemoteReturnsTheCorrectRemote(t *testing.T) {
Request: httptest.NewRequest("POST", "http://www.foo.bar:8080/", nil), Request: httptest.NewRequest("POST", "http://www.foo.bar:8080/", nil),
Writer: httptest.NewRecorder(), Writer: httptest.NewRecorder(),
} }
h.Request.RemoteAddr = "1.2.3.4:12345"
r := httptest.NewRequest("GET", "/not-important-here", nil) r := httptest.NewRequest("GET", "/not-important-here", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
getRequestRemote(w, r, &h) getRequestRemote(w, r, &h)
res := w.Result() res := w.Result()
body, _ := ioutil.ReadAll(res.Body) if body, _ := ioutil.ReadAll(res.Body); string(body) != h.Request.RemoteAddr {
rem := body[:bytes.Index(body, []byte(":"))] t.Errorf("Version mismatch. Expected %q, got %q", h.Request.RemoteAddr, string(body))
re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
if found := re.Match(rem); !found {
t.Errorf("Version mismatch. Expected %v, got %v", `^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`, string(rem))
} }
} }
// DOING #113: /request/ssl/client/i/dn
func createMuxRequest(pattern, url, method string, content io.Reader) (req *http.Request) { func createMuxRequest(pattern, url, method string, content io.Reader) (req *http.Request) {
m := mux.NewRouter() m := mux.NewRouter()
m.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { req = r }) m.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { req = r })
@@ -1139,6 +1134,8 @@ func TestGetRequestFileContent500sWhenHandlerRequestErrors(t *testing.T) {
} }
} }
// DOING #113: /request/ssl/client/i/dn
func TestGetRouteId200sOnHappyPath(t *testing.T) { func TestGetRouteId200sOnHappyPath(t *testing.T) {
h := model.Handler{ h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil), Request: httptest.NewRequest("POST", "/", nil),