Skip to content

Commit

Permalink
Refactor router to be able to update schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
caalberts committed Aug 30, 2018
1 parent 8bba225 commit 7fdc81a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
36 changes: 26 additions & 10 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type ServerFunc func(port string, schemas []localroast.Schema) Server

// NewServer creates a http server running on given port with handlers based on given schema.
func NewServer(port string, schemas []localroast.Schema) Server {
router := newRouter(schemas)
router := newRouter()
router.UpdateSchema(schemas)

log.Println("brewing on port " + port)
return &http.Server{
Expand All @@ -27,20 +28,35 @@ func NewServer(port string, schemas []localroast.Schema) Server {
}
}

func newRouter(schemas []localroast.Schema) http.Handler {
router := httprouter.New()
type router struct {
http.Handler
}

handlerFunc := func(s localroast.Schema) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(s.Status)
w.Write(s.Response)
}
func newRouter() *router {
rtr := router{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
}),
}
return &rtr
}

func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rtr.Handler.ServeHTTP(w, r)
}

func (rtr *router) UpdateSchema(schemas []localroast.Schema) {
router := httprouter.New()
for _, schema := range schemas {
router.Handle(schema.Method, schema.Path, handlerFunc(schema))
}
rtr.Handler = router
}

return router
func handlerFunc(schema localroast.Schema) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(schema.Status)
w.Write(schema.Response)
}
}
33 changes: 19 additions & 14 deletions http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,46 @@ package http

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/caalberts/localroast"

"github.com/stretchr/testify/assert"
"io/ioutil"
)

func TestNewServer(t *testing.T) {
schema := localroast.Schema{Path: "/"}
port := "8888"
server := NewServer(port, []localroast.Schema{schema}).(*http.Server)
server := NewServer(port, []localroast.Schema{}).(*http.Server)
assert.Equal(t, ":8888", server.Addr)
}

func TestNewRouter(t *testing.T) {
func TestNewRouterHasNoImplementation(t *testing.T) {
router := newRouter()
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, http.StatusNotImplemented, resp.Code)
}

func TestRouterWithUpdatedSchema(t *testing.T) {
router := newRouter()
schemas := []localroast.Schema{
localroast.Schema{
{
Method: "GET",
Path: "/",
Status: 200,
Response: []byte(`{"success": true}`),
},
localroast.Schema{
{
Method: "GET",
Path: "/users",
Status: 200,
Response: []byte(`{"success": true, "ids": [1, 2]}`),
},
localroast.Schema{
{
Method: "POST",
Path: "/users",
Status: 201,
Expand All @@ -47,10 +55,9 @@ func TestNewRouter(t *testing.T) {
IDs []int `json:"ids"`
Message string `json:"message"`
}
router := newRouter(schemas)
router.UpdateSchema(schemas)

var expected, actual testData

for _, schema := range schemas {
t.Run(schema.Method+schema.Path, func(t *testing.T) {
req := httptest.NewRequest(schema.Method, schema.Path, nil)
Expand Down Expand Up @@ -87,16 +94,14 @@ func TestPathParam(t *testing.T) {
Response: []byte(`{"success": true}`),
}

type testData struct {
Success bool `json:"success"`
}
mux := newRouter([]localroast.Schema{schema})
router := newRouter()
router.UpdateSchema([]localroast.Schema{schema})

testPath := "/users/1"

req := httptest.NewRequest(schema.Method, testPath, nil)
resp := httptest.NewRecorder()
mux.ServeHTTP(resp, req)
router.ServeHTTP(resp, req)

assert.Equal(t, schema.Status, resp.Code)
}
4 changes: 2 additions & 2 deletions json/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func (c Command) Execute(port string, args []string) error {
return err
}

schema, err := c.p.Parse(file)
schemas, err := c.p.Parse(file)
if err != nil {
return err
}

server := c.s(port, schema)
server := c.s(port, schemas)

return server.ListenAndServe()
}

0 comments on commit 7fdc81a

Please sign in to comment.