github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/router_test.go (about)

     1  package buffalo
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/gobuffalo/buffalo/render"
    12  	"github.com/markbates/willie"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func testApp() *App {
    17  	a := New(Options{})
    18  	rt := a.Group("/router/tests")
    19  
    20  	h := func(c Context) error {
    21  		return c.Render(200, render.String(c.Request().Method+"|"+c.Value("current_path").(string)))
    22  	}
    23  
    24  	rt.GET("/", h)
    25  	rt.POST("/", h)
    26  	rt.PUT("/", h)
    27  	rt.DELETE("/", h)
    28  	rt.OPTIONS("/", h)
    29  	rt.PATCH("/", h)
    30  	return a
    31  }
    32  
    33  func Test_Router(t *testing.T) {
    34  	r := require.New(t)
    35  
    36  	table := []string{
    37  		"GET",
    38  		"POST",
    39  		"PUT",
    40  		"DELETE",
    41  		"OPTIONS",
    42  		"PATCH",
    43  	}
    44  
    45  	ts := httptest.NewServer(testApp())
    46  	defer ts.Close()
    47  
    48  	for _, v := range table {
    49  		req, err := http.NewRequest(v, fmt.Sprintf("%s/router/tests", ts.URL), nil)
    50  		r.NoError(err)
    51  		res, err := http.DefaultClient.Do(req)
    52  		r.NoError(err)
    53  		b, _ := ioutil.ReadAll(res.Body)
    54  		r.Equal(fmt.Sprintf("%s|/router/tests", v), string(b))
    55  	}
    56  }
    57  
    58  func Test_Router_Group(t *testing.T) {
    59  	r := require.New(t)
    60  
    61  	a := testApp()
    62  	g := a.Group("/api/v1")
    63  	g.GET("/users", func(c Context) error {
    64  		return c.Render(201, nil)
    65  	})
    66  
    67  	w := willie.New(a)
    68  	res := w.Request("/api/v1/users").Get()
    69  	r.Equal(201, res.Code)
    70  }
    71  
    72  func Test_Router_Group_on_Group(t *testing.T) {
    73  	r := require.New(t)
    74  
    75  	a := testApp()
    76  	g := a.Group("/api/v1")
    77  	g.GET("/users", func(c Context) error {
    78  		return c.Render(201, nil)
    79  	})
    80  	f := g.Group("/foo")
    81  	f.GET("/bar", func(c Context) error {
    82  		return c.Render(420, nil)
    83  	})
    84  
    85  	w := willie.New(a)
    86  	res := w.Request("/api/v1/foo/bar").Get()
    87  	r.Equal(420, res.Code)
    88  }
    89  
    90  func Test_Router_Group_Middleware(t *testing.T) {
    91  	r := require.New(t)
    92  
    93  	a := testApp()
    94  	a.Use(func(h Handler) Handler { return h })
    95  	r.Len(a.Middleware.stack, 1)
    96  
    97  	g := a.Group("/api/v1")
    98  	r.Len(a.Middleware.stack, 1)
    99  	r.Len(g.Middleware.stack, 1)
   100  
   101  	g.Use(func(h Handler) Handler { return h })
   102  	r.Len(a.Middleware.stack, 1)
   103  	r.Len(g.Middleware.stack, 2)
   104  }
   105  
   106  func Test_Router_ServeFiles(t *testing.T) {
   107  	r := require.New(t)
   108  
   109  	tmpFile, err := ioutil.TempFile("", "assets")
   110  	r.NoError(err)
   111  
   112  	af := []byte("hi")
   113  	_, err = tmpFile.Write(af)
   114  	r.NoError(err)
   115  
   116  	a := New(Options{})
   117  	a.ServeFiles("/assets", http.Dir(filepath.Dir(tmpFile.Name())))
   118  
   119  	w := willie.New(a)
   120  	res := w.Request("/assets/%s", filepath.Base(tmpFile.Name())).Get()
   121  
   122  	r.Equal(200, res.Code)
   123  	r.Equal(af, res.Body.Bytes())
   124  }
   125  
   126  func Test_Resource(t *testing.T) {
   127  	r := require.New(t)
   128  
   129  	type trs struct {
   130  		Method string
   131  		Path   string
   132  		Result string
   133  	}
   134  
   135  	tests := []trs{
   136  		{
   137  			Method: "GET",
   138  			Path:   "/",
   139  			Result: "list",
   140  		},
   141  		{
   142  			Method: "GET",
   143  			Path:   "/new",
   144  			Result: "new",
   145  		},
   146  		{
   147  			Method: "GET",
   148  			Path:   "/1",
   149  			Result: "show 1",
   150  		},
   151  		{
   152  			Method: "GET",
   153  			Path:   "/1/edit",
   154  			Result: "edit 1",
   155  		},
   156  		{
   157  			Method: "POST",
   158  			Path:   "/",
   159  			Result: "create",
   160  		},
   161  		{
   162  			Method: "PUT",
   163  			Path:   "/1",
   164  			Result: "update 1",
   165  		},
   166  		{
   167  			Method: "DELETE",
   168  			Path:   "/1",
   169  			Result: "destroy 1",
   170  		},
   171  	}
   172  
   173  	a := Automatic(Options{})
   174  	a.Resource("/users", &userResource{})
   175  	a.Resource("/api/v1/users", &userResource{})
   176  
   177  	ts := httptest.NewServer(a)
   178  	defer ts.Close()
   179  
   180  	c := http.Client{}
   181  	for _, path := range []string{"/users", "/api/v1/users"} {
   182  		for _, test := range tests {
   183  			u := ts.URL + filepath.Join(path, test.Path)
   184  			req, err := http.NewRequest(test.Method, u, nil)
   185  			r.NoError(err)
   186  			res, err := c.Do(req)
   187  			r.NoError(err)
   188  			b, err := ioutil.ReadAll(res.Body)
   189  			r.NoError(err)
   190  			r.Equal(test.Result, string(b))
   191  		}
   192  	}
   193  
   194  }
   195  
   196  type userResource struct{}
   197  
   198  func (u *userResource) List(c Context) error {
   199  	return c.Render(200, render.String("list"))
   200  }
   201  
   202  func (u *userResource) Show(c Context) error {
   203  	return c.Render(200, render.String("show {{params.user_id}}"))
   204  }
   205  
   206  func (u *userResource) New(c Context) error {
   207  	return c.Render(200, render.String("new"))
   208  }
   209  
   210  func (u *userResource) Create(c Context) error {
   211  	return c.Render(200, render.String("create"))
   212  }
   213  
   214  func (u *userResource) Edit(c Context) error {
   215  	return c.Render(200, render.String("edit {{params.user_id}}"))
   216  }
   217  
   218  func (u *userResource) Update(c Context) error {
   219  	return c.Render(200, render.String("update {{params.user_id}}"))
   220  }
   221  
   222  func (u *userResource) Destroy(c Context) error {
   223  	return c.Render(200, render.String("destroy {{params.user_id}}"))
   224  }