Skip to content

Commit

Permalink
Add request/remote resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Héctor Hurtado committed Sep 9, 2020
1 parent 9988d0e commit fc19b92
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
3 changes: 1 addition & 2 deletions docs/source/concepts/resource_tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ to compose the response.
We access the resource tree easily with the ``kapow set`` and ``kapow get``
subcommands.

.. // DOING #85: /request/remote
.. // DOING #10: /route/id
.. // DOING #113: /request/ssl/client/i/dn
Expand All @@ -30,6 +28,7 @@ Overview
│ ├──── host Host part of the URL
│ ├──── version HTTP version of the request
│ ├──── path Complete URL path (URL-unquoted)
│ ├──── remote IP address of client
│ ├──── matches
│ │ └──── <name> Previously matched URL path parts
│ ├──── params
Expand Down
5 changes: 5 additions & 0 deletions internal/server/data/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func getRequestPath(w http.ResponseWriter, r *http.Request, h *model.Handler) {
_, _ = w.Write([]byte(h.Request.URL.Path))
}

func getRequestRemote(w http.ResponseWriter, r *http.Request, h *model.Handler) {
w.Header().Add("Content-Type", "application/octet-stream")
_, _ = w.Write([]byte(r.RemoteAddr))
}

func getRequestMatches(w http.ResponseWriter, r *http.Request, h *model.Handler) {
w.Header().Add("Content-Type", "application/octet-stream")
name := mux.Vars(r)["name"]
Expand Down
54 changes: 52 additions & 2 deletions internal/server/data/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http/httptest"
"net/url"
"reflect"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -286,8 +287,6 @@ func TestGetRequestVersionReturnsTheCorrectHttpVersion(t *testing.T) {
}
}

// DOING #85: /request/remote

func TestGetRequestPath200sOnHappyPath(t *testing.T) {
h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil),
Expand Down Expand Up @@ -352,6 +351,57 @@ func TestGetRequestPathDoesntReturnQueryStringParams(t *testing.T) {
}
}

func TestGetRequestRemote200sOnHappyPath(t *testing.T) {
h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil),
Writer: httptest.NewRecorder(),
}
r := httptest.NewRequest("GET", "/not-important-here", nil)
w := httptest.NewRecorder()

getRequestRemote(w, r, &h)

res := w.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("Status code mismatch. Expected: %d, got: %d", http.StatusOK, res.StatusCode)
}
}

func TestGetRequestRemoteSetsOctectStreamContentType(t *testing.T) {
h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil),
Writer: httptest.NewRecorder(),
}
r := httptest.NewRequest("GET", "/not-important-here", nil)
w := httptest.NewRecorder()

getRequestRemote(w, r, &h)

res := w.Result()
if ct := res.Header.Get("Content-Type"); ct != "application/octet-stream" {
t.Errorf("Content Type mismatch. Expected: %v, got: %v", "application/octet-stream", ct)
}
}

func TestGetRequestRemoteReturnsTheCorrectRemote(t *testing.T) {
h := model.Handler{
Request: httptest.NewRequest("POST", "http://www.foo.bar:8080/", nil),
Writer: httptest.NewRecorder(),
}
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))
}
}

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

func createMuxRequest(pattern, url, method string, content io.Reader) (req *http.Request) {
Expand Down

0 comments on commit fc19b92

Please sign in to comment.