github.com/goharbor/go-client@v0.210.0/pkg/harbor/test/server.go (about)

     1  package test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  
     9  	"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
    10  )
    11  
    12  const (
    13  	secret = "Basic Yjph"
    14  	result = "yes"
    15  )
    16  
    17  func NewServer() *httptest.Server {
    18  	mux := http.NewServeMux()
    19  
    20  	mux.HandleFunc("/api/v2.0/health", func(w http.ResponseWriter, r *http.Request) {
    21  		auth := r.Header.Get("Authorization")
    22  		if auth != secret {
    23  			fmt.Println(auth)
    24  			http.Error(w, "Auth header was incorrect", http.StatusUnauthorized)
    25  			return
    26  		}
    27  		if r.Method != http.MethodGet {
    28  			w.WriteHeader(http.StatusMethodNotAllowed)
    29  			return
    30  		}
    31  
    32  		w.Header().Add("Content-Type", "application/json")
    33  
    34  		h := &models.OverallHealthStatus{
    35  			Status: result,
    36  		}
    37  
    38  		b, _ := json.Marshal(h)
    39  		if _, err := w.Write(b); err != nil {
    40  			panic(err)
    41  		}
    42  
    43  		w.WriteHeader(http.StatusOK)
    44  	})
    45  
    46  	return httptest.NewServer(mux)
    47  }