Skip to content

Commit

Permalink
Fix error getting remote address in getRequestRemote
Browse files Browse the repository at this point in the history
  • Loading branch information
Héctor Hurtado committed Sep 10, 2020
1 parent 5c1a00e commit 99871de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/server/data/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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) {
Expand Down
13 changes: 5 additions & 8 deletions internal/server/data/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net/http/httptest"
"net/url"
"reflect"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -388,22 +387,18 @@ func TestGetRequestRemoteReturnsTheCorrectRemote(t *testing.T) {
Request: httptest.NewRequest("POST", "http://www.foo.bar:8080/", nil),
Writer: httptest.NewRecorder(),
}
h.Request.RemoteAddr = "1.2.3.4:12345"
r := httptest.NewRequest("GET", "/not-important-here", nil)
w := httptest.NewRecorder()

getRequestRemote(w, r, &h)

res := w.Result()
body, _ := ioutil.ReadAll(res.Body)
rem := body[:bytes.Index(body, []byte(":"))]
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))
if body, _ := ioutil.ReadAll(res.Body); string(body) != h.Request.RemoteAddr {
t.Errorf("Version mismatch. Expected %q, got %q", h.Request.RemoteAddr, string(body))
}
}

// DOING #113: /request/ssl/client/i/dn

func createMuxRequest(pattern, url, method string, content io.Reader) (req *http.Request) {
m := mux.NewRouter()
m.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { req = r })
Expand Down Expand Up @@ -1139,6 +1134,8 @@ func TestGetRequestFileContent500sWhenHandlerRequestErrors(t *testing.T) {
}
}

// DOING #113: /request/ssl/client/i/dn

func TestGetRouteId200sOnHappyPath(t *testing.T) {
h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil),
Expand Down

0 comments on commit 99871de

Please sign in to comment.