How to use OrDumpResponse method of tdhttp Package

Best Go-testdeep code snippet using tdhttp.OrDumpResponse

test_api_test.go

Source:test_api_test.go Github

copy

Full Screen

...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")962 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")963 })964 t.Run("Request error", func(t *testing.T) {965 var ta *tdhttp.TestAPI966 checkFatal := func(fn func()) {967 mockT := tdutil.NewT("test")968 td.CmpTrue(t, mockT.CatchFailNow(func() {969 ta = tdhttp.NewTestAPI(mockT, mux)970 fn()971 }))972 td.Cmp(t,973 mockT.LogBuf(),974 td.Contains("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool"),975 )976 }977 empty := strings.NewReader("")978 checkFatal(func() { ta.Get("/path", true) })979 checkFatal(func() { ta.Head("/path", true) })980 checkFatal(func() { ta.Options("/path", empty, true) })981 checkFatal(func() { ta.Post("/path", empty, true) })982 checkFatal(func() { ta.PostForm("/path", nil, true) })983 checkFatal(func() { ta.PostMultipartFormData("/path", &tdhttp.MultipartBody{}, true) })984 checkFatal(func() { ta.Put("/path", empty, true) })985 checkFatal(func() { ta.Patch("/path", empty, true) })986 checkFatal(func() { ta.Delete("/path", empty, true) })987 checkFatal(func() { ta.NewJSONRequest("ZIP", "/path", nil, true) })988 checkFatal(func() { ta.PostJSON("/path", nil, true) })989 checkFatal(func() { ta.PutJSON("/path", nil, true) })990 checkFatal(func() { ta.PatchJSON("/path", nil, true) })991 checkFatal(func() { ta.DeleteJSON("/path", nil, true) })992 checkFatal(func() { ta.NewXMLRequest("ZIP", "/path", nil, true) })993 checkFatal(func() { ta.PostXML("/path", nil, true) })994 checkFatal(func() { ta.PutXML("/path", nil, true) })995 checkFatal(func() { ta.PatchXML("/path", nil, true) })996 checkFatal(func() { ta.DeleteXML("/path", nil, true) })997 })998}999func TestWith(t *testing.T) {1000 mux := server()1001 ta := tdhttp.NewTestAPI(tdutil.NewT("test1"), mux)1002 td.CmpFalse(t, ta.Head("/any").CmpStatus(200).Failed())1003 nt := tdutil.NewT("test2")1004 nta := ta.With(nt)1005 td.Cmp(t, nta.T(), td.Not(td.Shallow(ta.T())))1006 td.CmpTrue(t, nta.CmpStatus(200).Failed()) // as no request sent yet1007 td.CmpContains(t, nt.LogBuf(),1008 "A request must be sent before testing status, header, body or full response")1009 td.CmpFalse(t, ta.CmpStatus(200).Failed()) // request already sent, so OK1010 nt = tdutil.NewT("test3")1011 nta = ta.With(nt)1012 td.CmpTrue(t, nta.Head("/any").1013 CmpStatus(400).1014 OrDumpResponse().1015 Failed())1016 td.CmpContains(t, nt.LogBuf(), "Response.Status: values differ")1017 td.CmpContains(t, nt.LogBuf(), "X-Testdeep-Method: HEAD") // Header dumped1018}1019func TestOr(t *testing.T) {1020 mux := server()1021 t.Run("Success", func(t *testing.T) {1022 var orCalled bool1023 for i, fn := range []any{1024 func(body string) { orCalled = true },1025 func(t *td.T, body string) { orCalled = true },1026 func(body []byte) { orCalled = true },1027 func(t *td.T, body []byte) { orCalled = true },1028 func(t *td.T, r *httptest.ResponseRecorder) { orCalled = true },...

Full Screen

Full Screen

test_api.go

Source:test_api.go Github

copy

Full Screen

...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())1074 return...

Full Screen

Full Screen

tdhttp.go

Source:tdhttp.go Github

copy

Full Screen

...40 }`,41 td.Catch(&id, td.NotZero()), // catch just created ID42 td.Gte(ta.SentAt()), // check that created_at is ≥ request sent date43 )).44 OrDumpResponse() // if some test fails, the response is dumped // HL45}...

Full Screen

Full Screen

OrDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 panic(err)5 }6 fmt.Println(resp)7}8import (9func main() {10 if err != nil {11 panic(err)12 }13 fmt.Println(resp)14}15import (16func main() {17 if err != nil {18 panic(err)19 }20 fmt.Println(resp)21}22import (23func main() {24 if err != nil {25 panic(err)26 }27 fmt.Println(resp)28}29import (30func main() {31 if err != nil {32 panic(err)33 }34 fmt.Println(resp)35}36import (37func main() {38 if err != nil {39 panic(err)40 }41 fmt.Println(resp)42}43import (44func main() {

Full Screen

Full Screen

OrDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 panic(err)5 }6 fmt.Println(resp.StatusCode)7}8import (9func main() {10 if err != nil {11 panic(err)12 }13 fmt.Println(resp.StatusCode)14}15import (16func main() {17 if err != nil {18 panic(err)19 }20 fmt.Println(resp.StatusCode)21}22import (23func main() {24 if err != nil {25 panic(err)26 }27 fmt.Println(resp.StatusCode)28}29import (30func main() {31 if err != nil {32 panic(err)33 }34 fmt.Println(resp.StatusCode)35}36import (37func main() {38 if err != nil {39 panic(err)40 }41 fmt.Println(resp.StatusCode)42}43import (44func main() {

Full Screen

Full Screen

OrDumpResponse

Using AI Code Generation

copy

Full Screen

1import "github.com/tdewolff/minify"2import "github.com/tdewolff/parse"3import "github.com/tdewolff/parse/vdom"4import "github.com/tdewolff/test"5import "github.com/tdewolff/test/tdhttp"6import "github.com/tdewolff/test/tdsuite"7import "github.com/tdewolff/test/tdtest"8import "github.com/tdewolff/test/tdvdom"9import "golang.org/x/net/html"10import "golang.org/x/net/html/atom"11import "io"12import "net/http"13import "net/http/httptest"14import "testing"15func TestRequest(t *testing.T) {16 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {17 w.Header().Set("Content-Type", "text/html; charset=utf-8")18 io.WriteString(w, `<!doctype html>19 }))20 defer ts.Close()21 req, err := http.NewRequest("GET", ts.URL, nil)22 if err != nil {23 t.Fatal(err)24 }25 res, err := http.DefaultClient.Do(req)26 if err != nil {27 t.Fatal(err)28 }29 defer res.Body.Close()30 suite := tdsuite.New(t)31 test := tdtest.New(suite, "TestRequest")32 td := tdhttp.New(test)33 td.OrDumpResponse(res)34 tdv := tdvdom.New(test)35 node, err := tdv.ParseResponse(res)36 if err != nil {37 t.Fatal(err)38 }39 m := minify.New()40 err = m.MinifyHTML(node)41 if err != nil {42 t.Fatal(err)

Full Screen

Full Screen

OrDumpResponse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6}7import (8func main() {9}10import (11func main() {12}13import (14func main() {15}16import (17func 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