github.com/wuhuizuo/gomplate@v3.5.0+incompatible/vault/testutils.go (about)

     1  package vault
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  
     9  	"github.com/hashicorp/vault/api"
    10  )
    11  
    12  // MockServer -
    13  func MockServer(code int, body string) (*httptest.Server, *Vault) {
    14  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  		w.WriteHeader(code)
    16  		// nolint: errcheck
    17  		fmt.Fprintln(w, body)
    18  	}))
    19  
    20  	tr := &http.Transport{
    21  		Proxy: func(req *http.Request) (*url.URL, error) {
    22  			return url.Parse(server.URL)
    23  		},
    24  	}
    25  	httpClient := &http.Client{Transport: tr}
    26  	config := &api.Config{
    27  		Address:    server.URL,
    28  		HttpClient: httpClient,
    29  	}
    30  	// nolint: gosec
    31  	c, _ := api.NewClient(config)
    32  	return server, &Vault{c}
    33  }