github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/generic/ping_test.go (about) 1 package generic 2 3 import ( 4 "fmt" 5 "github.com/stretchr/testify/assert" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 11 "github.com/jfrog/jfrog-cli-core/v2/utils/log" 12 ) 13 14 func TestPingSuccess(t *testing.T) { 15 log.SetDefaultLogger() 16 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 17 w.WriteHeader(http.StatusOK) 18 _, err := fmt.Fprint(w, "OK") 19 assert.NoError(t, err) 20 })) 21 defer ts.Close() 22 responseBytes, err := new(PingCommand).SetServerDetails(&config.ServerDetails{ArtifactoryUrl: ts.URL + "/"}).Ping() 23 if err != nil { 24 t.Logf("Error received from Artifactory following ping request: %s", err) 25 t.Fail() 26 } 27 responseString := string(responseBytes) 28 if responseString != "OK" { 29 t.Logf("Non 'OK' response received from Artifactory following ping request:: %s", responseString) 30 t.Fail() 31 } 32 } 33 34 func TestPingFailed(t *testing.T) { 35 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 36 w.WriteHeader(http.StatusServiceUnavailable) 37 _, err := fmt.Fprint(w, `{"error":"error"}`) 38 assert.NoError(t, err) 39 })) 40 defer ts.Close() 41 _, err := new(PingCommand).SetServerDetails(&config.ServerDetails{ArtifactoryUrl: ts.URL + "/"}).Ping() 42 if err == nil { 43 t.Log("Expected error from artifactory") 44 t.Fail() 45 } 46 }