github.com/woremacx/kocha@v0.7.1-0.20150731103243-a5889322afc9/testfixtures_test.go (about)

     1  package kocha
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"path/filepath"
     8  	"sort"
     9  	"strings"
    10  )
    11  
    12  func NewTestApp() *Application {
    13  	config := &Config{
    14  		AppPath:       "testdata",
    15  		AppName:       "appname",
    16  		DefaultLayout: "application",
    17  		Template: &Template{
    18  			PathInfo: TemplatePathInfo{
    19  				Name: "appname",
    20  				Paths: []string{
    21  					filepath.Join("testdata", "app", "view"),
    22  				},
    23  			},
    24  		},
    25  		RouteTable: RouteTable{
    26  			{
    27  				Name:       "root",
    28  				Path:       "/",
    29  				Controller: &FixtureRootTestCtrl{},
    30  			},
    31  			{
    32  				Name:       "user",
    33  				Path:       "/user/:id",
    34  				Controller: &FixtureUserTestCtrl{},
    35  			},
    36  			{
    37  				Name:       "date",
    38  				Path:       "/:year/:month/:day/user/:name",
    39  				Controller: &FixtureDateTestCtrl{},
    40  			},
    41  			{
    42  				Name:       "error",
    43  				Path:       "/error",
    44  				Controller: &FixtureErrorTestCtrl{},
    45  			},
    46  			{
    47  				Name:       "json",
    48  				Path:       "/json",
    49  				Controller: &FixtureJsonTestCtrl{},
    50  			},
    51  			{
    52  				Name:       "teapot",
    53  				Path:       "/teapot",
    54  				Controller: &FixtureTeapotTestCtrl{},
    55  			},
    56  			{
    57  				Name:       "panic_in_render",
    58  				Path:       "/panic_in_render",
    59  				Controller: &FixturePanicInRenderTestCtrl{},
    60  			},
    61  			{
    62  				Name:       "static",
    63  				Path:       "/static/*path",
    64  				Controller: &StaticServe{},
    65  			},
    66  			{
    67  				Name:       "post_test",
    68  				Path:       "/post_test",
    69  				Controller: &FixturePostTestCtrl{},
    70  			},
    71  			{
    72  				Name: "error_controller_test",
    73  				Path: "/error_controller_test",
    74  				Controller: &ErrorController{
    75  					StatusCode: http.StatusBadGateway,
    76  				},
    77  			},
    78  		},
    79  		Logger: &LoggerConfig{
    80  			Writer: ioutil.Discard,
    81  		},
    82  		Middlewares:       []Middleware{&DispatchMiddleware{}},
    83  		MaxClientBodySize: DefaultMaxClientBodySize,
    84  	}
    85  	app, err := New(config)
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  	return app
    90  }
    91  
    92  func NewTestSessionCookieStore() *SessionCookieStore {
    93  	return &SessionCookieStore{
    94  		SecretKey:  "abcdefghijklmnopqrstuvwxyzABCDEF",
    95  		SigningKey: "abcdefghijklmn",
    96  	}
    97  }
    98  
    99  type OrderedOutputMap map[string]interface{}
   100  
   101  func (m OrderedOutputMap) String() string {
   102  	keys := make([]string, 0, len(m))
   103  	for key, _ := range m {
   104  		keys = append(keys, key)
   105  	}
   106  	sort.Strings(keys)
   107  	outputs := make([]string, 0, len(keys))
   108  	for _, key := range keys {
   109  		outputs = append(outputs, fmt.Sprintf("%s:%s", key, m[key]))
   110  	}
   111  	return fmt.Sprintf("map[%v]", strings.Join(outputs, " "))
   112  }
   113  
   114  func (m OrderedOutputMap) GoString() string {
   115  	keys := make([]string, 0, len(m))
   116  	for key, _ := range m {
   117  		keys = append(keys, key)
   118  	}
   119  	sort.Strings(keys)
   120  	for i, key := range keys {
   121  		keys[i] = fmt.Sprintf("%#v:%#v", key, m[key])
   122  	}
   123  	return fmt.Sprintf("map[string]interface{}{%v}", strings.Join(keys, ", "))
   124  }
   125  
   126  type FixturePanicInRenderTestCtrl struct {
   127  	*DefaultController
   128  }
   129  
   130  func (ctrl *FixturePanicInRenderTestCtrl) GET(c *Context) error {
   131  	return c.RenderXML(map[interface{}]interface{}{}) // Context is unsupported type in XML.
   132  }
   133  
   134  type FixtureUserTestCtrl struct {
   135  	*DefaultController
   136  }
   137  
   138  func (ctrl *FixtureUserTestCtrl) GET(c *Context) error {
   139  	return c.Render(map[interface{}]interface{}{
   140  		"id": c.Params.Get("id"),
   141  	})
   142  }
   143  
   144  type FixtureDateTestCtrl struct {
   145  	DefaultController
   146  }
   147  
   148  func (ctrl *FixtureDateTestCtrl) GET(c *Context) error {
   149  	return c.Render(map[interface{}]interface{}{
   150  		"year":  c.Params.Get("year"),
   151  		"month": c.Params.Get("month"),
   152  		"day":   c.Params.Get("day"),
   153  		"name":  c.Params.Get("name"),
   154  	})
   155  }
   156  
   157  type FixtureErrorTestCtrl struct {
   158  	DefaultController
   159  }
   160  
   161  func (ctrl *FixtureErrorTestCtrl) GET(c *Context) error {
   162  	panic("panic test")
   163  }
   164  
   165  type FixtureJsonTestCtrl struct {
   166  	DefaultController
   167  }
   168  
   169  func (ctrl *FixtureJsonTestCtrl) GET(c *Context) error {
   170  	c.Response.ContentType = "application/json"
   171  	return c.Render(nil)
   172  }
   173  
   174  type FixtureRootTestCtrl struct {
   175  	*DefaultController
   176  }
   177  
   178  func (ctrl *FixtureRootTestCtrl) GET(c *Context) error {
   179  	return c.Render(nil)
   180  }
   181  
   182  type FixtureTeapotTestCtrl struct {
   183  	DefaultController
   184  }
   185  
   186  func (ctrl *FixtureTeapotTestCtrl) GET(c *Context) error {
   187  	c.Response.StatusCode = http.StatusTeapot
   188  	return c.Render(nil)
   189  }
   190  
   191  type FixtureInvalidReturnValueTypeTestCtrl struct {
   192  	*DefaultController
   193  }
   194  
   195  func (ctrl *FixtureInvalidReturnValueTypeTestCtrl) GET(c *Context) string {
   196  	return ""
   197  }
   198  
   199  type FixturePostTestCtrl struct {
   200  	*DefaultController
   201  }
   202  
   203  func (ctrl *FixturePostTestCtrl) POST(c *Context) error {
   204  	m := OrderedOutputMap{}
   205  	for k, v := range c.Params.Values {
   206  		m[k] = v
   207  	}
   208  	return c.Render(map[interface{}]interface{}{"params": m})
   209  }
   210  
   211  type FixtureAnotherDelimsTestCtrl struct {
   212  	*DefaultController
   213  	Ctx string
   214  }
   215  
   216  func (ctrl *FixtureAnotherDelimsTestCtrl) GET(c *Context) error {
   217  	return c.Render(ctrl.Ctx)
   218  }