github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/net/request_matcher.go (about)

     1  package net
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  type JSONMapRequest map[string]interface{}
    15  
    16  func (json *JSONMapRequest) String() string {
    17  	return fmt.Sprintf("%#v", *json)
    18  }
    19  
    20  type JSONArrayRequest []interface{}
    21  
    22  func (json *JSONArrayRequest) String() string {
    23  	return fmt.Sprintf("%#v", *json)
    24  }
    25  
    26  func bytesToInterface(jsonBytes []byte) (interface{}, error) {
    27  	mapResult := &JSONMapRequest{}
    28  	err := json.Unmarshal(jsonBytes, mapResult)
    29  	if err == nil {
    30  		return mapResult, err
    31  	}
    32  
    33  	arrayResult := &JSONArrayRequest{}
    34  	err = json.Unmarshal(jsonBytes, arrayResult)
    35  	return arrayResult, err
    36  }
    37  
    38  func EmptyQueryParamMatcher() RequestMatcher {
    39  	return func(request *http.Request) {
    40  		defer GinkgoRecover()
    41  		Expect(request.URL.RawQuery).To(Equal(""))
    42  	}
    43  }
    44  
    45  func RequestBodyMatcher(expectedBodyString string) RequestMatcher {
    46  	return func(request *http.Request) {
    47  		defer GinkgoRecover()
    48  		bodyBytes, err := ioutil.ReadAll(request.Body)
    49  		if err != nil {
    50  			Fail(fmt.Sprintf("Error reading request body: %s", err))
    51  		}
    52  
    53  		actualBody, err := bytesToInterface(bodyBytes)
    54  		if err != nil {
    55  			Fail(fmt.Sprintf("Error unmarshalling request: %s", err.Error()))
    56  		}
    57  
    58  		expectedBody, err := bytesToInterface([]byte(expectedBodyString))
    59  		if err != nil {
    60  			Fail(fmt.Sprintf("Error unmarshalling expected json: %s", err.Error()))
    61  		}
    62  
    63  		Expect(actualBody).To(Equal(expectedBody))
    64  		Expect(request.Header.Get("content-type")).To(Equal("application/json"))
    65  	}
    66  }
    67  
    68  func RequestBodyMatcherWithContentType(expectedBody, expectedContentType string) RequestMatcher {
    69  	return func(request *http.Request) {
    70  		defer GinkgoRecover()
    71  		bodyBytes, err := ioutil.ReadAll(request.Body)
    72  		if err != nil {
    73  			Fail(fmt.Sprintf("Error reading request body: %s", err))
    74  		}
    75  
    76  		actualBody := string(bodyBytes)
    77  		Expect(RemoveWhiteSpaceFromBody(actualBody)).To(Equal(RemoveWhiteSpaceFromBody(expectedBody)), "Body did not match.")
    78  
    79  		actualContentType := request.Header.Get("content-type")
    80  		Expect(actualContentType).To(Equal(expectedContentType), "Content Type did not match.")
    81  	}
    82  }
    83  
    84  func RemoveWhiteSpaceFromBody(body string) string {
    85  	body = strings.Replace(body, " ", "", -1)
    86  	body = strings.Replace(body, "\n", "", -1)
    87  	body = strings.Replace(body, "\r", "", -1)
    88  	body = strings.Replace(body, "\t", "", -1)
    89  	return body
    90  }