This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new dde3297  fix ds request reader.  if empty list, return empty slice 
rather than nil
dde3297 is described below

commit dde32977297a86abb8838a9bbd8bacf70cb181fb
Author: Dan Kirkwood <dang...@apache.org>
AuthorDate: Thu Feb 22 13:47:11 2018 -0700

    fix ds request reader.  if empty list, return empty slice rather than nil
---
 .../traffic_ops_golang/api/shared_handlers_test.go | 42 ++++++++++++++++++++++
 .../deliveryservice/request/requests.go            |  2 +-
 2 files changed, 43 insertions(+), 1 deletion(-)

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 51b3aa6..50ee9ed 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 8cbbafc..ed984e3 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 {

-- 
To stop receiving notification emails like this one, please contact
dewr...@apache.org.

Reply via email to