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() }
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") }
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.") }
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.") }
// Return all middlwares func New() []negroni.Handler { return []negroni.Handler{session.New(), httphead.New(), security.New(), auth.New()} }