Add request/remote resource
This commit is contained in:
@@ -12,8 +12,6 @@ to compose the response.
|
|||||||
We access the resource tree easily with the ``kapow set`` and ``kapow get``
|
We access the resource tree easily with the ``kapow set`` and ``kapow get``
|
||||||
subcommands.
|
subcommands.
|
||||||
|
|
||||||
.. // DOING #85: /request/remote
|
|
||||||
|
|
||||||
.. // DOING #10: /route/id
|
.. // DOING #10: /route/id
|
||||||
|
|
||||||
.. // DOING #113: /request/ssl/client/i/dn
|
.. // DOING #113: /request/ssl/client/i/dn
|
||||||
@@ -30,6 +28,7 @@ Overview
|
|||||||
│ ├──── host Host part of the URL
|
│ ├──── host Host part of the URL
|
||||||
│ ├──── version HTTP version of the request
|
│ ├──── version HTTP version of the request
|
||||||
│ ├──── path Complete URL path (URL-unquoted)
|
│ ├──── path Complete URL path (URL-unquoted)
|
||||||
|
│ ├──── remote IP address of client
|
||||||
│ ├──── matches
|
│ ├──── matches
|
||||||
│ │ └──── <name> Previously matched URL path parts
|
│ │ └──── <name> Previously matched URL path parts
|
||||||
│ ├──── params
|
│ ├──── params
|
||||||
|
|||||||
@@ -68,6 +68,11 @@ func getRequestPath(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
|||||||
_, _ = w.Write([]byte(h.Request.URL.Path))
|
_, _ = 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) {
|
func getRequestMatches(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")
|
||||||
name := mux.Vars(r)["name"]
|
name := mux.Vars(r)["name"]
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -286,8 +287,6 @@ func TestGetRequestVersionReturnsTheCorrectHttpVersion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DOING #85: /request/remote
|
|
||||||
|
|
||||||
func TestGetRequestPath200sOnHappyPath(t *testing.T) {
|
func TestGetRequestPath200sOnHappyPath(t *testing.T) {
|
||||||
h := model.Handler{
|
h := model.Handler{
|
||||||
Request: httptest.NewRequest("POST", "/", nil),
|
Request: httptest.NewRequest("POST", "/", nil),
|
||||||
@@ -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
|
// 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user