Example #1
0
func TestMethodDispatch(t *testing.T) {
	a = 0
	b = 0
	called = 0
	hookZ := map[string]interface{}{
		"hooksA": []interface{}{"modA"},
		"hooksB": []interface{}{[]interface{}{"modB", "MethodB"}},
	}
	hs := hooks.New(hookZ, newModule)
	if a != 0 {
		t.Fatal(a)
	}
	hs.Select("hooksA").Fire("testA")
	if a != 1 {
		t.Fatal(a)
	}
	hs.Select("hooksA").Fire("asdadsad")
	if a != 1 {
		t.Fatal(a)
	}
	hs.Select("hooksC").Fire()
	if a != 1 {
		t.Fatal(a)
	}
	if b != 0 {
		t.Fatal(b)
	}
	hs.Select("hooksB").Fire("testB")
	if b != 1 {
		t.Fatal(b)
	}
}
Example #2
0
func NewWS(conn *gt.Connection, session *mgo.Session, dbConn *mgo.Database, w http.ResponseWriter, req *http.Request, config *config.Config, ws io.Writer) (iface.Context, error) {
	opts, _, err := queryOptions(dbConn, false)
	if err != nil {
		return nil, err
	}
	cli := client.New(w, req, req.Header, config.Secret)
	hookz, has := opts["Hooks"].(map[string]interface{})
	if !has {
		hookz = map[string]interface{}{}
	}
	hoo := hooks.New(hookz, mod.NewModule)
	datab := db.New(session, dbConn, opts, hoo)
	usr := user.New(datab, cli)
	err = req.ParseMultipartForm(1000000)
	if err != nil {
		return nil, err
	}
	var tempFiles map[string][]*multipart.FileHeader
	if req.MultipartForm != nil {
		tempFiles = req.MultipartForm.File
	}
	temp := temporaries.New(tempFiles)
	fs := filesys.New(config.AbsPath, scut.CanonicalHost(req.Host, opts), opts, temp)
	mods := modifiers(req.Form)
	np := nonportable.New(req.URL.Path, convert.Mapify(req.Form), req, w)
	vctx := viewcontext.New()
	dsp := ws_display.New(ws, config.AbsPath)
	ch := channels.New()
	o := options.New(opts, mods)
	ev := events.New(conn)
	cond := conducting.New(hoo, ev)
	ctx := context.New(cond, fs, usr, cli, datab, ch, vctx, np, dsp, o)
	hoo.Initer(moduleIniter(ctx))
	return ctx, err
}
Example #3
0
func New(conn *gt.Connection, session *mgo.Session, dbConn *mgo.Database, w http.ResponseWriter, req *http.Request, config *config.Config) (iface.Context, error) {
	opts, _, err := queryOptions(dbConn, false)
	if err != nil {
		return nil, err
	}
	paths := strings.Split(req.URL.Path, "/")
	if strings.Index(paths[len(paths)-1], ".") != -1 {
		serveFile(opts, w, req, config.AbsPath, scut.CanonicalHost(req.Host, opts), req.URL.Path)
		return nil, nil
	}
	cli := client.New(w, req, req.Header, config.Secret)
	hookz, has := opts["Hooks"].(map[string]interface{})
	if !has {
		hookz = map[string]interface{}{}
	}
	hoo := hooks.New(hookz, mod.NewModule)
	datab := db.New(dbConn, opts, hoo)
	usrFilter, err := filter.NewSimple(set.New(dbConn, "users"), nil)
	if err != nil {
		return nil, err
	}
	usr := user.New(datab, usrFilter, hoo, cli)
	err = req.ParseMultipartForm(1000000)
	if err != nil {
		return nil, err
	}
	var tempFiles map[string][]*multipart.FileHeader
	if req.MultipartForm != nil {
		tempFiles = req.MultipartForm.File
	}
	temp := temporaries.New(tempFiles)
	fs := filesys.New(config.AbsPath, scut.CanonicalHost(req.Host, opts), opts, temp)
	mods := modifiers(req.Form)
	np := nonportable.New(req.URL.Path, convert.Mapify(req.Form), req, w)
	vctx := viewcontext.New()
	dsp := http_display.New(w, config.AbsPath)
	ch := channels.New()
	o := options.New(opts, mods)
	ev := events.New(conn)
	cond := conducting.New(hoo, ev)
	ctx := context.New(cond, fs, usr, cli, datab, ch, vctx, np, dsp, o)
	initer := func(inst iface.Instance) error {
		if inst.HasMethod("Init") {
			inst.Method("Init").Call(nil, ctx)
		}
		return nil
	}
	hoo.Initer(initer)
	return ctx, err
}
Example #4
0
func TestStatePreserving(t *testing.T) {
	a = 0
	b = 0
	called = 0
	hookZ := map[string]interface{}{
		"hooksA": []interface{}{"modA"},
	}
	hs := hooks.New(hookZ, newModule)
	if called != 0 {
		t.Fatal(called)
	}
	for i := 0; i < 10; i++ {
		hs.Select("hooksA").Fire("dummy data")
		if called != i+1 {
			t.Fatal(called)
		}
	}
}