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

commit 538456f457445c0bb4f87ad44472ba3fe6fc52ae
Author: Dan Kirkwood <dang...@apache.org>
AuthorDate: Tue Mar 6 16:46:58 2018 -0700

    moves ping to own package; adds unit and api tests
---
 traffic_ops/traffic_ops_golang/{ => ping}/ping.go  |  6 +++---
 .../{ping.go => ping/ping_test.go}                 | 24 +++++++++++++++-------
 traffic_ops/traffic_ops_golang/routes.go           |  3 ++-
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/ping.go 
b/traffic_ops/traffic_ops_golang/ping/ping.go
similarity index 89%
copy from traffic_ops/traffic_ops_golang/ping.go
copy to traffic_ops/traffic_ops_golang/ping/ping.go
index a7377dd..e632aeb 100644
--- a/traffic_ops/traffic_ops_golang/ping.go
+++ b/traffic_ops/traffic_ops_golang/ping/ping.go
@@ -1,4 +1,4 @@
-package main
+package ping
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -24,8 +24,8 @@ import (
        "net/http"
 )
 
-// pingHandler simply returns a canned response to show that the server is 
responding
-func pingHandler() http.HandlerFunc {
+// PingHandler simply returns a canned response to show that the server is 
responding
+func PingHandler() http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
                m := map[string]string{"ping": "pong"}
                enc := json.NewEncoder(w)
diff --git a/traffic_ops/traffic_ops_golang/ping.go 
b/traffic_ops/traffic_ops_golang/ping/ping_test.go
similarity index 70%
rename from traffic_ops/traffic_ops_golang/ping.go
rename to traffic_ops/traffic_ops_golang/ping/ping_test.go
index a7377dd..d1498a1 100644
--- a/traffic_ops/traffic_ops_golang/ping.go
+++ b/traffic_ops/traffic_ops_golang/ping/ping_test.go
@@ -1,4 +1,4 @@
-package main
+package ping
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -20,15 +20,25 @@ package main
  */
 
 import (
-       "encoding/json"
        "net/http"
+       "net/http/httptest"
+       "testing"
 )
 
 // pingHandler simply returns a canned response to show that the server is 
responding
-func pingHandler() http.HandlerFunc {
-       return func(w http.ResponseWriter, r *http.Request) {
-               m := map[string]string{"ping": "pong"}
-               enc := json.NewEncoder(w)
-               enc.Encode(m)
+func TestPingHandler(t *testing.T) {
+       w := httptest.NewRecorder()
+       r, err := http.NewRequest("GET", "ping", nil)
+       if err != nil {
+               t.Error("Error creating new request")
+       }
+
+       PingHandler()(w, r)
+
+       // note the newline...
+       expected := `{"ping":"pong"}
+`
+       if w.Body.String() != expected {
+               t.Error("Expected body", expected, "got", w.Body.String())
        }
 }
diff --git a/traffic_ops/traffic_ops_golang/routes.go 
b/traffic_ops/traffic_ops_golang/routes.go
index ba9a34b..84c7dc0 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -36,6 +36,7 @@ import (
        dsrequest 
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice/request"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/division"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/physlocation"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/ping"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/region"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/status"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/systeminfo"
@@ -142,7 +143,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
                {1.3, http.MethodGet, `parameters/?(\.json)?$`, 
parametersHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 
                //Ping
-               {1.2, http.MethodGet, `ping$`, pingHandler(), 
auth.PrivLevelReadOnly, Authenticated, nil},
+               {1.2, http.MethodGet, `ping$`, ping.PingHandler(), 
auth.PrivLevelReadOnly, Authenticated, nil},
 
                //Servers
                {1.2, http.MethodGet, `servers/?(\.json)?$`, 
serversHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},

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

Reply via email to