github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/ping_test.go (about)

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