github.com/engineyard/workflow-cli@v2.21.6+incompatible/pkg/testutil/testutil.go (about)

     1  package testutil
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/teamhephy/controller-sdk-go"
    13  	"github.com/teamhephy/workflow-cli/settings"
    14  )
    15  
    16  // TestServer represents a test HTTP server along with a path to a config file
    17  type TestServer struct {
    18  	Server *httptest.Server
    19  	Mux    *http.ServeMux
    20  }
    21  
    22  // NewTestServer sets up a test HTTP Server without a configuration file.
    23  func NewTestServer() *TestServer {
    24  	// test server
    25  	mux := http.NewServeMux()
    26  	server := httptest.NewServer(mux)
    27  
    28  	return &TestServer{
    29  		Server: server,
    30  		Mux:    mux,
    31  	}
    32  }
    33  
    34  // Close closes the test HTTP server.
    35  func (t *TestServer) Close() {
    36  	t.Server.Close()
    37  }
    38  
    39  // NewTestServerAndClient sets up a test HTTP Server along with a configuration file to talk to it
    40  func NewTestServerAndClient() (string, *TestServer, error) {
    41  	server := NewTestServer()
    42  
    43  	name, err := ioutil.TempDir("", "client")
    44  	if err != nil {
    45  		server.Close()
    46  		return "", nil, err
    47  	}
    48  
    49  	filename := filepath.Join(name, "test.json")
    50  
    51  	client, err := deis.New(false, server.Server.URL, "")
    52  	if err != nil {
    53  		server.Close()
    54  		return "", nil, err
    55  	}
    56  
    57  	config := settings.Settings{
    58  		Username: "test",
    59  		Client:   client,
    60  	}
    61  
    62  	filename, err = config.Save(filename)
    63  	if err != nil {
    64  		server.Close()
    65  		return "", nil, err
    66  	}
    67  	return filename, server, nil
    68  }
    69  
    70  // AssertBody asserts the value of the body of a request.
    71  func AssertBody(t *testing.T, expected interface{}, req *http.Request) {
    72  	body, err := ioutil.ReadAll(req.Body)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	value, err := json.Marshal(expected)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	if string(body) != string(value) {
    83  		t.Errorf("Expected body '%s' actually got '%s'\n", value, body)
    84  	}
    85  }
    86  
    87  // StripProgress strips the output from the progress method
    88  func StripProgress(input string) string {
    89  	first := strings.Index(input, "\b")
    90  	// If \b charecter not part of string
    91  	if first == -1 {
    92  		return input
    93  	}
    94  	last := strings.LastIndex(input, "\b")
    95  
    96  	// return string without \b or the characters it deletes.
    97  	return input[:first-(last-first+1)] + input[last+1:]
    98  }
    99  
   100  // SetHeaders sets standard headers for requests
   101  func SetHeaders(w http.ResponseWriter) {
   102  	w.Header().Add("DEIS_API_VERSION", deis.APIVersion)
   103  }