Ejemplo n.º 1
0
func TestNotValidFlashOption(t *testing.T) {

	handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		c := new(controller)
		c.Request = r
		c.Response = rw
		c.Title = "Welcome"
		assert.Panics(t, func() {
			flash.Set(c.Request, "T", "This option is not allowed.")
		}, "Should panic because wrong option.")
		c.serve()
	})

	ts := createHttpServerTest(createMiddlewareHandler(session.New(), handler))
	defer ts.Close()
}
Ejemplo n.º 2
0
func TestFailAuthorization(t *testing.T) {

	n := negroni.New(httphead.New(), session.New(), authorize())
	n.UseHandler(testReader())
	tr := httptest.NewServer(n)
	defer tr.Close()

	redirectPolicyFunc := func(req *http.Request, via []*http.Request) error {
		return nil
	}

	rq, err := http.NewRequest("GET", tr.URL, nil)
	rq.Header.Add("Accept-Language", "en-US")
	c := &http.Client{CheckRedirect: redirectPolicyFunc}
	_, err = c.Do(rq)
	//fmt.Println(rs.Location())
	assert.NoError(t, err, "Should not contain any error")

}
Ejemplo n.º 3
0
func TestRequest(t *testing.T) {

	handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		c := new(controller)
		c.Request = r
		c.Response = rw
		c.Title = "Welcome"
		c.serve()
	})

	ts := createHttpServerTest(createMiddlewareHandler(session.New(), handler))
	defer ts.Close()

	client := &http.Client{}
	response, err := client.Get(ts.URL)
	assert.NoError(t, err, "Should not contain any error.")

	buffer := new(bytes.Buffer)
	io.Copy(buffer, response.Body)
	assert.Contains(t, buffer.String(), "Sign In", "Should contain sign in link.")

}
Ejemplo n.º 4
0
func TestAuthorizationReader(t *testing.T) {

	tw := httptest.NewServer(testWriter())
	n := negroni.New(httphead.New(), session.New(), authorize())
	n.UseHandler(testReader())
	tr := httptest.NewServer(n)
	defer tw.Close()
	defer tr.Close()

	c := &http.Client{}
	rs, err := c.Get(tw.URL)
	assert.NoError(t, err, "Should not contain any error")

	rq, err := http.NewRequest("GET", tr.URL, nil)
	assert.NoError(t, err, "Should not contain any error")
	rq.AddCookie(rs.Cookies()[0])
	rs.Header.Add("Accept-Language", "en-US")
	rs, err = c.Do(rq)

	buffer := new(bytes.Buffer)
	io.Copy(buffer, rs.Body)
	assert.Contains(t, buffer.String(), "You are signed", "User should be signed.")

}
Ejemplo n.º 5
0
// Return all middlwares
func New() []negroni.Handler {
	return []negroni.Handler{session.New(), httphead.New(), security.New(), auth.New()}
}