github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/utils/tests/xray/server.go (about)

     1  package xray
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/buger/jsonparser"
    10  	"github.com/cobalt77/jfrog-client-go/utils/log"
    11  	clienttests "github.com/cobalt77/jfrog-client-go/utils/tests"
    12  )
    13  
    14  const (
    15  	CleanScanBuildName  = "cleanBuildName"
    16  	FatalScanBuildName  = "fatalBuildName"
    17  	VulnerableBuildName = "vulnerableBuildName"
    18  )
    19  
    20  func handler(w http.ResponseWriter, r *http.Request) {
    21  	body, err := ioutil.ReadAll(r.Body)
    22  	if err != nil {
    23  		http.Error(w, err.Error(), http.StatusInternalServerError)
    24  		return
    25  	}
    26  
    27  	buildName, err := jsonparser.GetString(body, "buildName")
    28  	if err != nil {
    29  		http.Error(w, err.Error(), http.StatusInternalServerError)
    30  		return
    31  	}
    32  
    33  	switch buildName {
    34  	case CleanScanBuildName:
    35  		fmt.Fprint(w, CleanXrayScanResponse)
    36  		return
    37  	case FatalScanBuildName:
    38  		fmt.Fprint(w, FatalErrorXrayScanResponse)
    39  		return
    40  	case VulnerableBuildName:
    41  		fmt.Fprint(w, VulnerableXrayScanResponse)
    42  		return
    43  	}
    44  	http.Error(w, err.Error(), http.StatusInternalServerError)
    45  }
    46  
    47  func StartXrayMockServer() int {
    48  	handlers := clienttests.HttpServerHandlers{}
    49  	handlers["/api/xray/scanBuild"] = handler
    50  	handlers["/"] = http.NotFound
    51  
    52  	port, err := clienttests.StartHttpServer(handlers)
    53  	if err != nil {
    54  		log.Error(err)
    55  		os.Exit(1)
    56  	}
    57  	return port
    58  }