// LogRequest is a logging middleware which logs requests to the server, // what they are requesting, and how long the request takes to execute func LogRequest(c *eden.Context) { start := time.Now() c.Next() duration := time.Since(start) log.Printf("%s: %s | Status: %d | Request took %f miliseconds", c.Request.Method, c.Request.RequestURI, c.Status, duration.Seconds()*100) }
func echo(c *eden.Context) { response := new(echoResponse) response.Phrase = c.Params.ByName("phrase") response.Protocol = c.Request.Proto response.RequestURI = c.Request.RequestURI response.Requester = c.Request.RemoteAddr response.Headers = c.Request.Header response.Trailers = c.Request.Trailer c.Respond(200, response) }
func TestStatus(t *testing.T) { req, _ := http.NewRequest("GET", "/status", nil) res := httptest.NewRecorder() context := eden.Context{} context.Request = req context.Response = res status(&context) if res.Code != http.StatusOK { t.Errorf("Expected 200 status code instead got %d", res.Code) } if !strings.Contains(res.Body.String(), "cap'n") { t.Errorf("Expected to see \"cap'n\" in the body, instead got: %s", res.Body.String()) } }
func TestEcho(t *testing.T) { req, _ := http.NewRequest("GET", "/echo/Hello there", nil) res := httptest.NewRecorder() context := eden.Context{} context.Request = req context.Response = res context.Params = make([]httprouter.Param, 1) context.Params[0] = httprouter.Param{Key: "phrase", Value: "Hello there"} echo(&context) if res.Code != http.StatusOK { t.Errorf("Expected 200 status code instead got %d", res.Code) } if !strings.Contains(res.Body.String(), "Hello there") { t.Errorf("Expected to see 'Hello there' in the body, instead got: %s", res.Body.String()) } }
func status(c *eden.Context) { c.Respond(200, eden.DefaultResponse{Status: "OK", Data: "Everything's looking good cap'n"}) }