當前位置: 首頁>>代碼示例>>Golang>>正文


Golang TestServer.Post方法代碼示例

本文整理匯總了Golang中github.com/nucleartide/request.TestServer.Post方法的典型用法代碼示例。如果您正苦於以下問題:Golang TestServer.Post方法的具體用法?Golang TestServer.Post怎麽用?Golang TestServer.Post使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/nucleartide/request.TestServer的用法示例。


在下文中一共展示了TestServer.Post方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestSessionCreate

func TestSessionCreate(t *testing.T) {
	record := request.TestServer{t, TestHandler}

	Describe("when submitting a malformed request", func() {
		body := url.Values{
			"not email":    {"asdf"},
			"not password": {"asdf"},
		}

		It("should return 400", func() {
			record.Post("/login", body).Expect(400).Expect(`schema: invalid path`)
		})
	})

	Describe("when the request fails validation", func() {
		body := url.Values{
			"email":    {"not an actual email"},
			"password": {"password"},
		}

		It("should return 422", func() {
			record.Post("/login", body).Expect(422).Expect("invalid email or password")
		})

		It("should fill in form values after failed submission", func() {
			record.Post("/login", body).
				Expect(`value="not an actual email"`).
				Expect(`value="password"`)
		})
	})

	Describe("when signing in", func() {
		var (
			email    = "[email protected]"
			password = "asdfasdf"
		)

		body := url.Values{
			"email":    {email},
			"password": {password},
		}

		BeforeEach(func() {
			body := url.Values{
				"name":     {"jason"},
				"email":    {email},
				"password": {password},
				"confirm":  {password},
			}

			record.Post("/signup", body).Expect(302)
		})

		AfterEach(func() {
			query := "TRUNCATE TABLE users; ALTER SEQUENCE users_id_seq RESTART WITH 1"
			_, err := models.DB.Exec(query)
			if err != nil {
				t.Error(err)
			}
		})

		// http://stackoverflow.com/questions/15721238/go-serial-execution-of-package-tests
		It("should return 302", func() {
			user := &models.User{ID: 1}
			if err := models.DB.Read(user); err != nil {
				t.Error(err)
			}

			record.Post("/login", body).Expect(302)
		})

		It("should return a session cookie", func() {
			c := record.Post("/login", body).ResponseRecorder.Header().Get("Set-Cookie")
			assert.NotEmpty(t, c)
		})

		It("regression :: should fill in the user's remember digest", func() {
			user := &models.User{ID: 1}
			if err := models.DB.Read(user); err != nil {
				t.Error(err)
			}

			user.RememberDigest = ""
			if err := models.DB.Update(user); err != nil {
				t.Error(err)
			}

			record.Post("/login", body)
			if err := models.DB.Read(user); err != nil {
				t.Error(err)
			}

			assert.NotEmpty(t, user.RememberDigest)
		})

		It("should show a flash message", func() {
			h := record.Post("/login", body).ResponseRecorder.Header()
			h["Cookie"] = h["Set-Cookie"]
			h.Del("Set-Cookie")
			record.GetWithHeaders("/user/1", h).Expect("welcome jason!")
//.........這裏部分代碼省略.........
開發者ID:nucleardump,項目名稱:go-wires,代碼行數:101,代碼來源:session_test.go

示例2: TestUserCreate

func TestUserCreate(t *testing.T) {
	record := request.TestServer{t, TestHandler}

	Describe("when the request is malformed", func() {
		body := url.Values{}
		body.Set("name", "jason")
		body.Set("not a user field", "asdf")

		It("should return 400", func() {
			record.Post("/signup", body).Expect(400).Expect(`bad request`)
		})
	})

	Describe("when the request fails validation", func() {
		body := url.Values{}
		body.Set("name", "jesse")
		body.Set("email", "[email protected]")
		body.Set("password", "some random password")
		body.Set("confirm", "fail confirm")

		It("should return 422", func() {
			record.Post("/signup", body).Expect(422).Expect("password does not match")
		})

		It("should fill in form values after failed submission", func() {
			record.Post("/signup", body).
				Expect(`value="jesse"`).
				Expect(`value="[email protected]"`).
				Expect(`value="some random password"`).
				Expect(`value="fail confirm"`)
		})
	})

	Describe("when creating a user that already exists", func() {
		var (
			name  = "jason"
			email = "[email protected]"
		)

		BeforeEach(func() {
			user := &models.User{Name: name, Email: email}
			_, err := models.DB.Create(user)
			if err != nil {
				t.Error(err)
			}
		})

		AfterEach(func() {
			query := "TRUNCATE TABLE users; ALTER SEQUENCE users_id_seq RESTART WITH 1"
			_, err := models.DB.Exec(query)
			if err != nil {
				t.Error(err)
			}
		})

		It("should return a 409 for an existing email", func() {
			body := url.Values{}
			body.Set("name", name)
			body.Set("email", email)
			body.Set("password", "somepassword")
			body.Set("confirm", "somepassword")
			record.Post("/signup", body).Expect(409).Expect("email is taken")
		})
	})

	Describe("when creating a user that doesn't exist", func() {
		body := url.Values{
			"name":     {"jason"},
			"email":    {"[email protected]"},
			"password": {"somepassword"},
			"confirm":  {"somepassword"},
		}

		AfterEach(func() {
			query := "TRUNCATE TABLE users; ALTER SEQUENCE users_id_seq RESTART WITH 1"
			_, err := models.DB.Exec(query)
			if err != nil {
				t.Error(err)
			}
		})

		It("should sign in", func() {
			ctx := record.Post("/signup", body)
			assert.NotEmpty(t, ctx.ResponseRecorder.Header().Get("Set-Cookie"))
		})

		It("should redirect to the newly created profile", func() {
			ctx := record.Post("/signup", body).Expect(302)
			redirect := ctx.ResponseRecorder.Header().Get("Location")
			assert.Equal(t, "/user/1", redirect)
		})

		It("should work regardless of the current session cookie", func() {
			ctx := record.Post("/signup", body)

			header := ctx.ResponseRecorder.Header()
			header["Cookie"] = header["Set-Cookie"]
			header.Del("Set-Cookie")

			models.Store = sessions.NewCookieStore(
//.........這裏部分代碼省略.........
開發者ID:nucleardump,項目名稱:go-wires,代碼行數:101,代碼來源:user_test.go

示例3: TestSessionDestroy

func TestSessionDestroy(t *testing.T) {
	record := request.TestServer{t, TestHandler}

	Describe("when logging out after logging in", func() {
		// Create a user.
		body := url.Values{
			"name":     {"jason"},
			"email":    {"[email protected]"},
			"password": {"password"},
			"confirm":  {"password"},
		}
		record.Post("/signup", body).Expect(302)

		// Defer cleanup.
		defer func() {
			clearTable := "TRUNCATE TABLE users; ALTER SEQUENCE users_id_seq RESTART WITH 1"
			_, err := models.DB.Exec(clearTable)
			if err != nil {
				t.Error(err)
			}
		}()

		// Login.
		b2 := url.Values{
			"email":    {"[email protected]"},
			"password": {"password"},
		}
		ctx := record.Post("/login", b2).Expect(302)
		header := ctx.ResponseRecorder.Header()
		header["Cookie"] = header["Set-Cookie"]
		header.Del("Set-Cookie")
		ctx = record.GetWithHeaders("/logout", header).Expect(302)

		It("should redirect to the home page", func() {
			assert.Equal(t, ctx.ResponseRecorder.Header().Get("Location"), "/")
		})

		It("should show a flash message", func() {
			redirect := ctx.ResponseRecorder.Header().Get("Location")

			header = ctx.ResponseRecorder.Header()
			header["Cookie"] = header["Set-Cookie"]
			header.Del("Set-Cookie")

			record.GetWithHeaders(redirect, header).Expect("you have been logged out")
		})
	})

	Describe("when the session is invalid", func() {
		// Make an invalid cookie.
		w, _ := NewReqRes(t)
		http.SetCookie(w, &http.Cookie{Name: models.SessionName, Value: "asdf"})

		// Do a GET with the invalid cookie.
		header := w.Header()
		header["Cookie"] = header["Set-Cookie"]
		header.Del("Set-Cookie")
		ctx := record.GetWithHeaders("/logout", header)

		It("should redirect to the home page", func() {
			assert.Equal(t, "/", ctx.ResponseRecorder.Header().Get("Location"))
		})
	})

	Describe("when the session is valid but the user ID is absent", func() {
		// Create a valid session cookie.
		w, r := NewReqRes(t)
		session, err := models.Store.Get(r, models.SessionName)
		if err != nil {
			t.Error(err)
		}
		if session.Save(r, w); err != nil {
			t.Error(err)
		}

		// Do a GET with the valid session cookie.
		header := w.Header()
		header["Cookie"] = header["Set-Cookie"]
		header.Del("Set-Cookie")
		ctx := record.GetWithHeaders("/logout", header)

		It("should clear the session cookie", func() {
			assert.NotEmpty(t, ctx.ResponseRecorder.Header().Get("Set-Cookie"))
		})

		It("should redirect to the home page", func() {
			assert.Equal(t, "/", ctx.ResponseRecorder.Header().Get("Location"))
		})
	})

	Describe("when the session is valid and the user ID is present", func() {
		// Create a user.
		body := url.Values{
			"name":     {"jason"},
			"email":    {"[email protected]"},
			"password": {"password"},
			"confirm":  {"password"},
		}
		record.Post("/signup", body).Expect(302)

//.........這裏部分代碼省略.........
開發者ID:nucleardump,項目名稱:go-wires,代碼行數:101,代碼來源:session_test.go


注:本文中的github.com/nucleartide/request.TestServer.Post方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。