// postProduct400 validates a product can't be created with the endpoint
// unless a valid product document is submitted.
postProduct400 validates a product can't be created with the endpoint
unless a valid product document is submitted.
// postProduct400 validates a product can ' t be created with the endpoint // unless a valid product document is submitted .
func postProduct400(t *testing.T) {
	np := map[string]string{
		"notes": "missing fields",
	}
	body, err := json.Marshal(&np)
	if err != nil {
		t.Fatal(err)
	}
	r := httptest.NewRequest("POST", "/v1/products", bytes.NewBuffer(body))
	w := httptest.NewRecorder()
	r.Header.Set("Authorization", userAuthorization)
	a.ServeHTTP(w, r)
	t.Log("Given the need to validate a new product can't be created with an invalid document.")
	{
		t.Log("\tTest 0:\tWhen using an incomplete product value.")
		{
			if w.Code != http.StatusBadRequest {
				t.Fatalf("\t%s\tShould receive a status code of 400 for the response : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould receive a status code of 400 for the response.", tests.Success)
			var got web.JSONError
			if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the response to an error type : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the response to an error type.", tests.Success)
			want := web.JSONError{
				Error: "field validation failure",
				Fields: web.InvalidError{
					{Fld: "Name", Err: "required"},
					{Fld: "Family", Err: "required"},
					{Fld: "UnitPrice", Err: "required"},
					{Fld: "Quantity", Err: "required"},
				},
			}
			sorter := cmpopts.SortSlices(func(a, b web.Invalid) bool {
				return a.Fld < b.Fld
			})
			if diff := cmp.Diff(want, got, sorter); diff != "" {
				t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
			}
			t.Logf("\t%s\tShould get the expected result.", tests.Success)
		}
	}
}
func postProduct400 ( t * testing . T ) { np := map [ string ] string { " " : " " , } body , err := json . Marshal ( & np ) if err != nil { t . Fatal ( err ) } r := httptest . NewRequest ( " " , " " , bytes . NewBuffer ( body ) ) w := httptest . NewRecorder ( ) r . Header . Set ( " " , userAuthorization ) a . ServeHTTP ( w , r ) t . Log ( " " ) { t . Log ( " \t \t " ) { if w . Code != http . StatusBadRequest { t . Fatalf ( " \t \t " , tests . Failed , w . Code ) } t . Logf ( " \t \t " , tests . Success ) var got web . JSONError if err := json . NewDecoder ( w . Body ) . Decode ( & got ) ; err != nil { t . Fatalf ( " \t \t " , tests . Failed , err ) } t . Logf ( " \t \t " , tests . Success ) want := web . JSONError { Error : " " , Fields : web . InvalidError { { Fld : " " , Err : " " } , { Fld : " " , Err : " " } , { Fld : " " , Err : " " } , { Fld : " " , Err : " " } , } , } sorter := cmpopts . SortSlices ( func ( a , b web . Invalid ) bool { return a . Fld < b . Fld } ) if diff := cmp . Diff ( want , got , sorter ) ; diff != " " { t . Fatalf ( " \t \t \n " , tests . Failed , diff ) } t . Logf ( " \t \t " , tests . Success ) } } }
postProduct400 validates a product can't be created with the endpoint
unless a valid product document is submitted.
postProduct400 validates a product can ' t be created with the endpoint unless a valid product document is submitted .
// Inspect the response. // Define what we want to see. // We can't rely on the order of the field errors so they have to be // sorted. Tell the cmp package how to sort them.