示例#1
0
func TestSession(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	tg := tango.Classic()
	store, err := New(Options{
		Host:    "127.0.0.1",
		Port:    8888,
		DbIndex: 0,
		MaxAge:  30 * time.Minute,
	})
	if err != nil {
		t.Error(err)
	}

	tg.Use(session.New(session.Options{
		Store: store,
	}))
	tg.Get("/", new(SessionAction))

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "1")
}
示例#2
0
文件: main.go 项目: LYY/gotestlab
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	logger := log.New(os.Stdout, "[tango] ", log.Ldefault())
	t := tango.Classic(logger)
	t.Use(tpongo2.New())
	t.Get("/json", new(JsonAction))
	t.Get("/pongo", new(PongoAction))
	t.Get("/string", new(StringAction))
	t.Run(":8001")
}
示例#3
0
func Test_Cacher(t *testing.T) {
	Convey("Use cache middleware", t, func() {
		t := tango.Classic()
		t.Use(New())
		t.Get("/", new(testCacheController))

		resp := httptest.NewRecorder()
		req, err := http.NewRequest("GET", "/", nil)
		So(err, ShouldBeNil)
		t.ServeHTTP(resp, req)
	})

	Convey("Register invalid adapter", t, func() {
		Convey("Adatper not exists", func() {
			defer func() {
				So(recover(), ShouldNotBeNil)
			}()

			t := tango.Classic()
			t.Use(New(Options{
				Adapter: "fake",
			}))
		})

		Convey("Provider value is nil", func() {
			defer func() {
				So(recover(), ShouldNotBeNil)
			}()

			Register("fake", nil)
		})

		Convey("Register twice", func() {
			defer func() {
				So(recover(), ShouldNotBeNil)
			}()

			Register("memory", &MemoryCacher{})
		})
	})
}
示例#4
0
func testAdapter(opt Options) {
	Convey("Basic operations", func() {
		t := tango.Classic()
		t.Use(New(opt))
		t.Get("/", new(testCacheController))

		resp := httptest.NewRecorder()
		req, err := http.NewRequest("GET", "/", nil)
		So(err, ShouldBeNil)
		t.ServeHTTP(resp, req)
	})

	Convey("Increase and decrease operations", func() {
		t := tango.Classic()
		t.Use(New(opt))

		t.Get("/", new(test2CacheController))

		resp := httptest.NewRecorder()
		req, err := http.NewRequest("GET", "/", nil)
		So(err, ShouldBeNil)
		t.ServeHTTP(resp, req)
	})
}
示例#5
0
func TestJwt(t *testing.T) {
	tg := tango.Classic()
	tg.Use(New(Options{
		KeyFunc: func(ctx *tango.Context) (string, error) {
			return DefaultKey, nil
		},
	}))
	tg.Any("/jwt", new(JwtAction))
	tg.Any("/noJwt", new(NoJwtAction))

	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "http://localhost:8000/jwt", nil)
	if err != nil {
		t.Error(err)
	}

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusUnauthorized)
	expect(t, recorder.Body.String(), http.StatusText(http.StatusUnauthorized))

	recorder = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "http://localhost:8000/noJwt", nil)
	if err != nil {
		t.Error(err)
	}

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	expect(t, recorder.Body.String(), "NoJwtAction")

	recorder = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "http://localhost:8000/jwt", nil)
	if err != nil {
		t.Error(err)
	}

	token, err := NewToken(DefaultKey)
	if err != nil {
		t.Error(err)
	}
	req.Header.Add("Authorization", "Bearer "+token)

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	expect(t, recorder.Body.String(), "JwtAction")
}
示例#6
0
func main() {
	err := conf.Init()
	if err != nil {
		fmt.Println(err)
		return
	}

	err = models.Init()
	if err != nil {
		fmt.Println(err)
		return
	}

	t := tango.Classic()
	t.Use(
		tango.Static(tango.StaticOptions{
			RootPath: conf.BooksRootPath,
			Prefix:   "read",
		}),
		renders.New(renders.Options{
			Reload:    true,
			Directory: "templates",
			//Extensions: []string{".html"},
			Vars: renders.T{
				"AppUrl":  "/",
				"AppLogo": "/",
			},
		}),
	)

	t.Get("/", new(routers.Home))
	t.Any("/github", new(routers.GithubPush))
	//t.Get("/github/auth", new(routers.Github))
	//t.Get("/github/callback", new(routers.GithubCallback))

	t.Run(conf.Listen)
}
示例#7
0
文件: web.go 项目: goftp/ftpd
func Web(listen, static, templates, admin, pass string, tls bool, certFile, keyFile string) {
	_, err := DB.GetUser(admin)
	if err != nil {
		if err == leveldb.ErrNotFound {
			err = DB.AddUser(admin, pass)
		}
	}
	if err != nil {
		panic(err)
	}
	adminUser = admin

	t := tango.Classic()
	sess := session.New(session.Options{
		MaxAge: timeout,
	})
	t.Use(
		tango.Static(tango.StaticOptions{
			RootPath: static,
		}),
		renders.New(renders.Options{
			Reload:    true, // if reload when template is changed
			Directory: templates,
		}),
		sess,
		auth(),
		binding.Bind(),
		xsrf.New(timeout),
		flash.Flashes(sess),
	)

	t.Get("/", new(MainAction))
	t.Any("/login", new(LoginAction))
	t.Get("/logout", new(LogoutAction))
	t.Get("/down", new(DownAction))
	t.Group("/user", func(g *tango.Group) {
		g.Get("/", new(UserAction))
		g.Any("/chgpass", new(ChgPassAction))
		g.Any("/add", new(UserAddAction))
		g.Any("/edit", new(UserEditAction))
		g.Any("/del", new(UserDelAction))
	})

	t.Group("/group", func(g *tango.Group) {
		g.Get("/", new(GroupAction))
		g.Get("/add", new(GroupAddAction))
		g.Get("/edit", new(GroupEditAction))
		g.Get("/del", new(GroupDelAction))
	})
	t.Group("/perm", func(g *tango.Group) {
		g.Get("/", new(PermAction))
		g.Any("/add", new(PermAddAction))
		g.Any("/edit", new(PermEditAction))
		g.Any("/del", new(PermDelAction))
		g.Any("/updateOwner", new(PermUpdateOwner))
		g.Any("/updateGroup", new(PermUpdateGroup))
		g.Any("/updatePerm", new(PermUpdatePerm))
	})

	if tls {
		t.RunTLS(certFile, keyFile, listen)
		return
	}

	t.Run(listen)
}
示例#8
0
func TestSession2(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	tg := tango.Classic()
	store, err := New(Options{
		Host:   "127.0.0.1",
		Port:   8888,
		MaxAge: 10 * time.Second,
	})
	if err != nil {
		t.Error(err)
	}

	tg.Use(session.New(session.Options{
		Store: store,
	}))
	tg.Get("/", new(SessionAction2))

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	expect(t, len(buff.String()), 0)
	expect(t, buff.String(), "")

	time.Sleep(time.Second)

	req, err = http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}
	cks := readSetCookies(recorder.Header())
	for _, ck := range cks {
		req.AddCookie(ck)
	}
	buff.Reset()
	recorder = httptest.NewRecorder()
	recorder.Body = buff

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	expect(t, len(buff.String()), 1)
	expect(t, buff.String(), "b")

	time.Sleep(time.Second * 15)

	req, err = http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}
	cks = readSetCookies(recorder.Header())
	for _, ck := range cks {
		req.AddCookie(ck)
	}
	buff.Reset()
	recorder = httptest.NewRecorder()
	recorder.Body = buff

	tg.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	expect(t, len(buff.String()), 0)
	expect(t, buff.String(), "")
}
示例#9
0
文件: web.go 项目: hefju/RentPro
func init() {
	// Server= gin.Default()
	Server = tango.Classic()
}