github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/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 RequestBodyMatcher(expectedBodyString string) RequestMatcher {
    39  	return func(request *http.Request) {
    40  		defer GinkgoRecover()
    41  		bodyBytes, err := ioutil.ReadAll(request.Body)
    42  		if err != nil {
    43  			Fail(fmt.Sprintf("Error reading request body: %s", err))
    44  		}
    45  
    46  		actualBody, err := bytesToInterface(bodyBytes)
    47  		if err != nil {
    48  			Fail(fmt.Sprintf("Error unmarshalling request", err.Error()))
    49  		}
    50  
    51  		expectedBody, err := bytesToInterface([]byte(expectedBodyString))
    52  		if err != nil {
    53  			Fail(fmt.Sprintf("Error unmarshalling expected json", err.Error()))
    54  		}
    55  
    56  		Expect(actualBody).To(Equal(expectedBody))
    57  		Expect(request.Header.Get("content-type")).To(Equal("application/json"))
    58  	}
    59  }
    60  
    61  func RequestBodyMatcherWithContentType(expectedBody, expectedContentType string) RequestMatcher {
    62  	return func(request *http.Request) {
    63  		defer GinkgoRecover()
    64  		bodyBytes, err := ioutil.ReadAll(request.Body)
    65  		if err != nil {
    66  			Fail(fmt.Sprintf("Error reading request body: %s", err))
    67  		}
    68  
    69  		actualBody := string(bodyBytes)
    70  		Expect(RemoveWhiteSpaceFromBody(actualBody)).To(Equal(RemoveWhiteSpaceFromBody(expectedBody)), "Body did not match.")
    71  
    72  		actualContentType := request.Header.Get("content-type")
    73  		Expect(actualContentType).To(Equal(expectedContentType), "Content Type did not match.")
    74  	}
    75  }
    76  
    77  func RemoveWhiteSpaceFromBody(body string) string {
    78  	body = strings.Replace(body, " ", "", -1)
    79  	body = strings.Replace(body, "\n", "", -1)
    80  	body = strings.Replace(body, "\r", "", -1)
    81  	body = strings.Replace(body, "\t", "", -1)
    82  	return body
    83  }