当前位置: 首页>>代码示例>>Golang>>正文


Golang app.App类代码示例

本文整理汇总了Golang中github.com/ardanlabs/gotraining/topics/http/api/app.App的典型用法代码示例。如果您正苦于以下问题:Golang App类的具体用法?Golang App怎么用?Golang App使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了App类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: usersUpdate200

// usersUpdate200 validates a user can be updated with the endpoint.
func usersUpdate200(t *testing.T, a *app.App, c *app.Context) {
	u.FirstName = "Lisa"

	body, _ := json.Marshal(&u)
	r := tests.NewRequest("PUT", "/v1/users/"+u.UserID, bytes.NewBuffer(body))
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to validate a user can be updated with the users endpoint.")
	{
		if w.Code != 200 {
			t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 200 for the response.", tests.Succeed)

		var resp models.User
		if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
			t.Fatal("\tShould be able to unmarshal the response.", tests.Failed)
		}
		t.Log("\tShould be able to unmarshal the response.", tests.Succeed)

		if resp.UserID != u.UserID {
			t.Fatal("\tShould have an a user value with the same id.", tests.Failed)
		}
		t.Log("\tShould have an a user value with the same id.", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:28,代码来源:users_test.go

示例2: usersCreate200

// usersCreate200 validates a user can be created with the endpoint.
func usersCreate200(t *testing.T, a *app.App, c *app.Context) {
	body, _ := json.Marshal(&u)
	r := tests.NewRequest("POST", "/v1/users", bytes.NewBuffer(body))
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to add a new user with the users endpoint.")
	{
		if w.Code != 200 {
			t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 200 for the response.", tests.Succeed)

		var resp models.User
		if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
			t.Fatal("\tShould be able to unmarshal the response.", tests.Failed)
		}
		t.Log("\tShould be able to unmarshal the response.", tests.Succeed)

		if resp.UserID == "" {
			t.Fatal("\tShould have a user id in the response.", tests.Failed)
		}
		t.Log("\tShould have a user id in the response.", tests.Succeed)

		// Save for future calls.
		u.UserID = resp.UserID
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:29,代码来源:users_test.go

示例3: usersList404

// usersList404 validates an empty users list can be retrieved with the endpoint.
func usersList404(t *testing.T, a *app.App, c *app.Context) {
	r := tests.NewRequest("GET", "/v1/users", nil)
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to validate an empty list of users with the users endpoint.")
	{
		if w.Code != 404 {
			t.Fatalf("\tShould received a status code of 404 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 404 for the response.", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:14,代码来源:users_test.go

示例4: usersDelete404

// usersDelete404 validates a user that has been deleted is deleted.
func usersDelete404(t *testing.T, a *app.App, c *app.Context, id string) {
	r := tests.NewRequest("DELETE", "/v1/users/"+id, nil)
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to verify a deleted user is deleted.")
	{
		if w.Code != 404 {
			t.Fatalf("\tShould received a status code of 404 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 404 for the response.", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:14,代码来源:users_test.go

示例5: usersRetrieve400

// usersRetrieve400 validates a user request with an invalid id with the endpoint.
func usersRetrieve400(t *testing.T, a *app.App, c *app.Context, id string) {
	r := tests.NewRequest("GET", "/v1/users/"+id, nil)
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the situation of retrieving an individual user with an invalid id with the users endpoint.")
	{
		if w.Code != 400 {
			t.Fatalf("\tShould received a status code of 400 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 400 for the response.", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:14,代码来源:users_test.go

示例6: usersCreate400

// usersCreate400 validates a user can't be created with the endpoint
// unless a valid user document is submitted.
func usersCreate400(t *testing.T, a *app.App, c *app.Context) {
	u := models.User{
		UserType: 1,
		LastName: "Kennedy",
		Email:    "[email protected]",
		Company:  "Ardan Labs",
	}

	body, _ := json.Marshal(&u)
	r := tests.NewRequest("POST", "/v1/users", bytes.NewBuffer(body))
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to validate a new user can't be created with an invalid document.")
	{
		if w.Code != 400 {
			t.Fatalf("\tShould received a status code of 400 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 400 for the response.", tests.Succeed)

		v := struct {
			Error  string `json:"error"`
			Fields []struct {
				Fld string `json:"field_name"`
				Err string `json:"error"`
			} `json:"fields,omitempty"`
		}{}

		if err := json.NewDecoder(w.Body).Decode(&v); err != nil {
			t.Fatal("\tShould be able to unmarshal the response.", tests.Failed)
		}
		t.Log("\tShould be able to unmarshal the response.", tests.Succeed)

		if len(v.Fields) == 0 {
			t.Fatal("\tShould have validation errors in the response.", tests.Failed)
		}
		t.Log("\tShould have validation errors in the response.", tests.Succeed)

		if v.Fields[0].Fld != "FirstName" {
			t.Fatalf("\tShould have a FirstName validation error in the response. Received[%s] %s", v.Fields[0].Fld, tests.Failed)
		}
		t.Log("\tShould have a FirstName validation error in the response.", tests.Succeed)

		if v.Fields[1].Fld != "Addresses" {
			t.Fatalf("\tShould have an Addresses validation error in the response. Received[%s] %s", v.Fields[0].Fld, tests.Failed)
		}
		t.Log("\tShould have an Addresses validation error in the response.", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:51,代码来源:users_test.go

示例7: usersList200

// usersList200 validates a users list can be retrieved with the endpoint.
func usersList200(t *testing.T, a *app.App, c *app.Context) []models.User {
	r := tests.NewRequest("GET", "/v1/users", nil)
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to retrieve a list of users with the users endpoint.")
	{
		if w.Code != 200 {
			t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 200 for the response.", tests.Succeed)

		var us []models.User
		if err := json.NewDecoder(w.Body).Decode(&us); err != nil {
			t.Fatal("\tShould be able to unmarshal the response.", tests.Failed)
		}
		t.Log("\tShould be able to unmarshal the response.", tests.Succeed)

		if len(us) == 0 {
			t.Fatal("\tShould have users in the response.", tests.Failed)
		}
		t.Log("\tShould have a users in the response.", tests.Succeed)

		var failed bool
		marks := make([]string, len(us))
		for i, u := range us {
			if u.DateCreated == nil || u.DateModified == nil {
				marks[i] = tests.Failed
				failed = true
			} else {
				marks[i] = tests.Succeed
			}
		}

		if failed {
			t.Fatalf("\tShould have dates in all the user documents. %+v", marks)
		}
		t.Logf("\tShould have dates in all the user documents. %+v", marks)

		return us
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:43,代码来源:users_test.go

示例8: usersDelete200

// usersDelete200 validates a user can be deleted with the endpoint.
func usersDelete200(t *testing.T, a *app.App, c *app.Context, id string) {
	r := tests.NewRequest("DELETE", "/v1/users/"+id, nil)
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to delete a new user with the users endpoint.")
	{
		if w.Code != 200 {
			t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 200 for the response.", tests.Succeed)

		var resp models.User
		if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
			t.Fatal("\tShould be able to unmarshal the response.", tests.Failed)
		}
		t.Log("\tShould be able to unmarshal the response.", tests.Succeed)

		if resp.UserID != id {
			t.Fatal("\tShould have an a user value with the same id.", tests.Failed)
		}
		t.Log("\tShould have an a user value with the same id.", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:25,代码来源:users_test.go

示例9: usersRetrieve200

// usersList200 validates a users list can be retrieved with the endpoint.
func usersRetrieve200(t *testing.T, a *app.App, c *app.Context, id string) {
	r := tests.NewRequest("GET", "/v1/users/"+id, nil)
	w := httptest.NewRecorder()
	a.ServeHTTP(w, r)

	t.Log("Given the need to retrieve an individual user with the users endpoint.")
	{
		if w.Code != 200 {
			t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, tests.Failed)
		}
		t.Log("\tShould received a status code of 200 for the response.", tests.Succeed)

		var ur models.User
		if err := json.NewDecoder(w.Body).Decode(&ur); err != nil {
			t.Fatal("\tShould be able to unmarshal the response.", tests.Failed)
		}
		t.Log("\tShould be able to unmarshal the response.", tests.Succeed)

		if ur.UserID != id {
			t.Fatal("\tShould have the document specified by id.", tests.Failed)
		}
		t.Log("\tShould have the document specified by id", tests.Succeed)
	}
}
开发者ID:bsreera,项目名称:gotraining,代码行数:25,代码来源:users_test.go


注:本文中的github.com/ardanlabs/gotraining/topics/http/api/app.App类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。