...

Source file src/github.com/onsi/gomega/matchers/have_http_body_matcher_test.go

Documentation: github.com/onsi/gomega/matchers

     1  package matchers_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"strings"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/internal/gutil"
    13  )
    14  
    15  var _ = Describe("HaveHTTPBody", func() {
    16  	When("ACTUAL is *http.Response", func() {
    17  		It("matches the body", func() {
    18  			const body = "this is the body"
    19  			resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
    20  			Expect(resp).To(HaveHTTPBody(body))
    21  		})
    22  
    23  		It("mismatches the body", func() {
    24  			const body = "this is the body"
    25  			resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
    26  			Expect(resp).NotTo(HaveHTTPBody("something else"))
    27  		})
    28  
    29  		It("matches the body against later calls", func() {
    30  			firstCall := true
    31  			getResp := func() *http.Response {
    32  				if firstCall {
    33  					firstCall = false
    34  					return &http.Response{Body: io.NopCloser(strings.NewReader("first_call"))}
    35  				} else {
    36  					return &http.Response{Body: io.NopCloser(strings.NewReader("later_call"))}
    37  				}
    38  			}
    39  			Eventually(getResp).MustPassRepeatedly(2).Should(HaveHTTPBody([]byte("later_call")))
    40  		})
    41  	})
    42  
    43  	When("ACTUAL is *httptest.ResponseRecorder", func() {
    44  		It("matches the body", func() {
    45  			const body = "this is the body"
    46  			resp := &httptest.ResponseRecorder{Body: bytes.NewBufferString(body)}
    47  			Expect(resp).To(HaveHTTPBody(body))
    48  		})
    49  
    50  		It("mismatches the body", func() {
    51  			const body = "this is the body"
    52  			resp := &httptest.ResponseRecorder{Body: bytes.NewBufferString(body)}
    53  			Expect(resp).NotTo(HaveHTTPBody("something else"))
    54  		})
    55  
    56  		It("matches the body against later calls", func() {
    57  			firstCall := true
    58  			getResp := func() *httptest.ResponseRecorder {
    59  				if firstCall {
    60  					firstCall = false
    61  					return &httptest.ResponseRecorder{Body: bytes.NewBufferString("first_call")}
    62  				} else {
    63  					return &httptest.ResponseRecorder{Body: bytes.NewBufferString("later_call")}
    64  				}
    65  			}
    66  			Eventually(getResp).MustPassRepeatedly(2).Should(HaveHTTPBody([]byte("later_call")))
    67  		})
    68  	})
    69  
    70  	When("ACTUAL is neither *http.Response nor *httptest.ResponseRecorder", func() {
    71  		It("errors", func() {
    72  			failures := InterceptGomegaFailures(func() {
    73  				Expect("foo").To(HaveHTTPBody("bar"))
    74  			})
    75  			Expect(failures).To(ConsistOf("HaveHTTPBody matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n    <string>: foo"))
    76  		})
    77  	})
    78  
    79  	When("EXPECTED is []byte", func() {
    80  		It("matches the body", func() {
    81  			const body = "this is the body"
    82  			resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
    83  			Expect(resp).To(HaveHTTPBody([]byte(body)))
    84  		})
    85  
    86  		It("mismatches the body", func() {
    87  			const body = "this is the body"
    88  			resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
    89  			Expect(resp).NotTo(HaveHTTPBody([]byte("something else")))
    90  		})
    91  	})
    92  
    93  	When("EXPECTED is a submatcher", func() {
    94  		It("matches the body", func() {
    95  			resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
    96  			Expect(resp).To(HaveHTTPBody(MatchJSON(`{ "some": "json" }`)))
    97  		})
    98  
    99  		It("mismatches the body", func() {
   100  			resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
   101  			Expect(resp).NotTo(HaveHTTPBody(MatchJSON(`{ "something": "different" }`)))
   102  		})
   103  	})
   104  
   105  	When("EXPECTED is something else", func() {
   106  		It("errors", func() {
   107  			failures := InterceptGomegaFailures(func() {
   108  				resp := &http.Response{Body: gutil.NopCloser(strings.NewReader("body"))}
   109  				Expect(resp).To(HaveHTTPBody(map[int]bool{}))
   110  			})
   111  			Expect(failures).To(HaveLen(1))
   112  			Expect(failures[0]).To(Equal("HaveHTTPBody matcher expects string, []byte, or GomegaMatcher. Got:\n    <map[int]bool | len:0>: {}"))
   113  		})
   114  	})
   115  
   116  	Describe("FailureMessage", func() {
   117  		Context("EXPECTED is string", func() {
   118  			It("returns a match failure message", func() {
   119  				failures := InterceptGomegaFailures(func() {
   120  					resp := &http.Response{Body: gutil.NopCloser(strings.NewReader("this is the body"))}
   121  					Expect(resp).To(HaveHTTPBody("this is a different body"))
   122  				})
   123  				Expect(failures).To(HaveLen(1))
   124  				Expect(failures[0]).To(Equal(`Expected
   125      <string>: this is the body
   126  to equal
   127      <string>: this is a different body`), failures[0])
   128  			})
   129  		})
   130  
   131  		Context("EXPECTED is []byte", func() {
   132  			It("returns a match failure message", func() {
   133  				failures := InterceptGomegaFailures(func() {
   134  					resp := &http.Response{Body: gutil.NopCloser(strings.NewReader("this is the body"))}
   135  					Expect(resp).To(HaveHTTPBody([]byte("this is a different body")))
   136  				})
   137  				Expect(failures).To(HaveLen(1))
   138  				Expect(failures[0]).To(MatchRegexp(`^Expected
   139      <\[\]uint8 \| len:\d+, cap:\d+>: this is the body
   140  to equal
   141      <\[\]uint8 ]| len:\d+, cap:\d+>: this is a different body$`))
   142  			})
   143  		})
   144  
   145  		Context("EXPECTED is submatcher", func() {
   146  			It("returns a match failure message", func() {
   147  				failures := InterceptGomegaFailures(func() {
   148  					resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
   149  					Expect(resp).To(HaveHTTPBody(MatchJSON(`{"other":"stuff"}`)))
   150  				})
   151  				Expect(failures).To(HaveLen(1))
   152  				Expect(failures[0]).To(Equal(`Expected
   153      <string>: {
   154        "some": "json"
   155      }
   156  to match JSON of
   157      <string>: {
   158        "other": "stuff"
   159      }`))
   160  			})
   161  		})
   162  	})
   163  
   164  	Describe("NegatedFailureMessage", func() {
   165  		Context("EXPECTED is string", func() {
   166  			It("returns a negated failure message", func() {
   167  				const body = "this is the body"
   168  				failures := InterceptGomegaFailures(func() {
   169  					resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
   170  					Expect(resp).NotTo(HaveHTTPBody(body))
   171  				})
   172  				Expect(failures).To(HaveLen(1))
   173  				Expect(failures[0]).To(Equal(`Expected
   174      <string>: this is the body
   175  not to equal
   176      <string>: this is the body`))
   177  			})
   178  		})
   179  
   180  		Context("EXPECTED is []byte", func() {
   181  			It("returns a match failure message", func() {
   182  				const body = "this is the body"
   183  				failures := InterceptGomegaFailures(func() {
   184  					resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
   185  					Expect(resp).NotTo(HaveHTTPBody([]byte(body)))
   186  				})
   187  				Expect(failures).To(HaveLen(1))
   188  				Expect(failures[0]).To(MatchRegexp(`^Expected
   189      <\[\]uint8 \| len:\d+, cap:\d+>: this is the body
   190  not to equal
   191      <\[\]uint8 \| len:\d+, cap:\d+>: this is the body$`))
   192  			})
   193  		})
   194  
   195  		Context("EXPECTED is submatcher", func() {
   196  			It("returns a match failure message", func() {
   197  				const body = `{"some":"json"}`
   198  				failures := InterceptGomegaFailures(func() {
   199  					resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
   200  					Expect(resp).NotTo(HaveHTTPBody(MatchJSON(body)))
   201  				})
   202  				Expect(failures).To(HaveLen(1))
   203  				Expect(failures[0]).To(Equal(`Expected
   204      <string>: {
   205        "some": "json"
   206      }
   207  not to match JSON of
   208      <string>: {
   209        "some": "json"
   210      }`))
   211  			})
   212  		})
   213  	})
   214  })
   215  

View as plain text