Add request/version resource

This commit is contained in:
Héctor Hurtado
2020-09-08 15:27:51 +02:00
parent 092dc741a3
commit 9988d0e0d4
3 changed files with 71 additions and 4 deletions
+6
View File
@@ -12,6 +12,11 @@ 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 #113: /request/ssl/client/i/dn
Overview Overview
-------- --------
@@ -23,6 +28,7 @@ Overview
├─ request ├─ request
│ ├──── method HTTP Method used (GET, POST) │ ├──── method HTTP Method used (GET, POST)
│ ├──── host Host part of the URL │ ├──── host Host part of the URL
│ ├──── version HTTP version of the request
│ ├──── path Complete URL path (URL-unquoted) │ ├──── path Complete URL path (URL-unquoted)
│ ├──── matches │ ├──── matches
│ │ └──── <name> Previously matched URL path parts │ │ └──── <name> Previously matched URL path parts
+5
View File
@@ -57,6 +57,11 @@ func getRequestHost(w http.ResponseWriter, r *http.Request, h *model.Handler) {
_, _ = w.Write([]byte(h.Request.Host)) _, _ = w.Write([]byte(h.Request.Host))
} }
func getRequestVersion(w http.ResponseWriter, r *http.Request, h *model.Handler) {
w.Header().Add("Content-Type", "application/octet-stream")
_, _ = w.Write([]byte(r.Proto))
}
func getRequestPath(w http.ResponseWriter, r *http.Request, h *model.Handler) { func getRequestPath(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")
// TODO: Discuss a how to obtain URL.EscapedPath() instead // TODO: Discuss a how to obtain URL.EscapedPath() instead
+60 -4
View File
@@ -206,6 +206,22 @@ func TestGetRequestHost200sOnHappyPath(t *testing.T) {
} }
} }
func TestGetRequestHostSetsOctectStreamContentType(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()
getRequestHost(w, r, &h)
res := w.Result()
if res.Header.Get("Content-Type") != "application/octet-stream" {
t.Error("Content Type mismatch")
}
}
func TestGetRequestHostReturnsTheCorrectHostname(t *testing.T) { func TestGetRequestHostReturnsTheCorrectHostname(t *testing.T) {
h := model.Handler{ h := model.Handler{
Request: httptest.NewRequest("POST", "http://www.foo.bar:8080/", nil), Request: httptest.NewRequest("POST", "http://www.foo.bar:8080/", nil),
@@ -222,7 +238,7 @@ func TestGetRequestHostReturnsTheCorrectHostname(t *testing.T) {
} }
} }
func TestGetRequestHostSetsOctectStreamContentType(t *testing.T) { func TestGetRequestVersion200sOnHappyPath(t *testing.T) {
h := model.Handler{ h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil), Request: httptest.NewRequest("POST", "/", nil),
Writer: httptest.NewRecorder(), Writer: httptest.NewRecorder(),
@@ -230,14 +246,48 @@ func TestGetRequestHostSetsOctectStreamContentType(t *testing.T) {
r := httptest.NewRequest("GET", "/not-important-here", nil) r := httptest.NewRequest("GET", "/not-important-here", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
getRequestHost(w, r, &h) getRequestVersion(w, r, &h)
res := w.Result() res := w.Result()
if res.Header.Get("Content-Type") != "application/octet-stream" { if res.StatusCode != http.StatusOK {
t.Error("Content Type mismatch") t.Errorf("Status code mismatch. Expected: %d, got: %d", http.StatusOK, res.StatusCode)
} }
} }
func TestGetRequestVersionSetsOctectStreamContentType(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()
getRequestVersion(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 TestGetRequestVersionReturnsTheCorrectHttpVersion(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()
getRequestVersion(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "HTTP/1.1" {
t.Errorf("Version mismatch. Expected %v, got %v", "HTTP/1.1", string(body))
}
}
// 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),
@@ -302,6 +352,8 @@ func TestGetRequestPathDoesntReturnQueryStringParams(t *testing.T) {
} }
} }
// 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 })
@@ -589,6 +641,8 @@ func TestGetRequestHeadersReturnsTheFirstCorrectMatchValue(t *testing.T) {
} }
} }
// DOING #78: /request/headers/Host /request/headers/host
func TestGetRequestCookies200sOnHappyPath(t *testing.T) { func TestGetRequestCookies200sOnHappyPath(t *testing.T) {
h := model.Handler{ h := model.Handler{
Request: httptest.NewRequest("GET", "/", nil), Request: httptest.NewRequest("GET", "/", nil),
@@ -1005,6 +1059,8 @@ func TestGetRequestFileContent500sWhenHandlerRequestErrors(t *testing.T) {
} }
} }
// DOING #10: /route/id
func TestSetResponseStatus200sOnHappyPath(t *testing.T) { func TestSetResponseStatus200sOnHappyPath(t *testing.T) {
h := model.Handler{ h := model.Handler{
Request: httptest.NewRequest("POST", "/", nil), Request: httptest.NewRequest("POST", "/", nil),