Add request/remote resource

This commit is contained in:
Héctor Hurtado
2020-09-09 08:53:40 +02:00
parent 9988d0e0d4
commit fc19b921be
3 changed files with 58 additions and 4 deletions
+5
View File
@@ -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"]
+52 -2
View File
@@ -26,6 +26,7 @@ import (
"net/http/httptest"
"net/url"
"reflect"
"regexp"
"strings"
"testing"
@@ -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),
@@ -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) {