How to use AutoDumpResponse method of tdhttp Package

Best Go-testdeep code snippet using tdhttp.AutoDumpResponse

test_api_test.go

Source:test_api_test.go Github

copy

Full Screen

...574 CmpCookies(td.Len(2)). // succeeds575 Failed())576 td.CmpContains(t, mockT.LogBuf(),577 "Failed test 'cookies should match'")578 // AutoDumpResponse579 mockT = tdutil.NewT("test")580 td.CmpTrue(t,581 tdhttp.NewTestAPI(mockT, mux).582 AutoDumpResponse().583 Get("/any/cookies").584 Name("my test").585 CmpCookies(td.Len(100)).586 Failed())587 td.CmpContains(t, mockT.LogBuf(),588 "Failed test 'my test: cookies should match'")589 td.CmpContains(t, mockT.LogBuf(), "Response.Cookie: bad length")590 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))591 // Request not sent592 mockT = tdutil.NewT("test")593 ta := tdhttp.NewTestAPI(mockT, mux).594 Name("my test").595 CmpCookies(td.Len(2))596 td.CmpTrue(t, ta.Failed())597 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")598 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")599 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")600 td.CmpNot(t, mockT.LogBuf(), td.Contains("No response received yet\n"))601 })602 t.Run("Trailer", func(t *testing.T) {603 mockT := tdutil.NewT("test")604 td.CmpFalse(t,605 tdhttp.NewTestAPI(mockT, mux).606 Get("/any").607 CmpStatus(200).608 CmpTrailer(nil). // No trailer at all609 Failed())610 mockT = tdutil.NewT("test")611 td.CmpFalse(t,612 tdhttp.NewTestAPI(mockT, mux).613 Get("/any/trailer").614 CmpStatus(200).615 CmpTrailer(containsKey).616 Failed())617 mockT = tdutil.NewT("test")618 td.CmpFalse(t,619 tdhttp.NewTestAPI(mockT, mux).620 Get("/any/trailer").621 CmpStatus(200).622 CmpTrailer(http.Header{623 "X-Testdeep-Method": {"GET"},624 "X-Testdeep-Foo": {"bar"},625 }).626 Failed())627 // AutoDumpResponse628 mockT = tdutil.NewT("test")629 td.CmpTrue(t,630 tdhttp.NewTestAPI(mockT, mux).631 AutoDumpResponse().632 Get("/any/trailer").633 Name("my test").634 CmpTrailer(http.Header{}).635 Failed())636 td.CmpContains(t, mockT.LogBuf(),637 "Failed test 'my test: trailer should match'")638 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))639 // OrDumpResponse640 mockT = tdutil.NewT("test")641 td.CmpTrue(t,642 tdhttp.NewTestAPI(mockT, mux).643 Get("/any/trailer").644 Name("my test").645 CmpTrailer(http.Header{}).646 OrDumpResponse().647 OrDumpResponse(). // only one log648 Failed())649 td.CmpContains(t, mockT.LogBuf(),650 "Failed test 'my test: trailer should match'")651 logPos := strings.Index(mockT.LogBuf(), "Received response:\n")652 if td.Cmp(t, logPos, td.Gte(0)) {653 // Only one occurrence654 td.Cmp(t,655 strings.Index(mockT.LogBuf()[logPos+1:], "Received response:\n"),656 -1)657 }658 mockT = tdutil.NewT("test")659 ta := tdhttp.NewTestAPI(mockT, mux).660 Name("my test").661 CmpTrailer(http.Header{})662 td.CmpTrue(t, ta.Failed())663 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")664 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")665 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")666 td.CmpNot(t, mockT.LogBuf(), td.Contains("No response received yet\n"))667 end := len(mockT.LogBuf())668 ta.OrDumpResponse()669 td.CmpContains(t, mockT.LogBuf()[end:], "No response received yet\n")670 })671 t.Run("Status error", func(t *testing.T) {672 mockT := tdutil.NewT("test")673 td.CmpTrue(t,674 tdhttp.NewTestAPI(mockT, mux).675 Get("/any").676 CmpStatus(400).677 Failed())678 td.CmpContains(t, mockT.LogBuf(),679 "Failed test 'status code should match'")680 // Error followed by a success: Failed() should return true anyway681 mockT = tdutil.NewT("test")682 td.CmpTrue(t,683 tdhttp.NewTestAPI(mockT, mux).684 Get("/any").685 CmpStatus(400). // fails686 CmpStatus(200). // succeeds687 Failed())688 td.CmpContains(t, mockT.LogBuf(),689 "Failed test 'status code should match'")690 mockT = tdutil.NewT("test")691 td.CmpTrue(t,692 tdhttp.NewTestAPI(mockT, mux).693 Get("/any").694 Name("my test").695 CmpStatus(400).696 Failed())697 td.CmpContains(t, mockT.LogBuf(),698 "Failed test 'my test: status code should match'")699 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))700 // AutoDumpResponse701 mockT = tdutil.NewT("test")702 td.CmpTrue(t,703 tdhttp.NewTestAPI(mockT, mux).704 AutoDumpResponse().705 Get("/any").706 Name("my test").707 CmpStatus(400).708 Failed())709 td.CmpContains(t, mockT.LogBuf(),710 "Failed test 'my test: status code should match'")711 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))712 // OrDumpResponse713 mockT = tdutil.NewT("test")714 td.CmpTrue(t,715 tdhttp.NewTestAPI(mockT, mux).716 Get("/any").717 Name("my test").718 CmpStatus(400).719 OrDumpResponse().720 OrDumpResponse(). // only one log721 Failed())722 td.CmpContains(t, mockT.LogBuf(),723 "Failed test 'my test: status code should match'")724 logPos := strings.Index(mockT.LogBuf(), "Received response:\n")725 if td.Cmp(t, logPos, td.Gte(0)) {726 // Only one occurrence727 td.Cmp(t,728 strings.Index(mockT.LogBuf()[logPos+1:], "Received response:\n"),729 -1)730 }731 mockT = tdutil.NewT("test")732 ta := tdhttp.NewTestAPI(mockT, mux).733 Name("my test").734 CmpStatus(400)735 td.CmpTrue(t, ta.Failed())736 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")737 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")738 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")739 td.CmpNot(t, mockT.LogBuf(), td.Contains("No response received yet\n"))740 end := len(mockT.LogBuf())741 ta.OrDumpResponse()742 td.CmpContains(t, mockT.LogBuf()[end:], "No response received yet\n")743 })744 t.Run("Header error", func(t *testing.T) {745 mockT := tdutil.NewT("test")746 td.CmpTrue(t,747 tdhttp.NewTestAPI(mockT, mux).748 Get("/any").749 CmpHeader(td.Not(containsKey)).750 Failed())751 td.CmpContains(t, mockT.LogBuf(),752 "Failed test 'header should match'")753 // Error followed by a success: Failed() should return true anyway754 mockT = tdutil.NewT("test")755 td.CmpTrue(t,756 tdhttp.NewTestAPI(mockT, mux).757 Get("/any").758 CmpHeader(td.Not(containsKey)). // fails759 CmpHeader(td.Ignore()). // succeeds760 Failed())761 td.CmpContains(t, mockT.LogBuf(),762 "Failed test 'header should match'")763 mockT = tdutil.NewT("test")764 td.CmpTrue(t,765 tdhttp.NewTestAPI(mockT, mux).766 Get("/any").767 Name("my test").768 CmpHeader(td.Not(containsKey)).769 Failed())770 td.CmpContains(t, mockT.LogBuf(),771 "Failed test 'my test: header should match'")772 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))773 // AutoDumpResponse774 mockT = tdutil.NewT("test")775 td.CmpTrue(t,776 tdhttp.NewTestAPI(mockT, mux).777 AutoDumpResponse().778 Get("/any").779 Name("my test").780 CmpHeader(td.Not(containsKey)).781 Failed())782 td.CmpContains(t, mockT.LogBuf(),783 "Failed test 'my test: header should match'")784 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))785 mockT = tdutil.NewT("test")786 td.CmpTrue(t,787 tdhttp.NewTestAPI(mockT, mux).788 Name("my test").789 CmpHeader(td.Not(containsKey)).790 Failed())791 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")792 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")793 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")794 })795 t.Run("Body error", func(t *testing.T) {796 mockT := tdutil.NewT("test")797 td.CmpTrue(t,798 tdhttp.NewTestAPI(mockT, mux).799 Get("/any").800 CmpBody("xxx").801 Failed())802 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body contents is OK'")803 td.CmpContains(t, mockT.LogBuf(), "Response.Body: values differ\n")804 td.CmpContains(t, mockT.LogBuf(), `expected: "xxx"`)805 td.CmpContains(t, mockT.LogBuf(), `got: "GET!"`)806 // Error followed by a success: Failed() should return true anyway807 mockT = tdutil.NewT("test")808 td.CmpTrue(t,809 tdhttp.NewTestAPI(mockT, mux).810 Get("/any").811 CmpBody("xxx"). // fails812 CmpBody(td.Ignore()). // succeeds813 Failed())814 // Without AutoDumpResponse815 mockT = tdutil.NewT("test")816 td.CmpTrue(t,817 tdhttp.NewTestAPI(mockT, mux).818 Get("/any").819 Name("my test").820 CmpBody("xxx").821 Failed())822 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: body contents is OK'")823 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))824 // AutoDumpResponse825 mockT = tdutil.NewT("test")826 td.CmpTrue(t,827 tdhttp.NewTestAPI(mockT, mux).828 AutoDumpResponse().829 Get("/any").830 Name("my test").831 CmpBody("xxx").832 Failed())833 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: body contents is OK'")834 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))835 mockT = tdutil.NewT("test")836 td.CmpTrue(t,837 tdhttp.NewTestAPI(mockT, mux).838 Name("my test").839 CmpBody("xxx").840 Failed())841 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")842 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")843 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")844 // NoBody845 mockT = tdutil.NewT("test")846 td.CmpTrue(t,847 tdhttp.NewTestAPI(mockT, mux).848 Name("my test").849 NoBody().850 Failed())851 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")852 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")853 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")854 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))855 // Error followed by a success: Failed() should return true anyway856 mockT = tdutil.NewT("test")857 td.CmpTrue(t,858 tdhttp.NewTestAPI(mockT, mux).859 Name("my test").860 Head("/any").861 CmpBody("fail"). // fails862 NoBody(). // succeeds863 Failed())864 // No JSON body865 mockT = tdutil.NewT("test")866 td.CmpTrue(t,867 tdhttp.NewTestAPI(mockT, mux).868 Head("/any").869 CmpStatus(200).870 CmpHeader(containsKey).871 CmpJSONBody(json.RawMessage(`{}`)).872 Failed())873 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body should not be empty'")874 td.CmpContains(t, mockT.LogBuf(), "Response body is empty!")875 td.CmpContains(t, mockT.LogBuf(), "Body cannot be empty when using CmpJSONBody")876 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))877 // Error followed by a success: Failed() should return true anyway878 mockT = tdutil.NewT("test")879 td.CmpTrue(t,880 tdhttp.NewTestAPI(mockT, mux).881 Get("/any/json").882 CmpStatus(200).883 CmpHeader(containsKey).884 CmpJSONBody(json.RawMessage(`{}`)). // fails885 CmpJSONBody(td.Ignore()). // succeeds886 Failed())887 // No JSON body + AutoDumpResponse888 mockT = tdutil.NewT("test")889 td.CmpTrue(t,890 tdhttp.NewTestAPI(mockT, mux).891 AutoDumpResponse().892 Head("/any").893 CmpStatus(200).894 CmpHeader(containsKey).895 CmpJSONBody(json.RawMessage(`{}`)).896 Failed())897 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body should not be empty'")898 td.CmpContains(t, mockT.LogBuf(), "Response body is empty!")899 td.CmpContains(t, mockT.LogBuf(), "Body cannot be empty when using CmpJSONBody")900 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))901 // No XML body902 mockT = tdutil.NewT("test")903 td.CmpTrue(t,904 tdhttp.NewTestAPI(mockT, mux).905 Head("/any").906 CmpStatus(200).907 CmpHeader(containsKey).908 CmpXMLBody(struct{ Test string }{}).909 Failed())910 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body should not be empty'")911 td.CmpContains(t, mockT.LogBuf(), "Response body is empty!")912 td.CmpContains(t, mockT.LogBuf(), "Body cannot be empty when using CmpXMLBody")913 })914 t.Run("Response error", func(t *testing.T) {915 mockT := tdutil.NewT("test")916 td.CmpTrue(t,917 tdhttp.NewTestAPI(mockT, mux).918 Get("/any").919 CmpResponse(nil).920 Failed())921 td.CmpContains(t, mockT.LogBuf(), "Failed test 'full response should match'")922 td.CmpContains(t, mockT.LogBuf(), "Response: values differ")923 td.CmpContains(t, mockT.LogBuf(), "got: (*http.Response)(")924 td.CmpContains(t, mockT.LogBuf(), "expected: nil")925 // Error followed by a success: Failed() should return true anyway926 mockT = tdutil.NewT("test")927 td.CmpTrue(t,928 tdhttp.NewTestAPI(mockT, mux).929 Get("/any").930 CmpResponse(nil). // fails931 CmpResponse(td.Ignore()). // succeeds932 Failed())933 // Without AutoDumpResponse934 mockT = tdutil.NewT("test")935 td.CmpTrue(t,936 tdhttp.NewTestAPI(mockT, mux).937 Get("/any").938 Name("my test").939 CmpResponse(nil).940 Failed())941 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: full response should match'")942 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))943 // AutoDumpResponse944 mockT = tdutil.NewT("test")945 td.CmpTrue(t,946 tdhttp.NewTestAPI(mockT, mux).947 AutoDumpResponse().948 Get("/any").949 Name("my test").950 CmpResponse(nil).951 Failed())952 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: full response should match'")953 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))954 mockT = tdutil.NewT("test")955 td.CmpTrue(t,956 tdhttp.NewTestAPI(mockT, mux).957 Name("my test").958 CmpResponse(nil).959 Failed())960 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")961 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")...

Full Screen

Full Screen

test_api.go

Source:test_api.go Github

copy

Full Screen

...114 return ta.t.Run(name, func(tdt *td.T) {115 f(NewTestAPI(tdt, ta.handler))116 })117}118// AutoDumpResponse allows to dump the HTTP response when the first119// error is encountered after a request.120//121// ta.AutoDumpResponse()122// ta.AutoDumpResponse(true)123//124// both enable the dump.125func (ta *TestAPI) AutoDumpResponse(enable ...bool) *TestAPI {126 ta.autoDumpResponse = len(enable) == 0 || enable[0]127 return ta128}129// Name allows to name the series of tests that follow. This name is130// used as a prefix for all following tests, in case of failure to131// qualify each test. If len(args) > 1 and the first item of args is132// a string and contains a '%' rune then [fmt.Fprintf] is used to133// compose the name, else args are passed to [fmt.Fprint].134func (ta *TestAPI) Name(args ...any) *TestAPI {135 ta.name = tdutil.BuildTestName(args...)136 if ta.name != "" {137 ta.name += ": "138 }139 return ta140}141// Request sends a new HTTP request to the tested API. Any Cmp* or142// [TestAPI.NoBody] methods can now be called.143//144// Note that [TestAPI.Failed] status is reset just after this call.145func (ta *TestAPI) Request(req *http.Request) *TestAPI {146 ta.response = httptest.NewRecorder()147 ta.failed = 0148 ta.sentAt = time.Now().Truncate(0)149 ta.responseDumped = false150 ta.handler.ServeHTTP(ta.response, req)151 return ta152}153func (ta *TestAPI) checkRequestSent() bool {154 ta.t.Helper()155 // If no request has been sent, display a nice error message156 return ta.t.RootName("Request").157 Code(ta.response != nil,158 func(sent bool) error {159 if sent {160 return nil161 }162 return &ctxerr.Error{163 Message: "%% not sent!",164 Summary: ctxerr.NewSummary("A request must be sent before testing status, header, body or full response"),165 }166 },167 ta.name+"request is sent")168}169// Failed returns true if any Cmp* or [TestAPI.NoBody] method failed since last170// request sending.171func (ta *TestAPI) Failed() bool {172 return ta.failed != 0173}174// Get sends a HTTP GET to the tested API. Any Cmp* or [TestAPI.NoBody] methods175// can now be called.176//177// Note that [TestAPI.Failed] status is reset just after this call.178//179// See [NewRequest] for all possible formats accepted in headersQueryParams.180func (ta *TestAPI) Get(target string, headersQueryParams ...any) *TestAPI {181 ta.t.Helper()182 req, err := get(target, headersQueryParams...)183 if err != nil {184 ta.t.Fatal(err)185 }186 return ta.Request(req)187}188// Head sends a HTTP HEAD to the tested API. Any Cmp* or [TestAPI.NoBody] methods189// can now be called.190//191// Note that [TestAPI.Failed] status is reset just after this call.192//193// See [NewRequest] for all possible formats accepted in headersQueryParams.194func (ta *TestAPI) Head(target string, headersQueryParams ...any) *TestAPI {195 ta.t.Helper()196 req, err := head(target, headersQueryParams...)197 if err != nil {198 ta.t.Fatal(err)199 }200 return ta.Request(req)201}202// Options sends a HTTP OPTIONS to the tested API. Any Cmp* or203// [TestAPI.NoBody] methods can now be called.204//205// Note that [TestAPI.Failed] status is reset just after this call.206//207// See [NewRequest] for all possible formats accepted in headersQueryParams.208func (ta *TestAPI) Options(target string, body io.Reader, headersQueryParams ...any) *TestAPI {209 ta.t.Helper()210 req, err := options(target, body, headersQueryParams...)211 if err != nil {212 ta.t.Fatal(err)213 }214 return ta.Request(req)215}216// Post sends a HTTP POST to the tested API. Any Cmp* or217// [TestAPI.NoBody] methods can now be called.218//219// Note that [TestAPI.Failed] status is reset just after this call.220//221// See [NewRequest] for all possible formats accepted in headersQueryParams.222func (ta *TestAPI) Post(target string, body io.Reader, headersQueryParams ...any) *TestAPI {223 ta.t.Helper()224 req, err := post(target, body, headersQueryParams...)225 if err != nil {226 ta.t.Fatal(err)227 }228 return ta.Request(req)229}230// PostForm sends a HTTP POST with data's keys and values URL-encoded231// as the request body to the tested API. "Content-Type" header is232// automatically set to "application/x-www-form-urlencoded". Any Cmp*233// or [TestAPI.NoBody] methods can now be called.234//235// Note that [TestAPI.Failed] status is reset just after this call.236//237// See [NewRequest] for all possible formats accepted in headersQueryParams.238func (ta *TestAPI) PostForm(target string, data URLValuesEncoder, headersQueryParams ...any) *TestAPI {239 ta.t.Helper()240 req, err := postForm(target, data, headersQueryParams...)241 if err != nil {242 ta.t.Fatal(err)243 }244 return ta.Request(req)245}246// PostMultipartFormData sends a HTTP POST multipart request, like247// multipart/form-data one for example. See [MultipartBody] type for248// details. "Content-Type" header is automatically set depending on249// data.MediaType (defaults to "multipart/form-data") and250// data.Boundary (defaults to "go-testdeep-42"). Any Cmp* or251// [TestAPI.NoBody] methods can now be called.252//253// Note that [TestAPI.Failed] status is reset just after this call.254//255// ta.PostMultipartFormData("/data",256// &tdhttp.MultipartBody{257// // "multipart/form-data" by default258// Parts: []*tdhttp.MultipartPart{259// tdhttp.NewMultipartPartString("type", "Sales"),260// tdhttp.NewMultipartPartFile("report", "report.json", "application/json"),261// },262// },263// "X-Foo", "Foo-value",264// "X-Zip", "Zip-value",265// )266//267// See [NewRequest] for all possible formats accepted in headersQueryParams.268func (ta *TestAPI) PostMultipartFormData(target string, data *MultipartBody, headersQueryParams ...any) *TestAPI {269 ta.t.Helper()270 req, err := postMultipartFormData(target, data, headersQueryParams...)271 if err != nil {272 ta.t.Fatal(err)273 }274 return ta.Request(req)275}276// Put sends a HTTP PUT to the tested API. Any Cmp* or [TestAPI.NoBody] methods277// can now be called.278//279// Note that [TestAPI.Failed] status is reset just after this call.280//281// See [NewRequest] for all possible formats accepted in headersQueryParams.282func (ta *TestAPI) Put(target string, body io.Reader, headersQueryParams ...any) *TestAPI {283 ta.t.Helper()284 req, err := put(target, body, headersQueryParams...)285 if err != nil {286 ta.t.Fatal(err)287 }288 return ta.Request(req)289}290// Patch sends a HTTP PATCH to the tested API. Any Cmp* or [TestAPI.NoBody] methods291// can now be called.292//293// Note that [TestAPI.Failed] status is reset just after this call.294//295// See [NewRequest] for all possible formats accepted in headersQueryParams.296func (ta *TestAPI) Patch(target string, body io.Reader, headersQueryParams ...any) *TestAPI {297 ta.t.Helper()298 req, err := patch(target, body, headersQueryParams...)299 if err != nil {300 ta.t.Fatal(err)301 }302 return ta.Request(req)303}304// Delete sends a HTTP DELETE to the tested API. Any Cmp* or [TestAPI.NoBody] methods305// can now be called.306//307// Note that [TestAPI.Failed] status is reset just after this call.308//309// See [NewRequest] for all possible formats accepted in headersQueryParams.310func (ta *TestAPI) Delete(target string, body io.Reader, headersQueryParams ...any) *TestAPI {311 ta.t.Helper()312 req, err := del(target, body, headersQueryParams...)313 if err != nil {314 ta.t.Fatal(err)315 }316 return ta.Request(req)317}318// NewJSONRequest sends a HTTP request with body marshaled to319// JSON. "Content-Type" header is automatically set to320// "application/json". Any Cmp* or [TestAPI.NoBody] methods can now be called.321//322// Note that [TestAPI.Failed] status is reset just after this call.323//324// See [NewRequest] for all possible formats accepted in headersQueryParams.325func (ta *TestAPI) NewJSONRequest(method, target string, body any, headersQueryParams ...any) *TestAPI {326 ta.t.Helper()327 req, err := newJSONRequest(method, target, body, headersQueryParams...)328 if err != nil {329 ta.t.Fatal(err)330 }331 return ta.Request(req)332}333// PostJSON sends a HTTP POST with body marshaled to334// JSON. "Content-Type" header is automatically set to335// "application/json". Any Cmp* or [TestAPI.NoBody] methods can now be called.336//337// Note that [TestAPI.Failed] status is reset just after this call.338//339// See [NewRequest] for all possible formats accepted in headersQueryParams.340func (ta *TestAPI) PostJSON(target string, body any, headersQueryParams ...any) *TestAPI {341 ta.t.Helper()342 req, err := newJSONRequest(http.MethodPost, target, body, headersQueryParams...)343 if err != nil {344 ta.t.Fatal(err)345 }346 return ta.Request(req)347}348// PutJSON sends a HTTP PUT with body marshaled to349// JSON. "Content-Type" header is automatically set to350// "application/json". Any Cmp* or [TestAPI.NoBody] methods can now be called.351//352// Note that [TestAPI.Failed] status is reset just after this call.353//354// See [NewRequest] for all possible formats accepted in headersQueryParams.355func (ta *TestAPI) PutJSON(target string, body any, headersQueryParams ...any) *TestAPI {356 ta.t.Helper()357 req, err := newJSONRequest(http.MethodPut, target, body, headersQueryParams...)358 if err != nil {359 ta.t.Fatal(err)360 }361 return ta.Request(req)362}363// PatchJSON sends a HTTP PATCH with body marshaled to364// JSON. "Content-Type" header is automatically set to365// "application/json". Any Cmp* or [TestAPI.NoBody] methods can now be called.366//367// Note that [TestAPI.Failed] status is reset just after this call.368//369// See [NewRequest] for all possible formats accepted in headersQueryParams.370func (ta *TestAPI) PatchJSON(target string, body any, headersQueryParams ...any) *TestAPI {371 ta.t.Helper()372 req, err := newJSONRequest(http.MethodPatch, target, body, headersQueryParams...)373 if err != nil {374 ta.t.Fatal(err)375 }376 return ta.Request(req)377}378// DeleteJSON sends a HTTP DELETE with body marshaled to379// JSON. "Content-Type" header is automatically set to380// "application/json". Any Cmp* or [TestAPI.NoBody] methods can now be called.381//382// Note that [TestAPI.Failed] status is reset just after this call.383//384// See [NewRequest] for all possible formats accepted in headersQueryParams.385func (ta *TestAPI) DeleteJSON(target string, body any, headersQueryParams ...any) *TestAPI {386 ta.t.Helper()387 req, err := newJSONRequest(http.MethodDelete, target, body, headersQueryParams...)388 if err != nil {389 ta.t.Fatal(err)390 }391 return ta.Request(req)392}393// NewXMLRequest sends a HTTP request with body marshaled to394// XML. "Content-Type" header is automatically set to395// "application/xml". Any Cmp* or [TestAPI.NoBody] methods can now be called.396//397// Note that [TestAPI.Failed] status is reset just after this call.398//399// See [NewRequest] for all possible formats accepted in headersQueryParams.400func (ta *TestAPI) NewXMLRequest(method, target string, body any, headersQueryParams ...any) *TestAPI {401 ta.t.Helper()402 req, err := newXMLRequest(method, target, body, headersQueryParams...)403 if err != nil {404 ta.t.Fatal(err)405 }406 return ta.Request(req)407}408// PostXML sends a HTTP POST with body marshaled to409// XML. "Content-Type" header is automatically set to410// "application/xml". Any Cmp* or [TestAPI.NoBody] methods can now be called.411//412// Note that [TestAPI.Failed] status is reset just after this call.413//414// See [NewRequest] for all possible formats accepted in headersQueryParams.415func (ta *TestAPI) PostXML(target string, body any, headersQueryParams ...any) *TestAPI {416 ta.t.Helper()417 req, err := newXMLRequest(http.MethodPost, target, body, headersQueryParams...)418 if err != nil {419 ta.t.Fatal(err)420 }421 return ta.Request(req)422}423// PutXML sends a HTTP PUT with body marshaled to424// XML. "Content-Type" header is automatically set to425// "application/xml". Any Cmp* or [TestAPI.NoBody] methods can now be called.426//427// Note that [TestAPI.Failed] status is reset just after this call.428//429// See [NewRequest] for all possible formats accepted in headersQueryParams.430func (ta *TestAPI) PutXML(target string, body any, headersQueryParams ...any) *TestAPI {431 ta.t.Helper()432 req, err := newXMLRequest(http.MethodPut, target, body, headersQueryParams...)433 if err != nil {434 ta.t.Fatal(err)435 }436 return ta.Request(req)437}438// PatchXML sends a HTTP PATCH with body marshaled to439// XML. "Content-Type" header is automatically set to440// "application/xml". Any Cmp* or [TestAPI.NoBody] methods can now be called.441//442// Note that [TestAPI.Failed] status is reset just after this call.443//444// See [NewRequest] for all possible formats accepted in headersQueryParams.445func (ta *TestAPI) PatchXML(target string, body any, headersQueryParams ...any) *TestAPI {446 ta.t.Helper()447 req, err := newXMLRequest(http.MethodPatch, target, body, headersQueryParams...)448 if err != nil {449 ta.t.Fatal(err)450 }451 return ta.Request(req)452}453// DeleteXML sends a HTTP DELETE with body marshaled to454// XML. "Content-Type" header is automatically set to455// "application/xml". Any Cmp* or [TestAPI.NoBody] methods can now be called.456//457// Note that [TestAPI.Failed] status is reset just after this call.458//459// See [NewRequest] for all possible formats accepted in headersQueryParams.460func (ta *TestAPI) DeleteXML(target string, body any, headersQueryParams ...any) *TestAPI {461 ta.t.Helper()462 req, err := newXMLRequest(http.MethodDelete, target, body, headersQueryParams...)463 if err != nil {464 ta.t.Fatal(err)465 }466 return ta.Request(req)467}468// CmpResponse tests the last request response status against469// expectedResponse. expectedResponse can be a *http.Response or more470// probably a [td.TestDeep] operator.471//472// ta := tdhttp.NewTestAPI(t, mux)473//474// ta.Get("/test").475// CmpResponse(td.Struct(476// &http.Response{Status: http.StatusOK}, td.StructFields{477// "Header": td.SuperMapOf(http.Header{"X-Test": {"pipo"}}),478// "ContentLength": td.Gt(10),479// }))480//481// Some tests can be hard to achieve using operators chaining. In this482// case, the [td.Code] operator can be used to take the full control483// over the extractions and comparisons to do:484//485// ta.Get("/test").486// CmpResponse(td.Code(func (assert, require *td.T, r *http.Response) {487// token, err := ParseToken(r.Header.Get("X-Token"))488// require.CmpNoError(err)489//490// baseURL,err := url.Parse(r.Header.Get("X-Base-URL"))491// require.CmpNoError(err)492//493// assert.Cmp(baseURL.Query().Get("id"), token.ID)494// }))495//496// It fails if no request has been sent yet.497func (ta *TestAPI) CmpResponse(expectedResponse any) *TestAPI {498 defer ta.t.AnchorsPersistTemporarily()()499 ta.t.Helper()500 if !ta.checkRequestSent() {501 ta.failed |= responseFailed502 return ta503 }504 if !ta.t.RootName("Response").505 Cmp(ta.response.Result(), expectedResponse, ta.name+"full response should match") {506 ta.failed |= responseFailed507 if ta.autoDumpResponse {508 ta.dumpResponse()509 }510 }511 return ta512}513// CmpStatus tests the last request response status against514// expectedStatus. expectedStatus can be an int to match a fixed HTTP515// status code, or a [td.TestDeep] operator.516//517// ta := tdhttp.NewTestAPI(t, mux)518//519// ta.Get("/test").520// CmpStatus(http.StatusOK)521//522// ta.PostJSON("/new", map[string]string{"name": "Bob"}).523// CmpStatus(td.Between(200, 202))524//525// It fails if no request has been sent yet.526func (ta *TestAPI) CmpStatus(expectedStatus any) *TestAPI {527 defer ta.t.AnchorsPersistTemporarily()()528 ta.t.Helper()529 if !ta.checkRequestSent() {530 ta.failed |= statusFailed531 return ta532 }533 if !ta.t.RootName("Response.Status").534 CmpLax(ta.response.Code, expectedStatus, ta.name+"status code should match") {535 ta.failed |= statusFailed536 if ta.autoDumpResponse {537 ta.dumpResponse()538 }539 }540 return ta541}542// CmpHeader tests the last request response header against543// expectedHeader. expectedHeader can be a [http.Header] or a544// [td.TestDeep] operator. Keep in mind that if it is a [http.Header],545// it has to match exactly the response header. Often only the546// presence of a header key is needed:547//548// ta := tdhttp.NewTestAPI(t, mux).549// PostJSON("/new", map[string]string{"name": "Bob"}).550// CmdStatus(201).551// CmpHeader(td.ContainsKey("X-Custom"))552//553// or some specific key, value pairs:554//555// ta.CmpHeader(td.SuperMapOf(556// http.Header{557// "X-Account": []string{"Bob"},558// },559// td.MapEntries{560// "X-Token": td.Bag(td.Re(`^[a-z0-9-]{32}\z`)),561// }),562// )563//564// Note that CmpHeader calls can be chained:565//566// ta.CmpHeader(td.ContainsKey("X-Account")).567// CmpHeader(td.ContainsKey("X-Token"))568//569// instead of doing all tests in one call as [td.All] operator allows it:570//571// ta.CmpHeader(td.All(572// td.ContainsKey("X-Account"),573// td.ContainsKey("X-Token"),574// ))575//576// It fails if no request has been sent yet.577func (ta *TestAPI) CmpHeader(expectedHeader any) *TestAPI {578 defer ta.t.AnchorsPersistTemporarily()()579 ta.t.Helper()580 if !ta.checkRequestSent() {581 ta.failed |= headerFailed582 return ta583 }584 if !ta.t.RootName("Response.Header").585 CmpLax(ta.response.Result().Header, expectedHeader, ta.name+"header should match") {586 ta.failed |= headerFailed587 if ta.autoDumpResponse {588 ta.dumpResponse()589 }590 }591 return ta592}593// CmpTrailer tests the last request response trailer against594// expectedTrailer. expectedTrailer can be a [http.Header] or a595// [td.TestDeep] operator. Keep in mind that if it is a [http.Header],596// it has to match exactly the response trailer. Often only the597// presence of a trailer key is needed:598//599// ta := tdhttp.NewTestAPI(t, mux).600// PostJSON("/new", map[string]string{"name": "Bob"}).601// CmdStatus(201).602// CmpTrailer(td.ContainsKey("X-Custom"))603//604// or some specific key, value pairs:605//606// ta.CmpTrailer(td.SuperMapOf(607// http.Header{608// "X-Account": []string{"Bob"},609// },610// td.MapEntries{611// "X-Token": td.Re(`^[a-z0-9-]{32}\z`),612// }),613// )614//615// Note that CmpTrailer calls can be chained:616//617// ta.CmpTrailer(td.ContainsKey("X-Account")).618// CmpTrailer(td.ContainsKey("X-Token"))619//620// instead of doing all tests in one call as [td.All] operator allows it:621//622// ta.CmpTrailer(td.All(623// td.ContainsKey("X-Account"),624// td.ContainsKey("X-Token"),625// ))626//627// It fails if no request has been sent yet.628//629// Note that until go1.19, it does not handle multiple values in630// a single Trailer header field.631func (ta *TestAPI) CmpTrailer(expectedTrailer any) *TestAPI {632 defer ta.t.AnchorsPersistTemporarily()()633 ta.t.Helper()634 if !ta.checkRequestSent() {635 ta.failed |= trailerFailed636 return ta637 }638 if !ta.t.RootName("Response.Trailer").639 CmpLax(ta.response.Result().Trailer, expectedTrailer, ta.name+"trailer should match") {640 ta.failed |= trailerFailed641 if ta.autoDumpResponse {642 ta.dumpResponse()643 }644 }645 return ta646}647// CmpCookies tests the last request response cookies against648// expectedCookies. expectedCookies can be a [][*http.Cookie] or a649// [td.TestDeep] operator. Keep in mind that if it is a650// [][*http.Cookie], it has to match exactly the response651// cookies. Often only the presence of a cookie key is needed:652//653// ta := tdhttp.NewTestAPI(t, mux).654// PostJSON("/login", map[string]string{"name": "Bob", "password": "Sponge"}).655// CmdStatus(200).656// CmpCookies(td.SuperBagOf(td.Struct(&http.Cookie{Name: "cookie_session"}, nil))).657// CmpCookies(td.SuperBagOf(td.Smuggle("Name", "cookie_session"))) // shorter658//659// To make tests easier, [http.Cookie.Raw] and [http.Cookie.RawExpires] fields660// of each [*http.Cookie] are zeroed before doing the comparison. So no need661// to fill them when comparing against a simple literal as in:662//663// ta := tdhttp.NewTestAPI(t, mux).664// PostJSON("/login", map[string]string{"name": "Bob", "password": "Sponge"}).665// CmdStatus(200).666// CmpCookies([]*http.Cookies{667// {Name: "cookieName1", Value: "cookieValue1"},668// {Name: "cookieName2", Value: "cookieValue2"},669// })670//671// It fails if no request has been sent yet.672func (ta *TestAPI) CmpCookies(expectedCookies any) *TestAPI {673 defer ta.t.AnchorsPersistTemporarily()()674 ta.t.Helper()675 if !ta.checkRequestSent() {676 ta.failed |= cookiesFailed677 return ta678 }679 // Empty Raw* fields to make comparisons easier680 cookies := ta.response.Result().Cookies()681 for _, c := range cookies {682 c.RawExpires, c.Raw = "", ""683 }684 if !ta.t.RootName("Response.Cookie").685 CmpLax(cookies, expectedCookies, ta.name+"cookies should match") {686 ta.failed |= cookiesFailed687 if ta.autoDumpResponse {688 ta.dumpResponse()689 }690 }691 return ta692}693// findCmpXBodyCaller finds the oldest Cmp* method called.694func findCmpXBodyCaller() string {695 var (696 fn string697 pc [20]uintptr698 found bool699 )700 if num := runtime.Callers(5, pc[:]); num > 0 {701 frames := runtime.CallersFrames(pc[:num])702 for {703 frame, more := frames.Next()704 if pos := strings.Index(frame.Function, "tdhttp.(*TestAPI).Cmp"); pos > 0 {705 fn = frame.Function[pos+18:]706 found = true707 } else if found {708 more = false709 }710 if !more {711 break712 }713 }714 }715 return fn716}717func (ta *TestAPI) cmpMarshaledBody(718 acceptEmptyBody bool,719 unmarshal func([]byte, any) error,720 expectedBody any,721) *TestAPI {722 defer ta.t.AnchorsPersistTemporarily()()723 ta.t.Helper()724 if !ta.checkRequestSent() {725 ta.failed |= bodyFailed726 return ta727 }728 if !acceptEmptyBody &&729 !ta.t.RootName("Response body").Code(ta.response.Body.Bytes(),730 func(b []byte) error {731 if len(b) > 0 {732 return nil733 }734 return &ctxerr.Error{735 Message: "%% is empty!",736 Summary: ctxerr.NewSummary(737 "Body cannot be empty when using " + findCmpXBodyCaller()),738 }739 },740 ta.name+"body should not be empty") {741 ta.failed |= bodyFailed742 if ta.autoDumpResponse {743 ta.dumpResponse()744 }745 return ta746 }747 tt := ta.t.RootName("Response.Body")748 var bodyType reflect.Type749 // If expectedBody is a TestDeep operator, try to ask it the type750 // behind it. It should work in most cases (typically Struct(),751 // Map() & Slice()).752 var unknownExpectedType, showRawBody bool753 op, ok := expectedBody.(td.TestDeep)754 if ok {755 bodyType = op.TypeBehind()756 if bodyType == nil {757 // As the expected body type cannot be guessed, try to758 // unmarshal in an any759 bodyType = types.Interface760 unknownExpectedType = true761 // Special case for Ignore & NotEmpty operators762 switch op.GetLocation().Func {763 case "Ignore", "NotEmpty":764 showRawBody = (ta.failed & statusFailed) != 0 // Show real body if status failed765 }766 }767 } else {768 bodyType = reflect.TypeOf(expectedBody)769 if bodyType == nil {770 bodyType = types.Interface771 }772 }773 // For unmarshaling below, body must be a pointer774 bodyPtr := reflect.New(bodyType)775 // Try to unmarshal body776 if !tt.RootName("unmarshal(Response.Body)").777 CmpNoError(unmarshal(ta.response.Body.Bytes(), bodyPtr.Interface()), ta.name+"body unmarshaling") {778 // If unmarshal failed, perhaps it's coz the expected body type779 // is unknown?780 if unknownExpectedType {781 tt.Logf("Cannot guess the body expected type as %[1]s TestDeep\n"+782 "operator does not know the type behind it.\n"+783 "You can try All(Isa(EXPECTED_TYPE), %[1]s(…)) to disambiguate…",784 op.GetLocation().Func)785 }786 showRawBody = true // let's show its real body contents787 ta.failed |= bodyFailed788 } else if !tt.Cmp(bodyPtr.Elem().Interface(), expectedBody, ta.name+"body contents is OK") {789 // Try to catch bad body expected type when nothing has been set790 // to non-zero during unmarshaling body. In this case, require791 // to show raw body contents.792 if len(ta.response.Body.Bytes()) > 0 &&793 td.EqDeeply(bodyPtr.Interface(), reflect.New(bodyType).Interface()) {794 showRawBody = true795 tt.Log("Hmm… It seems nothing has been set during unmarshaling…")796 }797 ta.failed |= bodyFailed798 }799 if showRawBody || ((ta.failed&bodyFailed) != 0 && ta.autoDumpResponse) {800 ta.dumpResponse()801 }802 return ta803}804// CmpMarshaledBody tests that the last request response body can be805// unmarshaled using unmarshal function and then, that it matches806// expectedBody. expectedBody can be any type unmarshal function can807// handle, or a [td.TestDeep] operator.808//809// See [TestAPI.CmpJSONBody] and [TestAPI.CmpXMLBody] sources for810// examples of use.811//812// It fails if no request has been sent yet.813func (ta *TestAPI) CmpMarshaledBody(unmarshal func([]byte, any) error, expectedBody any) *TestAPI {814 ta.t.Helper()815 return ta.cmpMarshaledBody(false, unmarshal, expectedBody)816}817// CmpBody tests the last request response body against818// expectedBody. expectedBody can be a []byte, a string or a819// [td.TestDeep] operator.820//821// ta := tdhttp.NewTestAPI(t, mux)822//823// ta.Get("/test").824// CmpStatus(http.StatusOK).825// CmpBody("OK!\n")826//827// ta.Get("/test").828// CmpStatus(http.StatusOK).829// CmpBody(td.Contains("OK"))830//831// It fails if no request has been sent yet.832func (ta *TestAPI) CmpBody(expectedBody any) *TestAPI {833 ta.t.Helper()834 if expectedBody == nil {835 return ta.NoBody()836 }837 return ta.cmpMarshaledBody(838 true, // accept empty body839 func(body []byte, target any) error {840 switch target := target.(type) {841 case *string:842 *target = string(body)843 case *[]byte:844 *target = body845 case *any:846 *target = body847 default:848 // cmpMarshaledBody always calls us with target as a pointer849 return fmt.Errorf(850 "CmpBody only accepts expectedBody be a []byte, a string or a TestDeep operator allowing to match these types, but not type %s",851 reflect.TypeOf(target).Elem())852 }853 return nil854 },855 expectedBody)856}857// CmpJSONBody tests that the last request response body can be858// [json.Unmarshal]'ed and that it matches expectedBody. expectedBody859// can be any type one can [json.Unmarshal] into, or a [td.TestDeep]860// operator.861//862// ta := tdhttp.NewTestAPI(t, mux)863//864// ta.Get("/person/42").865// CmpStatus(http.StatusOK).866// CmpJSONBody(Person{867// ID: 42,868// Name: "Bob",869// Age: 26,870// })871//872// ta.PostJSON("/person", Person{Name: "Bob", Age: 23}).873// CmpStatus(http.StatusCreated).874// CmpJSONBody(td.SStruct(875// Person{876// Name: "Bob",877// Age: 26,878// },879// td.StructFields{880// "ID": td.NotZero(),881// }))882//883// The same with anchoring, and so without [td.SStruct]:884//885// ta := tdhttp.NewTestAPI(tt, mux)886//887// ta.PostJSON("/person", Person{Name: "Bob", Age: 23}).888// CmpStatus(http.StatusCreated).889// CmpJSONBody(Person{890// ID: ta.Anchor(td.NotZero(), uint64(0)).(uint64),891// Name: "Bob",892// Age: 26,893// })894//895// The same using [td.JSON]:896//897// ta.PostJSON("/person", Person{Name: "Bob", Age: 23}).898// CmpStatus(http.StatusCreated).899// CmpJSONBody(td.JSON(`900// {901// "id": NotZero(),902// "name": "Bob",903// "age": 26904// }`))905//906// It fails if no request has been sent yet.907func (ta *TestAPI) CmpJSONBody(expectedBody any) *TestAPI {908 ta.t.Helper()909 return ta.CmpMarshaledBody(json.Unmarshal, expectedBody)910}911// CmpXMLBody tests that the last request response body can be912// [xml.Unmarshal]'ed and that it matches expectedBody. expectedBody913// can be any type one can [xml.Unmarshal] into, or a [td.TestDeep]914// operator.915//916// ta := tdhttp.NewTestAPI(t, mux)917//918// ta.Get("/person/42").919// CmpStatus(http.StatusOK).920// CmpXMLBody(Person{921// ID: 42,922// Name: "Bob",923// Age: 26,924// })925//926// ta.Get("/person/43").927// CmpStatus(http.StatusOK).928// CmpXMLBody(td.SStruct(929// Person{930// Name: "Bob",931// Age: 26,932// },933// td.StructFields{934// "ID": td.NotZero(),935// }))936//937// The same with anchoring:938//939// ta := tdhttp.NewTestAPI(tt, mux)940//941// ta.Get("/person/42").942// CmpStatus(http.StatusOK).943// CmpXMLBody(Person{944// ID: ta.Anchor(td.NotZero(), uint64(0)).(uint64),945// Name: "Bob",946// Age: 26,947// })948//949// It fails if no request has been sent yet.950func (ta *TestAPI) CmpXMLBody(expectedBody any) *TestAPI {951 ta.t.Helper()952 return ta.CmpMarshaledBody(xml.Unmarshal, expectedBody)953}954// NoBody tests that the last request response body is empty.955//956// It fails if no request has been sent yet.957func (ta *TestAPI) NoBody() *TestAPI {958 defer ta.t.AnchorsPersistTemporarily()()959 ta.t.Helper()960 if !ta.checkRequestSent() {961 ta.failed |= bodyFailed962 return ta963 }964 ok := ta.t.RootName("Response.Body").965 Code(len(ta.response.Body.Bytes()) == 0,966 func(empty bool) error {967 if empty {968 return nil969 }970 return &ctxerr.Error{971 Message: "%% is not empty",972 Got: types.RawString("not empty"),973 Expected: types.RawString("empty"),974 }975 },976 "body should be empty")977 if !ok {978 ta.failed |= bodyFailed979 // Systematically dump response, no AutoDumpResponse needed980 ta.dumpResponse()981 }982 return ta983}984// Or executes function fn if ta.Failed() is true at the moment it is called.985//986// fn can have several types:987// - func(body string) or func(t *td.T, body string)988// → fn is called with response body as a string.989// If no response has been received yet, body is "";990// - func(body []byte) or func(t *td.T, body []byte)991// → fn is called with response body as a []byte.992// If no response has been received yet, body is nil;993// - func(t *td.T, resp *httptest.ResponseRecorder)994// → fn is called with the internal object containing the response.995// See net/http/httptest for details.996// If no response has been received yet, resp is nil.997//998// If fn type is not one of these types, it calls ta.T().Fatal().999func (ta *TestAPI) Or(fn any) *TestAPI {1000 ta.t.Helper()1001 switch fn := fn.(type) {1002 case func(string):1003 if ta.Failed() {1004 var body string1005 if ta.response != nil && ta.response.Body != nil {1006 body = ta.response.Body.String()1007 }1008 fn(body)1009 }1010 case func(*td.T, string):1011 if ta.Failed() {1012 var body string1013 if ta.response != nil && ta.response.Body != nil {1014 body = ta.response.Body.String()1015 }1016 fn(ta.t, body)1017 }1018 case func([]byte):1019 if ta.Failed() {1020 var body []byte1021 if ta.response != nil && ta.response.Body != nil {1022 body = ta.response.Body.Bytes()1023 }1024 fn(body)1025 }1026 case func(*td.T, []byte):1027 if ta.Failed() {1028 var body []byte1029 if ta.response != nil && ta.response.Body != nil {1030 body = ta.response.Body.Bytes()1031 }1032 fn(ta.t, body)1033 }1034 case func(*td.T, *httptest.ResponseRecorder):1035 if ta.Failed() {1036 fn(ta.t, ta.response)1037 }1038 default:1039 ta.t.Fatal(color.BadUsage(1040 "Or(func([*td.T,]string) | func([*td.T,][]byte) | func(*td.T,*httptest.ResponseRecorder))",1041 fn, 1, true))1042 }1043 return ta1044}1045// OrDumpResponse dumps the response if at least one previous test failed.1046//1047// ta := tdhttp.NewTestAPI(t, handler)1048//1049// ta.Get("/foo").1050// CmpStatus(200).1051// OrDumpResponse(). // if status check failed, dumps the response1052// CmpBody("bar") // if it fails, the response is not dumped1053//1054// ta.Get("/foo").1055// CmpStatus(200).1056// CmpBody("bar").1057// OrDumpResponse() // dumps the response if status and/or body checks fail1058//1059// See [TestAPI.AutoDumpResponse] method to automatize this dump.1060func (ta *TestAPI) OrDumpResponse() *TestAPI {1061 if ta.Failed() {1062 ta.dumpResponse()1063 }1064 return ta1065}1066func (ta *TestAPI) dumpResponse() {1067 if ta.responseDumped {1068 return1069 }1070 ta.t.Helper()1071 if ta.response != nil {1072 ta.responseDumped = true1073 internal.DumpResponse(ta.t, ta.response.Result())...

Full Screen

Full Screen

AutoDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func TestAutoDumpResponse(t *testing.T) {3 tdh := tdhttp.New()4 w := httptest.NewRecorder()5 r := httptest.NewRequest("GET", "/hello", nil)6 h := func(w http.ResponseWriter, r *http.Request) {7 fmt.Fprintf(w, "Hello, %s", r.URL.Path[1:])8 }9 tdh.AutoDumpResponse(w, r, h)10}11import (

Full Screen

Full Screen

AutoDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := tdhttp.New()4 td.AutoDumpResponse()5}6import (7func main() {8 td := tdhttp.New()9 td.AutoDumpResponse()10}11import (12func main() {13 td := tdhttp.New()14 td.AutoDumpResponse()15}16import (17func main() {18 td := tdhttp.New()19 td.AutoDumpResponse()20}21import (22func main() {23 td := tdhttp.New()24 td.AutoDumpResponse()

Full Screen

Full Screen

AutoDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tdhttp := tdhttp.New()4 tdhttp.SetMethod("GET")5 tdhttp.AutoDumpResponse()6 fmt.Println(tdhttp.GetResponseBody())7}

Full Screen

Full Screen

AutoDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := tdhttp.New()4 if err != nil {5 fmt.Println(err)6 }7 resp.AutoDumpResponse()8}9import (10func main() {11 client := tdhttp.New()12 if err != nil {13 fmt.Println(err)14 }15 resp.AutoDumpResponse()16}17import (18func main() {19 client := tdhttp.New()20 if err != nil {21 fmt.Println(err)22 }23 resp.AutoDumpResponse()24}25import (26func main() {27 client := tdhttp.New()28 if err != nil {29 fmt.Println(err)30 }31 resp.AutoDumpResponse()32}33import (34func main() {

Full Screen

Full Screen

AutoDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := tdhttp.NewClient()4 if err != nil {5 fmt.Println(err)6 }7 resp.AutoDumpResponse()8}9import (10func main() {11 client := tdhttp.NewClient()12 if err != nil {13 fmt.Println(err)14 }15 resp.AutoDumpResponse()16}17import (18func main() {19 client := tdhttp.NewClient()20 if err != nil {21 fmt.Println(err)22 }23 resp.AutoDumpResponse()24}25import (26func main() {27 client := tdhttp.NewClient()

Full Screen

Full Screen

AutoDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tdhttp := new(tdhttp.TDHTTP)4 tdhttp.Headers = map[string]string{5 }6 tdhttp.AutoDumpResponse()7 fmt.Println(tdhttp.Response)8}9import (10func main() {11 tdhttp := new(tdhttp.TDHTTP)12 tdhttp.Headers = map[string]string{13 }14 tdhttp.AutoDumpResponse()15 fmt.Println(tdhttp.Response)16}17import (18func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful