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


Golang TestServer.PostWithHeaders方法代碼示例

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


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

示例1: TestUserCreate


//.........這裏部分代碼省略.........
		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(
				securecookie.GenerateRandomKey(64),
				securecookie.GenerateRandomKey(16),
			)

			b2 := url.Values{
				"name":     {"jesse"},
				"email":    {"[email protected]"},
				"password": {"somepassword"},
				"confirm":  {"somepassword"},
			}

			record.PostWithHeaders("/signup", header, b2).Expect(302)
		})

		It("regression :: should set the password and remember digest", func() {
			record.Post("/signup", body)

			user := &models.User{ID: 1}
			err := models.DB.Read(user)
			if err != nil {
				t.Error(err)
			}

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

		It("should show a welcome flash message", func() {
			ctx := record.Post("/signup", body)
			h := ctx.ResponseRecorder.Header()
			h["Cookie"] = h["Set-Cookie"]
			h.Del("Set-Cookie")
			record.GetWithHeaders("/user/1", h).Expect("welcome jason!")
		})
	})

	Describe("when redirected after successful signup", func() {
		ctx := record.Post("/signup", url.Values{
			"name":     {"jason"},
			"email":    {"[email protected]"},
			"password": {"somepassword"},
			"confirm":  {"somepassword"},
		})

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

		ctx = record.GetWithHeaders("/user/1", h)

		reader := strings.NewReader(ctx.ResponseRecorder.Body.String())
		doc, err := goquery.NewDocumentFromReader(reader)
		if err != nil {
			t.Error(err)
		}

		It("should not show signup and login links", func() {
			doc.Find(".nav-item").Each(func(_ int, sel *goquery.Selection) {
				text := sel.Text()
				if strings.Contains(text, "Signup") || strings.Contains(text, "Login") {
					t.Errorf("whoops, you're logged in but signup and login links are present")
				}
			})
		})
	})
}
開發者ID:nucleardump,項目名稱:go-wires,代碼行數:101,代碼來源:user_test.go


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