You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/02/22 21:16:27 UTC

[GitHub] dewrich closed pull request #1912: TO Golang -- return empty slice for empty ds request list

dewrich closed pull request #1912: TO Golang -- return empty slice for empty ds request list
URL: https://github.com/apache/incubator-trafficcontrol/pull/1912
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/traffic_ops/traffic_ops_golang/api/shared_handlers_test.go b/traffic_ops/traffic_ops_golang/api/shared_handlers_test.go
index 51b3aa641e..50ee9edde9 100644
--- a/traffic_ops/traffic_ops_golang/api/shared_handlers_test.go
+++ b/traffic_ops/traffic_ops_golang/api/shared_handlers_test.go
@@ -41,6 +41,8 @@ type tester struct {
 	errorType tc.ApiErrorType //only for testing
 }
 
+type emptyTester tester
+
 //Identifier interface functions
 func (i *tester) GetID() (int, bool) {
 	return i.ID, true
@@ -76,6 +78,11 @@ func (i *tester) Read(db *sqlx.DB, v map[string]string, user auth.CurrentUser) (
 	return []interface{}{tester{ID: 1}}, nil, tc.NoError
 }
 
+//Reader interface functions
+func (i *emptyTester) Read(db *sqlx.DB, v map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) {
+	return []interface{}{}, nil, tc.NoError
+}
+
 //Updater interface functions
 func (i *tester) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
 	return i.error, i.errorType
@@ -131,6 +138,41 @@ func TestCreateHandler(t *testing.T) {
 	}
 }
 
+func TestEmptyReadHandler(t *testing.T) {
+	mockDB, _, err := sqlmock.New()
+	if err != nil {
+		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+	}
+	defer mockDB.Close()
+
+	db := sqlx.NewDb(mockDB, "sqlmock")
+	defer db.Close()
+
+	w := httptest.NewRecorder()
+	r, err := http.NewRequest("", "", nil)
+	if err != nil {
+		t.Error("Error creating new request")
+	}
+
+	ctx := r.Context()
+	ctx = context.WithValue(ctx, auth.CurrentUserKey,
+		auth.CurrentUser{UserName: "username", ID: 1, PrivLevel: auth.PrivLevelAdmin})
+	ctx = context.WithValue(ctx, PathParamsKey, map[string]string{})
+	// Add our context to the request
+	r = r.WithContext(ctx)
+
+	typeRef := emptyTester{}
+	readFunc := ReadHandler(&typeRef, db)
+
+	readFunc(w, r)
+
+	//verifies the body is in the expected format
+	body := `{"response":[]}`
+	if w.Body.String() != body {
+		t.Error("Expected body", body, "got", w.Body.String())
+	}
+}
+
 func TestReadHandler(t *testing.T) {
 	mockDB, _, err := sqlmock.New()
 	if err != nil {
diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/request/requests.go b/traffic_ops/traffic_ops_golang/deliveryservice/request/requests.go
index 8cbbafc8b1..ed984e3a11 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservice/request/requests.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservice/request/requests.go
@@ -112,7 +112,7 @@ func (req *TODeliveryServiceRequest) Read(db *sqlx.DB, parameters map[string]str
 	}
 	defer rows.Close()
 
-	var deliveryServiceRequests []interface{}
+	deliveryServiceRequests := []interface{}{}
 	for rows.Next() {
 		var s TODeliveryServiceRequest
 		if err = rows.StructScan(&s); err != nil {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services