Best Go-testdeep code snippet using td.CmpContains
test_api_test.go
Source:test_api_test.go
...520 Expires: time.Date(2021, time.August, 12, 11, 22, 33, 0, time.UTC),521 },522 }).523 Failed())524 td.CmpContains(t, mockT.LogBuf(),525 "Failed test 'cookies should match'")526 td.CmpContains(t, mockT.LogBuf(),527 "Response.Cookie: comparing slices, from index #1")528 // 2 cookies are here whatever their order is using Bag529 mockT = tdutil.NewT("test")530 td.CmpFalse(t,531 tdhttp.NewTestAPI(mockT, mux).532 Get("/any/cookies").533 CmpCookies(td.Bag(534 td.Smuggle("Name", "second"),535 td.Smuggle("Name", "first"),536 )).537 Failed())538 td.CmpEmpty(t, mockT.LogBuf())539 // Testing only Name & Value whatever their order is using Bag540 mockT = tdutil.NewT("test")541 td.CmpFalse(t,542 tdhttp.NewTestAPI(mockT, mux).543 Get("/any/cookies").544 CmpCookies(td.Bag(545 td.Struct(&http.Cookie{Name: "first", Value: "cookie1"}, nil),546 td.Struct(&http.Cookie{Name: "second", Value: "cookie2"}, nil),547 )).548 Failed())549 td.CmpEmpty(t, mockT.LogBuf())550 // Testing the presence of only one using SuperBagOf551 mockT = tdutil.NewT("test")552 td.CmpFalse(t,553 tdhttp.NewTestAPI(mockT, mux).554 Get("/any/cookies").555 CmpCookies(td.SuperBagOf(556 td.Struct(&http.Cookie{Name: "first", Value: "cookie1"}, nil),557 )).558 Failed())559 td.CmpEmpty(t, mockT.LogBuf())560 // Testing only the number of cookies561 mockT = tdutil.NewT("test")562 td.CmpFalse(t,563 tdhttp.NewTestAPI(mockT, mux).564 Get("/any/cookies").565 CmpCookies(td.Len(2)).566 Failed())567 td.CmpEmpty(t, mockT.LogBuf())568 // Error followed by a success: Failed() should return true anyway569 mockT = tdutil.NewT("test")570 td.CmpTrue(t,571 tdhttp.NewTestAPI(mockT, mux).572 Get("/any").573 CmpCookies(td.Len(100)). // fails574 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")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 },1029 } {1030 orCalled = false1031 // As CmpStatus succeeds, Or function is not called1032 td.CmpFalse(t,1033 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1034 Head("/any").1035 CmpStatus(200).1036 Or(fn).1037 Failed(),1038 "Not failed #%d", i)1039 td.CmpFalse(t, orCalled, "called #%d", i)1040 }1041 })1042 t.Run("No request sent", func(t *testing.T) {1043 var ok, orCalled bool1044 for i, fn := range []any{1045 func(body string) { orCalled = true; ok = body == "" },1046 func(t *td.T, body string) { orCalled = true; ok = t != nil && body == "" },1047 func(body []byte) { orCalled = true; ok = body == nil },1048 func(t *td.T, body []byte) { orCalled = true; ok = t != nil && body == nil },1049 func(t *td.T, r *httptest.ResponseRecorder) { orCalled = true; ok = t != nil && r == nil },1050 } {1051 orCalled, ok = false, false1052 // Check status without sending a request â fail1053 td.CmpTrue(t,1054 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1055 CmpStatus(123).1056 Or(fn).1057 Failed(),1058 "Failed #%d", i)1059 td.CmpTrue(t, orCalled, "called #%d", i)1060 td.CmpTrue(t, ok, "OK #%d", i)1061 }1062 })1063 t.Run("Empty bodies", func(t *testing.T) {1064 var ok, orCalled bool1065 for i, fn := range []any{1066 func(body string) { orCalled = true; ok = body == "" },1067 func(t *td.T, body string) { orCalled = true; ok = t != nil && body == "" },1068 func(body []byte) { orCalled = true; ok = body == nil },1069 func(t *td.T, body []byte) { orCalled = true; ok = t != nil && body == nil },1070 func(t *td.T, r *httptest.ResponseRecorder) {1071 orCalled = true1072 ok = t != nil && r != nil && r.Body.Len() == 01073 },1074 } {1075 orCalled, ok = false, false1076 // HEAD /any = no body + CmpStatus fails1077 td.CmpTrue(t,1078 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1079 Head("/any").1080 CmpStatus(123).1081 Or(fn).1082 Failed(),1083 "Failed #%d", i)1084 td.CmpTrue(t, orCalled, "called #%d", i)1085 td.CmpTrue(t, ok, "OK #%d", i)1086 }1087 })1088 t.Run("Body", func(t *testing.T) {1089 var ok, orCalled bool1090 for i, fn := range []any{1091 func(body string) { orCalled = true; ok = body == "GET!" },1092 func(t *td.T, body string) { orCalled = true; ok = t != nil && body == "GET!" },1093 func(body []byte) { orCalled = true; ok = string(body) == "GET!" },1094 func(t *td.T, body []byte) { orCalled = true; ok = t != nil && string(body) == "GET!" },1095 func(t *td.T, r *httptest.ResponseRecorder) {1096 orCalled = true1097 ok = t != nil && r != nil && r.Body.String() == "GET!"1098 },1099 } {1100 orCalled, ok = false, false1101 // GET /any = "GET!" body + CmpStatus fails1102 td.CmpTrue(t,1103 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1104 Get("/any").1105 CmpStatus(123).1106 Or(fn).1107 Failed(),1108 "Failed #%d", i)1109 td.CmpTrue(t, orCalled, "called #%d", i)1110 td.CmpTrue(t, ok, "OK #%d", i)1111 }1112 })1113 tt := tdutil.NewT("test")1114 ta := tdhttp.NewTestAPI(tt, mux)1115 if td.CmpTrue(t, tt.CatchFailNow(func() { ta.Or(123) })) {1116 td.CmpContains(t, tt.LogBuf(),1117 "usage: Or(func([*td.T,]string) | func([*td.T,][]byte) | func(*td.T,*httptest.ResponseRecorder)), but received int as 1st parameter")1118 }1119}1120func TestRun(t *testing.T) {1121 mux := server()1122 ta := tdhttp.NewTestAPI(tdutil.NewT("test"), mux)1123 ok := ta.Run("Test", func(ta *tdhttp.TestAPI) {1124 td.CmpFalse(t, ta.Get("/any").CmpStatus(200).Failed())1125 })1126 td.CmpTrue(t, ok)1127 ok = ta.Run("Test", func(ta *tdhttp.TestAPI) {1128 td.CmpTrue(t, ta.Get("/any").CmpStatus(123).Failed())1129 })1130 td.CmpFalse(t, ok)...
list_test.go
Source:list_test.go
...100 cl := client.New(WithBaseURL(server.URL))101 got, err := ListHosts(cl)102 if tt.want.err != "" {103 assert.NotNil(t, err)104 td.CmpContains(t, err, tt.want.err)105 return106 }107 assert.Nil(t, err)108 td.Cmp(t, got, tt.want.hosts)109 })110 }111}112func TestListHostsPaginator(t *testing.T) {113 type MockData struct {114 status int115 body string116 }117 type Fields struct {118 data []MockData119 }120 type Want struct {121 hosts Hosts122 err string123 }124 tests := []struct {125 name string126 fields Fields127 want Want128 }{129 {130 name: "one",131 fields: Fields{132 data: []MockData{133 {134 status: http.StatusOK,135 body: `136 {137 "data": [138 {139 "id": "abc-def",140 "type": "rule"141 }142 ]143 }144 `,145 },146 },147 },148 want: Want{149 hosts: Hosts{150 Data: []Data{151 {152 ID: "abc-def",153 Type: "rule",154 },155 },156 },157 },158 },159 {160 name: "many",161 fields: Fields{162 data: []MockData{163 {164 status: http.StatusOK,165 body: `166 {167 "data": [168 {169 "id": "abc-def",170 "type": "host"171 }172 ],173 "meta": {174 "has_more": true175 },176 "links": {177 "next": "/v1/hosts?starting_after=abc-def"178 }179 }180 `,181 },182 {183 status: http.StatusOK,184 body: `185 {186 "data": [187 {188 "id": "bcd-efg",189 "type": "host"190 }191 ]192 }193 `,194 },195 },196 },197 want: Want{198 hosts: Hosts{199 Data: []Data{200 {201 ID: "abc-def",202 Type: "host",203 }, {204 ID: "bcd-efg",205 Type: "host",206 },207 },208 },209 },210 },211 {212 name: "none",213 want: Want{214 hosts: Hosts{215 Data: []Data{},216 },217 },218 },219 {220 name: "invalid_page",221 fields: Fields{222 data: []MockData{223 {224 status: http.StatusOK,225 body: `226 {227 "data": [228 {229 "id": "abc-def",230 "type": "host"231 }232 ],233 "meta": {234 "has_more": true235 },236 "links": {237 "next": "/v1/hosts?starting_after=abc-def"238 }239 }240 `,241 },242 {243 status: http.StatusOK,244 body: `{ notjson }`,245 },246 },247 },248 want: Want{249 hosts: Hosts{250 Data: []Data{251 {252 ID: "abc-def",253 Type: "host",254 },255 },256 },257 err: "unable to get a hosts page",258 },259 },260 }261 for _, tt := range tests {262 t.Run(tt.name, func(t *testing.T) {263 page := func() func() int {264 i := 0265 return func() int {266 i++267 return i - 1268 }269 }()270 mux := http.NewServeMux()271 mux.HandleFunc("/hosts/", func(w http.ResponseWriter, req *http.Request) {272 if len(tt.fields.data) < 1 {273 w.WriteHeader(http.StatusOK)274 w.Write([]byte("{}"))275 return276 }277 data := tt.fields.data[page()]278 w.WriteHeader(data.status)279 w.Write([]byte(data.body))280 })281 server := httptest.NewServer(mux)282 defer server.Close()283 cl := client.New(WithBaseURL(server.URL))284 got, err := ListHostsPaginator(cl)285 if tt.want.err != "" {286 assert.NotNil(t, err)287 td.CmpContains(t, err, tt.want.err)288 return289 }290 assert.Nil(t, err)291 td.Cmp(t, got, tt.want.hosts)292 })293 }294}295func TestBuildListHosts(t *testing.T) {296 type Args struct {297 options *option.Options298 }299 type Want struct {300 pathQuery string301 }...
apply_test.go
Source:apply_test.go
...24 result, err = resolveObject(v, h, nil, "")25 if !td.CmpError(t, err) {26 return27 }28 td.CmpContains(t, err.Error(), "can't evaluate field notexisting in type interface")29}30func TestApplyNested(t *testing.T) {31 assert, require := td.AssertRequire(t)32 v := values.NewValues()33 v.SetInput(map[string]interface{}{34 "ifoo1": "Hello",35 "ifoo2": true,36 "ifoo3": 12,37 "ifoo4": []string{"Hello", "you!"},38 })39 h := json.RawMessage(`{"fooo":{"foo": "{{ .input.ifoo1}} -> {{.input.ifoo3 }}","array":["{{.input.ifoo2}}"]}}`)40 result, err := resolveObject(v, h, nil, "")41 require.CmpNoError(err)42 expected := `{"fooo":{"array":["true"],"foo":"Hello -> 12"}}`...
CmpContains
Using AI Code Generation
1import (2func main() {3 fmt.Println(td.CmpContains(1, 1, 2, 3))4 fmt.Println(td.CmpContains(1, 1, 2, 3).String())5 fmt.Println(td.CmpContains(1, 1, 2, 3).Got())6 fmt.Println(td.CmpContains(1, 1, 2, 3).Expected())7 fmt.Println(td.CmpContains(1, 1, 2, 3).Location())8 fmt.Println(td.CmpContains(1, 1, 2, 3).Error())9}
CmpContains
Using AI Code Generation
1import (2func main() {3 var a = []int{1, 2, 3}4 var b = []int{1, 2, 4}5 fmt.Println(td.CmpContains(a, b))6}7import (8func main() {9 var a = []int{1, 2, 3}10 var b = []int{1, 2, 4}11 fmt.Println(td.CmpContainsAny(a, b))12}13import (14func main() {15 var a = []int{1, 2, 3}16 var b = []int{1, 2, 4}17 fmt.Println(td.CmpContainsNone(a, b))18}19import (
CmpContains
Using AI Code Generation
1import (2func main() {3 fmt.Println(td.CmpContains("Hello world!", "Hello"))4 fmt.Println(td.CmpContains("Hello world!", "Hello", "world"))5 fmt.Println(td.CmpContains("Hello world!", "Hello", "world", "foo"))6}7import (8func main() {9 fmt.Println(td.CmpContains("Hello world!", "Hello"))10 fmt.Println(td.CmpContains("Hello world!", "Hello", "world"))11 fmt.Println(td.CmpContains("Hello world!", "Hello", "world", "foo"))12}13import (14func main() {15 fmt.Println(td.CmpContains("Hello world!", "Hello"))16 fmt.Println(td.CmpContains("Hello world!", "Hello", "world"))17 fmt.Println(td.CmpContains("Hello world!", "Hello", "world", "foo"))18}19import (20func main() {21 fmt.Println(td.CmpContains("Hello world!", "Hello"))22 fmt.Println(td.CmpContains("Hello world!", "Hello", "world"))23 fmt.Println(td.CmpContains("Hello world!", "Hello", "world", "foo"))24}25import (26func main() {27 fmt.Println(td.CmpContains("Hello world!",
CmpContains
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(td.CmpContains(s, "Hello"))4 fmt.Println(td.CmpContains(s, "World"))5 fmt.Println(td.CmpContains(s, "Hello World"))6 fmt.Println(td.CmpContains(s, "Hello World!"))7 fmt.Println(td.CmpContains(s, "Hello", "World", "Hello World"))8}
CmpContains
Using AI Code Generation
1import (2func main() {3 td.CmpContains(t, []int{1, 2, 3}, 2)4}5import (6func TestCmpContains(t *testing.T) {7 td.CmpContains(t, []int{1, 2, 3}, 2)8}9import (10func TestCmpContains(t *testing.T) {11 td.CmpContains(t, []int{1, 2, 3}, 2, td.Bag(1, 2, 3))12}
CmpContains
Using AI Code Generation
1import (2func main() {3 a := []int{1, 2, 3}4 b := []int{1, 2, 3, 4}5 c := []int{1, 2, 3}6 is := is.New(t)7}8--- FAIL: TestCmpContains (0.00s)9 2.go:17: []int{1, 2, 3} is not a subset of []int{1, 2, 3, 4}10func (i *I) CmpNotContains(a, b interface{}, msgAndArgs ...interface{}) bool11--- PASS: TestCmpNotContains (0.00s)12import (13func main() {14 a := []int{1, 2, 3}15 b := []int{1, 2, 3, 4}16 c := []int{1, 2, 3}17 is := is.New(t)18 is.CmpNotContains(a
CmpContains
Using AI Code Generation
1import (2func TestCmpContains(t *testing.T) {3 a := assert.New(t)4 a.Equal(1, 1)5 a.Equal(1, 2)6 a.Equal(1, 3)7 a.Equal(1, 4)8 a.Equal(1, 5)9 a.Equal(1, 6)10 a.Equal(1, 7)11 a.Equal(1, 8)12 a.Equal(1, 9)13 a.Equal(1, 10)14 a.Equal(1, 11)15 a.Equal(1, 12)16 a.Equal(1, 13)17 a.Equal(1, 14)18 a.Equal(1, 15)19 a.Equal(1, 16)20 a.Equal(1, 17)21 a.Equal(1, 18)22 a.Equal(1, 19)23 a.Equal(1, 20)24 a.Equal(1, 21)25 a.Equal(1, 22)26 a.Equal(1, 23)27 a.Equal(1, 24)28 a.Equal(1, 25)29 a.Equal(1, 26)30 a.Equal(1, 27)31 a.Equal(1, 28)32 a.Equal(1, 29)33 a.Equal(1, 30)34 a.Equal(1, 31)35 a.Equal(1, 32)36 a.Equal(1, 33)37 a.Equal(1, 34)38 a.Equal(1, 35)39 a.Equal(1, 36)40 a.Equal(1, 37)41 a.Equal(1, 38)42 a.Equal(1, 39)43 a.Equal(1, 40)44 a.Equal(1, 41)45 a.Equal(1, 42)46 a.Equal(1, 43)47 a.Equal(1, 44)48 a.Equal(1, 45)49 a.Equal(1, 46)50 a.Equal(1, 47)51 a.Equal(1, 48)52 a.Equal(1, 49)53 a.Equal(1, 50)54 a.Equal(1, 51)55 a.Equal(1, 52)56 a.Equal(1, 53)57 a.Equal(1, 54)58 a.Equal(1, 55)59 a.Equal(1, 56)60 a.Equal(1, 57)61 a.Equal(1, 58)62 a.Equal(1
CmpContains
Using AI Code Generation
1import "fmt"2import "github.com/joeybloggs/go-triad"3func main() {4 td := triad.NewTriad()5 td.Add("a", "b", "c")6 td.Add("a", "b", "d")7 td.Add("a", "b", "e")8 td.Add("a", "b", "f")9 td.Add("a", "c", "d")10 fmt.Println(td.CmpContains("a", "b", "c"))11 fmt.Println(td.CmpContains("a", "b", "g"))12}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!