github.com/gophercloud/gophercloud@v1.11.0/testhelper/http_responses.go (about)

     1  package testhelper
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"net/url"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  var (
    16  	// Mux is a multiplexer that can be used to register handlers.
    17  	Mux *http.ServeMux
    18  
    19  	// Server is an in-memory HTTP server for testing.
    20  	Server *httptest.Server
    21  )
    22  
    23  // SetupPersistentPortHTTP prepares the Mux and Server listening specific port.
    24  func SetupPersistentPortHTTP(t *testing.T, port int) {
    25  	l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
    26  	if err != nil {
    27  		t.Errorf("Failed to listen to 127.0.0.1:%d: %s", port, err)
    28  	}
    29  	Mux = http.NewServeMux()
    30  	Server = httptest.NewUnstartedServer(Mux)
    31  	Server.Listener = l
    32  	Server.Start()
    33  }
    34  
    35  // SetupHTTP prepares the Mux and Server.
    36  func SetupHTTP() {
    37  	Mux = http.NewServeMux()
    38  	Server = httptest.NewServer(Mux)
    39  }
    40  
    41  // TeardownHTTP releases HTTP-related resources.
    42  func TeardownHTTP() {
    43  	Server.Close()
    44  }
    45  
    46  // Endpoint returns a fake endpoint that will actually target the Mux.
    47  func Endpoint() string {
    48  	return Server.URL + "/"
    49  }
    50  
    51  // TestFormValues ensures that all the URL parameters given to the http.Request are the same as values.
    52  func TestFormValues(t *testing.T, r *http.Request, values map[string]string) {
    53  	want := url.Values{}
    54  	for k, v := range values {
    55  		want.Add(k, v)
    56  	}
    57  
    58  	r.ParseForm()
    59  	if !reflect.DeepEqual(want, r.Form) {
    60  		t.Errorf("Request parameters = %v, want %v", r.Form, want)
    61  	}
    62  }
    63  
    64  // TestMethod checks that the Request has the expected method (e.g. GET, POST).
    65  func TestMethod(t *testing.T, r *http.Request, expected string) {
    66  	if expected != r.Method {
    67  		t.Errorf("Request method = %v, expected %v", r.Method, expected)
    68  	}
    69  }
    70  
    71  // TestHeader checks that the header on the http.Request matches the expected value.
    72  func TestHeader(t *testing.T, r *http.Request, header string, expected string) {
    73  	if len(r.Header.Values(header)) == 0 {
    74  		t.Errorf("Header %s not found, expected %q", header, expected)
    75  		return
    76  	}
    77  	for _, actual := range r.Header.Values(header) {
    78  		if expected != actual {
    79  			t.Errorf("Header %s = %q, expected %q", header, actual, expected)
    80  		}
    81  	}
    82  }
    83  
    84  // TestHeaderUnset checks that the header on the http.Request doesn't exist.
    85  func TestHeaderUnset(t *testing.T, r *http.Request, header string) {
    86  	if len(r.Header.Values(header)) > 0 {
    87  		t.Errorf("Header %s is not expected", header)
    88  	}
    89  }
    90  
    91  // TestBody verifies that the request body matches an expected body.
    92  func TestBody(t *testing.T, r *http.Request, expected string) {
    93  	b, err := ioutil.ReadAll(r.Body)
    94  	if err != nil {
    95  		t.Errorf("Unable to read body: %v", err)
    96  	}
    97  	str := string(b)
    98  	if expected != str {
    99  		t.Errorf("Body = %s, expected %s", str, expected)
   100  	}
   101  }
   102  
   103  // TestJSONRequest verifies that the JSON payload of a request matches an expected structure, without asserting things about
   104  // whitespace or ordering.
   105  func TestJSONRequest(t *testing.T, r *http.Request, expected string) {
   106  	b, err := ioutil.ReadAll(r.Body)
   107  	if err != nil {
   108  		t.Errorf("Unable to read request body: %v", err)
   109  	}
   110  
   111  	var actualJSON interface{}
   112  	err = json.Unmarshal(b, &actualJSON)
   113  	if err != nil {
   114  		t.Errorf("Unable to parse request body as JSON: %v", err)
   115  	}
   116  
   117  	CheckJSONEquals(t, expected, actualJSON)
   118  }