Ejemplo n.º 1
0
func BenchmarkNew(b *testing.B) {
	app := kocha.NewTestApp()
	config := app.Config
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if _, err := kocha.New(config); err != nil {
			b.Fatal(err)
		}
	}
}
Ejemplo n.º 2
0
func TestNew_buildLogger(t *testing.T) {
	func() {
		config := newConfig()
		config.Logger = nil
		app, err := kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		actual := app.Config.Logger
		expected := &kocha.LoggerConfig{
			Writer:    os.Stdout,
			Formatter: &log.LTSVFormatter{},
		}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf(`New(...).Config.Logger => %#v; want %#v`, actual, expected)
		}
	}()

	func() {
		var buf bytes.Buffer
		formatter := &testLogFormatter{}
		level := log.PANIC
		config := newConfig()
		config.Logger.Writer = &buf
		config.Logger.Formatter = formatter
		config.Logger.Level = level
		app, err := kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		actual := app.Config.Logger
		expected := &kocha.LoggerConfig{
			Writer:    &buf,
			Formatter: formatter,
			Level:     level,
		}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf(`New(...).Config.Logger => %#v; want %#v`, actual, expected)
		}
	}()
}
Ejemplo n.º 3
0
func TestTemplateDelims(t *testing.T) {
	app, err := kocha.New(&kocha.Config{
		AppPath:       "testdata",
		AppName:       "appname",
		DefaultLayout: "",
		Template: &kocha.Template{
			PathInfo: kocha.TemplatePathInfo{
				Name: "appname",
				Paths: []string{
					filepath.Join("testdata", "app", "view"),
				},
			},
			LeftDelim:  "{%",
			RightDelim: "%}",
		},
		RouteTable: []*kocha.Route{
			{
				Name: "another_delims",
				Path: "/",
				Controller: &kocha.FixtureAnotherDelimsTestCtrl{
					Ctx: "test_other_delims_ctx",
				},
			},
		},
		Middlewares: []kocha.Middleware{
			&kocha.DispatchMiddleware{},
		},
		Logger: &kocha.LoggerConfig{
			Writer: ioutil.Discard,
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}
	w := httptest.NewRecorder()
	app.ServeHTTP(w, req)
	var actual interface{} = w.Code
	var expect interface{} = 200
	if !reflect.DeepEqual(actual, expect) {
		t.Errorf(`GET / status => %#v; want %#v`, actual, expect)
	}
	actual = w.Body.String()
	expect = "This is other delims: test_other_delims_ctx\n"
	if !reflect.DeepEqual(actual, expect) {
		t.Errorf(`GET / => %#v; want %#v`, actual, expect)
	}
}
Ejemplo n.º 4
0
func newTestContext(name, layout string) *kocha.Context {
	app, err := kocha.New(&kocha.Config{
		AppPath:       "testdata",
		AppName:       "appname",
		DefaultLayout: "",
		Template: &kocha.Template{
			PathInfo: kocha.TemplatePathInfo{
				Name: "appname",
				Paths: []string{
					filepath.Join("testdata", "app", "view"),
				},
			},
		},
		RouteTable: []*kocha.Route{
			{
				Name:       name,
				Path:       "/",
				Controller: &kocha.FixtureRootTestCtrl{},
			},
		},
		Logger: &kocha.LoggerConfig{
			Writer: ioutil.Discard,
		},
	})
	if err != nil {
		panic(err)
	}
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		panic(err)
	}
	return &kocha.Context{
		Name:     name,
		Layout:   layout,
		Request:  &kocha.Request{Request: req},
		Response: &kocha.Response{ResponseWriter: httptest.NewRecorder()},
		Params:   &kocha.Params{},
		App:      app,
	}
}
Ejemplo n.º 5
0
func TestNew(t *testing.T) {
	func() {
		config := newConfig()
		app, err := kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		actual := app.Config
		expected := config
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Expect %v, but %v", expected, actual)
		}
		if config.MaxClientBodySize != kocha.DefaultMaxClientBodySize {
			t.Errorf("Expect %v, but %v", kocha.DefaultMaxClientBodySize, config.MaxClientBodySize)
		}
	}()

	func() {
		config := newConfig()
		config.MaxClientBodySize = -1
		app, err := kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		actual := app.Config
		expected := config
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Expect %v, but %v", expected, actual)
		}
		if config.MaxClientBodySize != kocha.DefaultMaxClientBodySize {
			t.Errorf("Expect %v, but %v", kocha.DefaultMaxClientBodySize, config.MaxClientBodySize)
		}
	}()

	func() {
		config := newConfig()
		config.MaxClientBodySize = 20131108
		app, err := kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		actual := app.Config
		expected := config
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Expect %v, but %v", expected, actual)
		}
		if config.MaxClientBodySize != 20131108 {
			t.Errorf("Expect %v, but %v", 20131108, config.MaxClientBodySize)
		}
	}()

	// test for event.
	func() {
		config := newConfig()
		app, err := kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		var actual interface{} = app.Event.WorkersPerQueue
		if actual == nil {
			t.Errorf(`New(config).Event => %#v; want not nil`, actual)
		}

		config.Event = &kocha.Event{
			WorkersPerQueue: 100,
		}
		app, err = kocha.New(config)
		if err != nil {
			t.Fatal(err)
		}
		actual = app.Event
		var expect interface{} = config.Event
		if !reflect.DeepEqual(actual, expect) {
			t.Errorf(`New(config).Event => %#v; want %#v`, actual, expect)
		}
	}()
}