github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/commands/generic/ping_test.go (about)

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