Add route/id resource. Closes #10

This commit is contained in:
Héctor Hurtado
2020-09-09 13:27:30 +02:00
parent 774b9202b5
commit 6c3bee9ae0
4 changed files with 87 additions and 8 deletions
+6
View File
@@ -156,6 +156,12 @@ func getRequestFileContent(w http.ResponseWriter, r *http.Request, h *model.Hand
}
}
func getRequestId(w http.ResponseWriter, r *http.Request, h *model.Handler) {
w.Header().Add("Content-Type", "application/octet-stream")
_, _ = w.Write([]byte(h.Route.ID))
}
// FIXME: Allow any HTTP status code. Now we are limited by WriteHeader
// capabilities
func setResponseStatus(w http.ResponseWriter, r *http.Request, h *model.Handler) {
+53 -6
View File
@@ -266,8 +266,8 @@ func TestGetRequestVersionSetsOctectStreamContentType(t *testing.T) {
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)
if v := res.Header.Get("Content-Type"); v != "application/octet-stream" {
t.Errorf("Content Type mismatch. Expected: %q, got: %q", "application/octet-stream", v)
}
}
@@ -378,8 +378,8 @@ func TestGetRequestRemoteSetsOctectStreamContentType(t *testing.T) {
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)
if v := res.Header.Get("Content-Type"); v != "application/octet-stream" {
t.Errorf("Content Type mismatch. Expected: %q, got: %q", "application/octet-stream", v)
}
}
@@ -586,7 +586,7 @@ func TestGetRequestHeadersSetsOctectStreamContentType(t *testing.T) {
res := w.Result()
if v := res.Header.Get("Content-Type"); v != "application/octet-stream" {
t.Errorf("Content Type mismatch. Expected: application/octet-stream. Got: %q", v)
t.Errorf("Content Type mismatch. Expected: %q. Got: %q", "application/octet-stream", v)
}
}
@@ -1109,7 +1109,54 @@ func TestGetRequestFileContent500sWhenHandlerRequestErrors(t *testing.T) {
}
}
// DOING #10: /route/id
func TestGetRouteId200sOnHappyPath(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()
getRequestId(w, r, &h)
res := w.Result()
if res.StatusCode != http.StatusOK {
t.Error("Status code mismatch")
}
}
func TestGetRouteIdSetsOctectStreamContentType(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()
getRequestId(w, r, &h)
res := w.Result()
if v := res.Header.Get("Content-Type"); v != "application/octet-stream" {
t.Errorf("Content Type mismatch. Expected: %q, got: %q", "application/octet-stream", v)
}
}
func TestGetRouteIdReturnsTheCorrectMethod(t *testing.T) {
h := model.Handler{
Request: httptest.NewRequest("FOO", "/", nil),
Writer: httptest.NewRecorder(),
Route: model.Route{ID: "Expected_ID"},
}
r := httptest.NewRequest("GET", "/not-important-here", nil)
w := httptest.NewRecorder()
getRequestId(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "Expected_ID" {
t.Errorf("Body mismatch. Expected: %q, got: %q", "Expected_ID", string(body))
}
}
func TestSetResponseStatus200sOnHappyPath(t *testing.T) {
h := model.Handler{
+3
View File
@@ -71,6 +71,9 @@ func Run(bindAddr string, wg *sync.WaitGroup) {
{"/handlers/{handlerID}/request/files/{name}/content", "GET", getRequestFileContent},
{"/handlers/{handlerID}/request/body", "GET", getRequestBody},
// route
//{"/handlers/{handlerID}/route/id", "GET", getRouteId},
// response
{"/handlers/{handlerID}/response/status", "PUT", lockResponseWriter(setResponseStatus)},
{"/handlers/{handlerID}/response/headers/{name}", "PUT", lockResponseWriter(setResponseHeaders)},