How to use Put method of tdhttp Package

Best Go-testdeep code snippet using tdhttp.Put

test_api_test.go

Source:test_api_test.go Github

copy

Full Screen

...258 td.CmpEmpty(t, mockT.LogBuf())259 mockT = tdutil.NewT("test")260 td.CmpFalse(t,261 tdhttp.NewTestAPI(mockT, mux).262 Put("/any", strings.NewReader("PUT body")).263 CmpStatus(200).264 CmpHeader(containsKey).265 CmpBody("PUT!\n---\nPUT body").266 Failed())267 td.CmpEmpty(t, mockT.LogBuf())268 mockT = tdutil.NewT("test")269 td.CmpFalse(t,270 tdhttp.NewTestAPI(mockT, mux).271 Patch("/any", strings.NewReader("PATCH body")).272 CmpStatus(200).273 CmpHeader(containsKey).274 CmpBody("PATCH!\n---\nPATCH body").275 Failed())276 td.CmpEmpty(t, mockT.LogBuf())277 mockT = tdutil.NewT("test")278 td.CmpFalse(t,279 tdhttp.NewTestAPI(mockT, mux).280 Delete("/any", strings.NewReader("DELETE body")).281 CmpStatus(200).282 CmpHeader(containsKey).283 CmpBody("DELETE!\n---\nDELETE body").284 Failed())285 td.CmpEmpty(t, mockT.LogBuf())286 })287 t.Run("No JSON error", func(t *testing.T) {288 requestBody := map[string]any{"hey": 123}289 expectedBody := func(m string) td.TestDeep {290 return td.JSON(`{"method": $1, "body": {"hey": 123}}`, m)291 }292 mockT := tdutil.NewT("test")293 td.CmpFalse(t,294 tdhttp.NewTestAPI(mockT, mux).295 NewJSONRequest("GET", "/mirror/json", json.RawMessage(`null`)).296 CmpStatus(200).297 CmpHeader(containsKey).298 CmpJSONBody(nil).299 Failed())300 td.CmpEmpty(t, mockT.LogBuf())301 mockT = tdutil.NewT("test")302 td.CmpFalse(t,303 tdhttp.NewTestAPI(mockT, mux).304 NewJSONRequest("ZIP", "/any/json", requestBody).305 CmpStatus(200).306 CmpHeader(containsKey).307 CmpJSONBody(expectedBody("ZIP")).308 Failed())309 td.CmpEmpty(t, mockT.LogBuf())310 mockT = tdutil.NewT("test")311 td.CmpFalse(t,312 tdhttp.NewTestAPI(mockT, mux).313 NewJSONRequest("ZIP", "/any/json", requestBody).314 CmpStatus(200).315 CmpHeader(containsKey).316 CmpJSONBody(td.JSONPointer("/body/hey", 123)).317 Failed())318 td.CmpEmpty(t, mockT.LogBuf())319 mockT = tdutil.NewT("test")320 td.CmpFalse(t,321 tdhttp.NewTestAPI(mockT, mux).322 PostJSON("/any/json", requestBody).323 CmpStatus(200).324 CmpHeader(containsKey).325 CmpJSONBody(expectedBody("POST")).326 Failed())327 td.CmpEmpty(t, mockT.LogBuf())328 mockT = tdutil.NewT("test")329 td.CmpFalse(t,330 tdhttp.NewTestAPI(mockT, mux).331 PutJSON("/any/json", requestBody).332 CmpStatus(200).333 CmpHeader(containsKey).334 CmpJSONBody(expectedBody("PUT")).335 Failed())336 td.CmpEmpty(t, mockT.LogBuf())337 mockT = tdutil.NewT("test")338 td.CmpFalse(t,339 tdhttp.NewTestAPI(mockT, mux).340 PatchJSON("/any/json", requestBody).341 CmpStatus(200).342 CmpHeader(containsKey).343 CmpJSONBody(expectedBody("PATCH")).344 Failed())345 td.CmpEmpty(t, mockT.LogBuf())346 mockT = tdutil.NewT("test")347 td.CmpFalse(t,348 tdhttp.NewTestAPI(mockT, mux).349 DeleteJSON("/any/json", requestBody).350 CmpStatus(200).351 CmpHeader(containsKey).352 CmpJSONBody(expectedBody("DELETE")).353 Failed())354 td.CmpEmpty(t, mockT.LogBuf())355 // With anchors356 type ReqBody struct {357 Hey int `json:"hey"`358 }359 type Resp struct {360 Method string `json:"method"`361 ReqBody ReqBody `json:"body"`362 }363 mockT = tdutil.NewT("test")364 tt := td.NewT(mockT)365 td.CmpFalse(t,366 tdhttp.NewTestAPI(mockT, mux).367 DeleteJSON("/any/json", requestBody).368 CmpStatus(200).369 CmpHeader(containsKey).370 CmpJSONBody(Resp{371 Method: tt.A(td.Re(`^(?i)delete\z`), "").(string),372 ReqBody: ReqBody{373 Hey: tt.A(td.Between(120, 130)).(int),374 },375 }).376 Failed())377 td.CmpEmpty(t, mockT.LogBuf())378 // JSON and root operator (here SuperMapOf)379 mockT = tdutil.NewT("test")380 td.CmpFalse(t,381 tdhttp.NewTestAPI(mockT, mux).382 PostJSON("/any/json", true).383 CmpStatus(200).384 CmpJSONBody(td.JSON(`SuperMapOf({"body":Ignore()})`)).385 Failed())386 td.CmpEmpty(t, mockT.LogBuf())387 // td.Bag+td.JSON388 mockT = tdutil.NewT("test")389 td.CmpFalse(t,390 tdhttp.NewTestAPI(mockT, mux).391 PostJSON("/mirror/json",392 json.RawMessage(`[{"name":"Bob"},{"name":"Alice"}]`)).393 CmpStatus(200).394 CmpJSONBody(td.Bag(395 td.JSON(`{"name":"Alice"}`),396 td.JSON(`{"name":"Bob"}`),397 )).398 Failed())399 td.CmpEmpty(t, mockT.LogBuf())400 // td.Bag+literal401 type People struct {402 Name string `json:"name"`403 }404 mockT = tdutil.NewT("test")405 td.CmpFalse(t,406 tdhttp.NewTestAPI(mockT, mux).407 PostJSON("/mirror/json",408 json.RawMessage(`[{"name":"Bob"},{"name":"Alice"}]`)).409 CmpStatus(200).410 CmpJSONBody(td.Bag(People{"Alice"}, People{"Bob"})).411 Failed())412 td.CmpEmpty(t, mockT.LogBuf())413 })414 t.Run("No XML error", func(t *testing.T) {415 type XBody struct {416 Hey int `xml:"hey"`417 }418 type XResp struct {419 Method string `xml:"method"`420 ReqBody *XBody `xml:"XBody"`421 }422 requestBody := XBody{Hey: 123}423 expectedBody := func(m string) XResp {424 return XResp{425 Method: m,426 ReqBody: &requestBody,427 }428 }429 mockT := tdutil.NewT("test")430 td.CmpFalse(t,431 tdhttp.NewTestAPI(mockT, mux).432 NewXMLRequest("ZIP", "/any/xml", requestBody).433 CmpStatus(200).434 CmpHeader(containsKey).435 CmpXMLBody(expectedBody("ZIP")).436 Failed())437 td.CmpEmpty(t, mockT.LogBuf())438 mockT = tdutil.NewT("test")439 td.CmpFalse(t,440 tdhttp.NewTestAPI(mockT, mux).441 PostXML("/any/xml", requestBody).442 CmpStatus(200).443 CmpHeader(containsKey).444 CmpXMLBody(expectedBody("POST")).445 Failed())446 td.CmpEmpty(t, mockT.LogBuf())447 mockT = tdutil.NewT("test")448 td.CmpFalse(t,449 tdhttp.NewTestAPI(mockT, mux).450 PutXML("/any/xml", requestBody).451 CmpStatus(200).452 CmpHeader(containsKey).453 CmpXMLBody(expectedBody("PUT")).454 Failed())455 td.CmpEmpty(t, mockT.LogBuf())456 mockT = tdutil.NewT("test")457 td.CmpFalse(t,458 tdhttp.NewTestAPI(mockT, mux).459 PatchXML("/any/xml", requestBody).460 CmpStatus(200).461 CmpHeader(containsKey).462 CmpXMLBody(expectedBody("PATCH")).463 Failed())464 td.CmpEmpty(t, mockT.LogBuf())465 mockT = tdutil.NewT("test")466 td.CmpFalse(t,467 tdhttp.NewTestAPI(mockT, mux).468 DeleteXML("/any/xml", requestBody).469 CmpStatus(200).470 CmpHeader(containsKey).471 CmpXMLBody(expectedBody("DELETE")).472 Failed())473 td.CmpEmpty(t, mockT.LogBuf())474 // With anchors475 mockT = tdutil.NewT("test")476 tt := td.NewT(mockT)477 td.CmpFalse(tt,478 tdhttp.NewTestAPI(mockT, mux).479 DeleteXML("/any/xml", requestBody).480 CmpStatus(200).481 CmpHeader(containsKey).482 CmpXMLBody(XResp{483 Method: tt.A(td.Re(`^(?i)delete\z`), "").(string),484 ReqBody: &XBody{485 Hey: tt.A(td.Between(120, 130)).(int),486 },487 }).488 Failed())489 td.CmpEmpty(t, mockT.LogBuf())490 })491 t.Run("Cookies", func(t *testing.T) {492 mockT := tdutil.NewT("test")493 td.CmpFalse(t,494 tdhttp.NewTestAPI(mockT, mux).495 Get("/any/cookies").496 CmpCookies([]*http.Cookie{497 {498 Name: "first",499 Value: "cookie1",500 MaxAge: 123456,501 Expires: time.Date(2021, time.August, 12, 11, 22, 33, 0, time.UTC),502 },503 {504 Name: "second",505 Value: "cookie2",506 MaxAge: 654321,507 },508 }).509 Failed())510 td.CmpEmpty(t, mockT.LogBuf())511 mockT = tdutil.NewT("test")512 td.CmpTrue(t,513 tdhttp.NewTestAPI(mockT, mux).514 Get("/any/cookies").515 CmpCookies([]*http.Cookie{516 {517 Name: "first",518 Value: "cookie1",519 MaxAge: 123456,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")...

Full Screen

Full Screen

request.go

Source:request.go Github

copy

Full Screen

...135 append(headersQueryParams, "Content-Type", data.ContentType()),136 )137}138func put(target string, body io.Reader, headersQueryParams ...any) (*http.Request, error) {139 return newRequest(http.MethodPut, target, body, headersQueryParams)140}141func patch(target string, body io.Reader, headersQueryParams ...any) (*http.Request, error) {142 return newRequest(http.MethodPatch, target, body, headersQueryParams)143}144func del(target string, body io.Reader, headersQueryParams ...any) (*http.Request, error) {145 return newRequest(http.MethodDelete, target, body, headersQueryParams)146}147// NewRequest creates a new HTTP request as [httptest.NewRequest]148// does, with the ability to immediately add some headers and/or some149// query parameters.150//151// Headers can be added using string pairs as in:152//153// req := tdhttp.NewRequest("POST", "/pdf", body,154// "Content-type", "application/pdf",155// "X-Test", "value",156// )157//158// or using [http.Header] as in:159//160// req := tdhttp.NewRequest("POST", "/pdf", body,161// http.Header{"Content-type": []string{"application/pdf"}},162// )163//164// or using [BasicAuthHeader] as in:165//166// req := tdhttp.NewRequest("POST", "/pdf", body,167// tdhttp.BasicAuthHeader("max", "5ecr3T"),168// )169//170// or using [http.Cookie] (pointer or not, behind the scene,171// [http.Request.AddCookie] is used) as in:172//173// req := tdhttp.NewRequest("POST", "/pdf", body,174// http.Cookie{Name: "cook1", Value: "val1"},175// &http.Cookie{Name: "cook2", Value: "val2"},176// )177//178// Several header sources are combined:179//180// req := tdhttp.NewRequest("POST", "/pdf", body,181// "Content-type", "application/pdf",182// http.Header{"X-Test": []string{"value1"}},183// "X-Test", "value2",184// http.Cookie{Name: "cook1", Value: "val1"},185// tdhttp.BasicAuthHeader("max", "5ecr3T"),186// &http.Cookie{Name: "cook2", Value: "val2"},187// )188//189// Produces the following [http.Header]:190//191// http.Header{192// "Authorization": []string{"Basic bWF4OjVlY3IzVA=="},193// "Content-type": []string{"application/pdf"},194// "Cookie": []string{"cook1=val1; cook2=val2"},195// "X-Test": []string{"value1", "value2"},196// }197//198// A string slice or a map can be flatened as well. As [NewRequest] expects199// ...any, [td.Flatten] can help here too:200//201// strHeaders := map[string]string{202// "X-Length": "666",203// "X-Foo": "bar",204// }205// req := tdhttp.NewRequest("POST", "/pdf", body, td.Flatten(strHeaders))206//207// Or combined with forms seen above:208//209// req := tdhttp.NewRequest("POST", "/pdf", body,210// "Content-type", "application/pdf",211// http.Header{"X-Test": []string{"value1"}},212// td.Flatten(strHeaders),213// "X-Test", "value2",214// http.Cookie{Name: "cook1", Value: "val1"},215// tdhttp.BasicAuthHeader("max", "5ecr3T"),216// &http.Cookie{Name: "cook2", Value: "val2"},217// )218//219// Header keys are always canonicalized using [http.CanonicalHeaderKey].220//221// Query parameters can be added using [url.Values] or more flexible222// [Q], as in:223//224// req := tdhttp.NewRequest("GET", "/pdf",225// url.Values{226// "param": {"val"},227// "names": {"bob", "alice"},228// },229// "X-Test": "a header in the middle",230// tdhttp.Q{231// "limit": 20,232// "ids": []int64{456, 789},233// "details": true,234// },235// )236//237// All [url.Values] and [Q] instances are combined to produce the238// final query string to use. The previous example produces the239// following target:240//241// /pdf?details=true&ids=456&ids=789&limit=20&names=bob&names=alice&param=val242//243// If target already contains a query string, it is reused:244//245// req := tdhttp.NewRequest("GET", "/pdf?limit=10", tdhttp.Q{"details": true})246//247// produces the following target:248//249// /path?details=true&limit=10250//251// Behind the scene, [url.Values.Encode] is used, so the parameters252// are always sorted by key. If you want a specific order, then do not253// use [url.Values] nor [Q] instances, but compose target by yourself.254//255// See [Q] documentation to learn how values are stringified.256func NewRequest(method, target string, body io.Reader, headersQueryParams ...any) *http.Request {257 req, err := newRequest(method, target, body, headersQueryParams)258 if err != nil {259 panic(err)260 }261 return req262}263// Get creates a new HTTP GET. It is a shortcut for:264//265// tdhttp.NewRequest(http.MethodGet, target, nil, headersQueryParams...)266//267// See [NewRequest] for all possible formats accepted in headersQueryParams.268func Get(target string, headersQueryParams ...any) *http.Request {269 req, err := get(target, headersQueryParams...)270 if err != nil {271 panic(err)272 }273 return req274}275// Head creates a new HTTP HEAD. It is a shortcut for:276//277// tdhttp.NewRequest(http.MethodHead, target, nil, headersQueryParams...)278//279// See [NewRequest] for all possible formats accepted in headersQueryParams.280func Head(target string, headersQueryParams ...any) *http.Request {281 req, err := head(target, headersQueryParams...)282 if err != nil {283 panic(err)284 }285 return req286}287// Options creates a HTTP OPTIONS. It is a shortcut for:288//289// tdhttp.NewRequest(http.MethodOptions, target, body, headersQueryParams...)290//291// See [NewRequest] for all possible formats accepted in headersQueryParams.292func Options(target string, body io.Reader, headersQueryParams ...any) *http.Request {293 req, err := options(target, body, headersQueryParams...)294 if err != nil {295 panic(err)296 }297 return req298}299// Post creates a HTTP POST. It is a shortcut for:300//301// tdhttp.NewRequest(http.MethodPost, target, body, headersQueryParams...)302//303// See [NewRequest] for all possible formats accepted in headersQueryParams.304func Post(target string, body io.Reader, headersQueryParams ...any) *http.Request {305 req, err := post(target, body, headersQueryParams...)306 if err != nil {307 panic(err)308 }309 return req310}311// URLValuesEncoder is an interface [PostForm] and [TestAPI.PostForm] data312// must implement.313// Encode can be called to generate a "URL encoded" form such as314// ("bar=baz&foo=quux") sorted by key.315//316// [url.Values] and [Q] implement this interface.317type URLValuesEncoder interface {318 Encode() string319}320// PostForm creates a HTTP POST with data's keys and values321// URL-encoded as the request body. "Content-Type" header is322// automatically set to "application/x-www-form-urlencoded". Other323// headers can be added via headersQueryParams, as in:324//325// req := tdhttp.PostForm("/data",326// url.Values{327// "param1": []string{"val1", "val2"},328// "param2": []string{"zip"},329// },330// "X-Foo", "Foo-value",331// "X-Zip", "Zip-value",332// )333//334// See [NewRequest] for all possible formats accepted in headersQueryParams.335func PostForm(target string, data URLValuesEncoder, headersQueryParams ...any) *http.Request {336 req, err := postForm(target, data, headersQueryParams...)337 if err != nil {338 panic(err)339 }340 return req341}342// PostMultipartFormData creates a HTTP POST multipart request, like343// multipart/form-data one for example. See [MultipartBody] type for344// details. "Content-Type" header is automatically set depending on345// data.MediaType (defaults to "multipart/form-data") and data.Boundary346// (defaults to "go-testdeep-42"). Other headers can be added via347// headersQueryParams, as in:348//349// req := tdhttp.PostMultipartFormData("/data",350// &tdhttp.MultipartBody{351// // "multipart/form-data" by default352// Parts: []*tdhttp.MultipartPart{353// tdhttp.NewMultipartPartString("type", "Sales"),354// tdhttp.NewMultipartPartFile("report", "report.json", "application/json"),355// },356// },357// "X-Foo", "Foo-value",358// "X-Zip", "Zip-value",359// )360//361// and with a different media type:362//363// req := tdhttp.PostMultipartFormData("/data",364// &tdhttp.MultipartBody{365// MediaType: "multipart/mixed",366// Parts: []*tdhttp.MultipartPart{367// tdhttp.NewMultipartPartString("type", "Sales"),368// tdhttp.NewMultipartPartFile("report", "report.json", "application/json"),369// },370// },371// "X-Foo", "Foo-value",372// "X-Zip", "Zip-value",373// )374//375// See [NewRequest] for all possible formats accepted in headersQueryParams.376func PostMultipartFormData(target string, data *MultipartBody, headersQueryParams ...any) *http.Request {377 req, err := postMultipartFormData(target, data, headersQueryParams...)378 if err != nil {379 panic(err)380 }381 return req382}383// Put creates a HTTP PUT. It is a shortcut for:384//385// tdhttp.NewRequest(http.MethodPut, target, body, headersQueryParams...)386//387// See [NewRequest] for all possible formats accepted in headersQueryParams.388func Put(target string, body io.Reader, headersQueryParams ...any) *http.Request {389 req, err := put(target, body, headersQueryParams...)390 if err != nil {391 panic(err)392 }393 return req394}395// Patch creates a HTTP PATCH. It is a shortcut for:396//397// tdhttp.NewRequest(http.MethodPatch, target, body, headersQueryParams...)398//399// See [NewRequest] for all possible formats accepted in headersQueryParams.400func Patch(target string, body io.Reader, headersQueryParams ...any) *http.Request {401 req, err := patch(target, body, headersQueryParams...)402 if err != nil {403 panic(err)404 }405 return req406}407// Delete creates a HTTP DELETE. It is a shortcut for:408//409// tdhttp.NewRequest(http.MethodDelete, target, body, headersQueryParams...)410//411// See [NewRequest] for all possible formats accepted in headersQueryParams.412func Delete(target string, body io.Reader, headersQueryParams ...any) *http.Request {413 req, err := del(target, body, headersQueryParams...)414 if err != nil {415 panic(err)416 }417 return req418}419func newJSONRequest(method, target string, body any, headersQueryParams ...any) (*http.Request, error) {420 b, err := json.Marshal(body)421 if err != nil {422 if opErr, ok := types.AsOperatorNotJSONMarshallableError(err); ok {423 var plus string424 switch op := opErr.Operator(); op {425 case "JSON", "SubJSONOf", "SuperJSONOf":426 plus = ", use json.RawMessage() instead"427 }428 return nil, errors.New(color.Bad("JSON encoding failed: %s%s", err, plus))429 }430 return nil, errors.New(color.Bad("%s", err))431 }432 return newRequest(433 method, target, bytes.NewReader(b),434 append(headersQueryParams, "Content-Type", "application/json"),435 )436}437// NewJSONRequest creates a new HTTP request with body marshaled to438// JSON. "Content-Type" header is automatically set to439// "application/json". Other headers can be added via headersQueryParams, as in:440//441// req := tdhttp.NewJSONRequest("POST", "/data", body,442// "X-Foo", "Foo-value",443// "X-Zip", "Zip-value",444// )445//446// See [NewRequest] for all possible formats accepted in headersQueryParams.447func NewJSONRequest(method, target string, body any, headersQueryParams ...any) *http.Request {448 req, err := newJSONRequest(method, target, body, headersQueryParams...)449 if err != nil {450 panic(err)451 }452 return req453}454// PostJSON creates a HTTP POST with body marshaled to455// JSON. "Content-Type" header is automatically set to456// "application/json". It is a shortcut for:457//458// tdhttp.NewJSONRequest(http.MethodPost, target, body, headersQueryParams...)459//460// See [NewRequest] for all possible formats accepted in headersQueryParams.461func PostJSON(target string, body any, headersQueryParams ...any) *http.Request {462 req, err := newJSONRequest(http.MethodPost, target, body, headersQueryParams...)463 if err != nil {464 panic(err)465 }466 return req467}468// PutJSON creates a HTTP PUT with body marshaled to469// JSON. "Content-Type" header is automatically set to470// "application/json". It is a shortcut for:471//472// tdhttp.NewJSONRequest(http.MethodPut, target, body, headersQueryParams...)473//474// See [NewRequest] for all possible formats accepted in headersQueryParams.475func PutJSON(target string, body any, headersQueryParams ...any) *http.Request {476 req, err := newJSONRequest(http.MethodPut, target, body, headersQueryParams...)477 if err != nil {478 panic(err)479 }480 return req481}482// PatchJSON creates a HTTP PATCH with body marshaled to483// JSON. "Content-Type" header is automatically set to484// "application/json". It is a shortcut for:485//486// tdhttp.NewJSONRequest(http.MethodPatch, target, body, headersQueryParams...)487//488// See [NewRequest] for all possible formats accepted in headersQueryParams.489func PatchJSON(target string, body any, headersQueryParams ...any) *http.Request {490 req, err := newJSONRequest(http.MethodPatch, target, body, headersQueryParams...)491 if err != nil {492 panic(err)493 }494 return req495}496// DeleteJSON creates a HTTP DELETE with body marshaled to497// JSON. "Content-Type" header is automatically set to498// "application/json". It is a shortcut for:499//500// tdhttp.NewJSONRequest(http.MethodDelete, target, body, headersQueryParams...)501//502// See [NewRequest] for all possible formats accepted in headersQueryParams.503func DeleteJSON(target string, body any, headersQueryParams ...any) *http.Request {504 req, err := newJSONRequest(http.MethodDelete, target, body, headersQueryParams...)505 if err != nil {506 panic(err)507 }508 return req509}510func newXMLRequest(method, target string, body any, headersQueryParams ...any) (*http.Request, error) {511 b, err := xml.Marshal(body)512 if err != nil {513 return nil, errors.New(color.Bad("XML encoding failed: %s", err))514 }515 return newRequest(516 method, target, bytes.NewReader(b),517 append(headersQueryParams, "Content-Type", "application/xml"),518 )519}520// NewXMLRequest creates a new HTTP request with body marshaled to521// XML. "Content-Type" header is automatically set to522// "application/xml". Other headers can be added via headersQueryParams, as in:523//524// req := tdhttp.NewXMLRequest("POST", "/data", body,525// "X-Foo", "Foo-value",526// "X-Zip", "Zip-value",527// )528//529// See [NewRequest] for all possible formats accepted in headersQueryParams.530func NewXMLRequest(method, target string, body any, headersQueryParams ...any) *http.Request {531 req, err := newXMLRequest(method, target, body, headersQueryParams...)532 if err != nil {533 panic(err)534 }535 return req536}537// PostXML creates a HTTP POST with body marshaled to538// XML. "Content-Type" header is automatically set to539// "application/xml". It is a shortcut for:540//541// tdhttp.NewXMLRequest(http.MethodPost, target, body, headersQueryParams...)542//543// See [NewRequest] for all possible formats accepted in headersQueryParams.544func PostXML(target string, body any, headersQueryParams ...any) *http.Request {545 req, err := newXMLRequest(http.MethodPost, target, body, headersQueryParams...)546 if err != nil {547 panic(err)548 }549 return req550}551// PutXML creates a HTTP PUT with body marshaled to552// XML. "Content-Type" header is automatically set to553// "application/xml". It is a shortcut for:554//555// tdhttp.NewXMLRequest(http.MethodPut, target, body, headersQueryParams...)556//557// See [NewRequest] for all possible formats accepted in headersQueryParams.558func PutXML(target string, body any, headersQueryParams ...any) *http.Request {559 req, err := newXMLRequest(http.MethodPut, target, body, headersQueryParams...)560 if err != nil {561 panic(err)562 }563 return req564}565// PatchXML creates a HTTP PATCH with body marshaled to566// XML. "Content-Type" header is automatically set to567// "application/xml". It is a shortcut for:568//569// tdhttp.NewXMLRequest(http.MethodPatch, target, body, headersQueryParams...)570//571// See [NewRequest] for all possible formats accepted in headersQueryParams.572func PatchXML(target string, body any, headersQueryParams ...any) *http.Request {573 req, err := newXMLRequest(http.MethodPatch, target, body, headersQueryParams...)...

Full Screen

Full Screen

request_test.go

Source:request_test.go Github

copy

Full Screen

...139 t.CmpPanic(140 func() { tdhttp.Patch("/path", nil, true) },141 td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))142 t.CmpPanic(143 func() { tdhttp.Put("/path", nil, true) },144 td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))145 t.CmpPanic(146 func() { tdhttp.Delete("/path", nil, true) },147 td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))148 // Bad target149 t.CmpPanic(150 func() { tdhttp.NewRequest("GET", ":/badpath", nil) },151 td.HasPrefix(`target is not a valid path: `))152 // Q error153 t.CmpPanic(154 func() { tdhttp.Get("/", tdhttp.Q{"bad": map[string]bool{}}) },155 td.HasPrefix(`headersQueryParams... tdhttp.Q bad parameter: don't know how to add type map[string]bool (map) to param "bad" (@ headersQueryParams[0])`))156 })157 // Get158 t.Cmp(tdhttp.Get("/path", "Foo", "Bar"),159 td.Struct(160 &http.Request{161 Method: "GET",162 Header: http.Header{"Foo": []string{"Bar"}},163 },164 td.StructFields{165 "URL": td.String("/path"),166 }))167 // Head168 t.Cmp(tdhttp.Head("/path", "Foo", "Bar"),169 td.Struct(170 &http.Request{171 Method: "HEAD",172 Header: http.Header{"Foo": []string{"Bar"}},173 },174 td.StructFields{175 "URL": td.String("/path"),176 }))177 // Options178 t.Cmp(tdhttp.Options("/path", nil, "Foo", "Bar"),179 td.Struct(180 &http.Request{181 Method: "OPTIONS",182 Header: http.Header{"Foo": []string{"Bar"}},183 },184 td.StructFields{185 "URL": td.String("/path"),186 }))187 // Post188 t.Cmp(tdhttp.Post("/path", nil, "Foo", "Bar"),189 td.Struct(190 &http.Request{191 Method: "POST",192 Header: http.Header{"Foo": []string{"Bar"}},193 },194 td.StructFields{195 "URL": td.String("/path"),196 }))197 // PostForm - url.Values198 t.Cmp(199 tdhttp.PostForm("/path",200 url.Values{201 "param1": []string{"val1", "val2"},202 "param2": []string{"zip"},203 },204 "Foo", "Bar"),205 td.Struct(206 &http.Request{207 Method: "POST",208 Header: http.Header{209 "Content-Type": []string{"application/x-www-form-urlencoded"},210 "Foo": []string{"Bar"},211 },212 },213 td.StructFields{214 "URL": td.String("/path"),215 "Body": td.Smuggle(216 io.ReadAll,217 []byte("param1=val1&param1=val2&param2=zip"),218 ),219 }))220 // PostForm - td.Q221 t.Cmp(222 tdhttp.PostForm("/path",223 tdhttp.Q{224 "param1": "val1",225 "param2": "val2",226 },227 "Foo", "Bar"),228 td.Struct(229 &http.Request{230 Method: "POST",231 Header: http.Header{232 "Content-Type": []string{"application/x-www-form-urlencoded"},233 "Foo": []string{"Bar"},234 },235 },236 td.StructFields{237 "URL": td.String("/path"),238 "Body": td.Smuggle(239 io.ReadAll,240 []byte("param1=val1&param2=val2"),241 ),242 }))243 // PostForm - nil data244 t.Cmp(245 tdhttp.PostForm("/path", nil, "Foo", "Bar"),246 td.Struct(247 &http.Request{248 Method: "POST",249 Header: http.Header{250 "Content-Type": []string{"application/x-www-form-urlencoded"},251 "Foo": []string{"Bar"},252 },253 },254 td.StructFields{255 "URL": td.String("/path"),256 "Body": td.Smuggle(257 io.ReadAll,258 []byte{},259 ),260 }))261 // PostMultipartFormData262 req := tdhttp.PostMultipartFormData("/path",263 &tdhttp.MultipartBody{264 Boundary: "BoUnDaRy",265 Parts: []*tdhttp.MultipartPart{266 tdhttp.NewMultipartPartString("p1", "body1!"),267 tdhttp.NewMultipartPartString("p2", "body2!"),268 },269 },270 "Foo", "Bar")271 t.Cmp(req,272 td.Struct(273 &http.Request{274 Method: "POST",275 Header: http.Header{276 "Content-Type": []string{`multipart/form-data; boundary="BoUnDaRy"`},277 "Foo": []string{"Bar"},278 },279 },280 td.StructFields{281 "URL": td.String("/path"),282 }))283 if t.CmpNoError(req.ParseMultipartForm(10000)) {284 t.Cmp(req.PostFormValue("p1"), "body1!")285 t.Cmp(req.PostFormValue("p2"), "body2!")286 }287 // Put288 t.Cmp(tdhttp.Put("/path", nil, "Foo", "Bar"),289 td.Struct(290 &http.Request{291 Method: "PUT",292 Header: http.Header{"Foo": []string{"Bar"}},293 },294 td.StructFields{295 "URL": td.String("/path"),296 }))297 // Patch298 t.Cmp(tdhttp.Patch("/path", nil, "Foo", "Bar"),299 td.Struct(300 &http.Request{301 Method: "PATCH",302 Header: http.Header{"Foo": []string{"Bar"}},303 },304 td.StructFields{305 "URL": td.String("/path"),306 }))307 // Delete308 t.Cmp(tdhttp.Delete("/path", nil, "Foo", "Bar"),309 td.Struct(310 &http.Request{311 Method: "DELETE",312 Header: http.Header{"Foo": []string{"Bar"}},313 },314 td.StructFields{315 "URL": td.String("/path"),316 }))317}318type TestStruct struct {319 Name string `json:"name" xml:"name"`320}321func TestNewJSONRequest(tt *testing.T) {322 t := td.NewT(tt)323 t.Run("NewJSONRequest", func(t *td.T) {324 req := tdhttp.NewJSONRequest("GET", "/path",325 TestStruct{326 Name: "Bob",327 },328 "Foo", "Bar",329 "Zip", "Test")330 t.String(req.Header.Get("Content-Type"), "application/json")331 t.String(req.Header.Get("Foo"), "Bar")332 t.String(req.Header.Get("Zip"), "Test")333 body, err := io.ReadAll(req.Body)334 if t.CmpNoError(err, "read request body") {335 t.String(string(body), `{"name":"Bob"}`)336 }337 })338 t.Run("NewJSONRequest panic", func(t *td.T) {339 t.CmpPanic(340 func() { tdhttp.NewJSONRequest("GET", "/path", func() {}) },341 td.Contains("json: unsupported type: func()"))342 t.CmpPanic(343 func() { tdhttp.PostJSON("/path", func() {}) },344 td.Contains("json: unsupported type: func()"))345 t.CmpPanic(346 func() { tdhttp.PutJSON("/path", func() {}) },347 td.Contains("json: unsupported type: func()"))348 t.CmpPanic(349 func() { tdhttp.PatchJSON("/path", func() {}) },350 td.Contains("json: unsupported type: func()"))351 t.CmpPanic(352 func() { tdhttp.DeleteJSON("/path", func() {}) },353 td.Contains("json: unsupported type: func()"))354 t.CmpPanic(355 func() { tdhttp.NewJSONRequest("GET", "/path", td.JSONPointer("/a", 0)) },356 td.Contains("JSON encoding failed: json: error calling MarshalJSON for type *td.tdJSONPointer: JSONPointer TestDeep operator cannot be json.Marshal'led"))357 // Common user mistake358 t.CmpPanic(359 func() { tdhttp.NewJSONRequest("GET", "/path", td.JSON(`{}`)) },360 td.Contains(`JSON encoding failed: json: error calling MarshalJSON for type *td.tdJSON: JSON TestDeep operator cannot be json.Marshal'led, use json.RawMessage() instead`))361 })362 // Post363 t.Cmp(tdhttp.PostJSON("/path", 42, "Foo", "Bar"),364 td.Struct(365 &http.Request{366 Method: "POST",367 Header: http.Header{368 "Foo": []string{"Bar"},369 "Content-Type": []string{"application/json"},370 },371 },372 td.StructFields{373 "URL": td.String("/path"),374 }))375 // Put376 t.Cmp(tdhttp.PutJSON("/path", 42, "Foo", "Bar"),377 td.Struct(378 &http.Request{379 Method: "PUT",380 Header: http.Header{381 "Foo": []string{"Bar"},382 "Content-Type": []string{"application/json"},383 },384 },385 td.StructFields{386 "URL": td.String("/path"),387 }))388 // Patch389 t.Cmp(tdhttp.PatchJSON("/path", 42, "Foo", "Bar"),390 td.Struct(391 &http.Request{392 Method: "PATCH",393 Header: http.Header{394 "Foo": []string{"Bar"},395 "Content-Type": []string{"application/json"},396 },397 },398 td.StructFields{399 "URL": td.String("/path"),400 }))401 // Delete402 t.Cmp(tdhttp.DeleteJSON("/path", 42, "Foo", "Bar"),403 td.Struct(404 &http.Request{405 Method: "DELETE",406 Header: http.Header{407 "Foo": []string{"Bar"},408 "Content-Type": []string{"application/json"},409 },410 },411 td.StructFields{412 "URL": td.String("/path"),413 }))414}415func TestNewXMLRequest(tt *testing.T) {416 t := td.NewT(tt)417 t.Run("NewXMLRequest", func(t *td.T) {418 req := tdhttp.NewXMLRequest("GET", "/path",419 TestStruct{420 Name: "Bob",421 },422 "Foo", "Bar",423 "Zip", "Test")424 t.String(req.Header.Get("Content-Type"), "application/xml")425 t.String(req.Header.Get("Foo"), "Bar")426 t.String(req.Header.Get("Zip"), "Test")427 body, err := io.ReadAll(req.Body)428 if t.CmpNoError(err, "read request body") {429 t.String(string(body), `<TestStruct><name>Bob</name></TestStruct>`)430 }431 })432 t.Run("NewXMLRequest panic", func(t *td.T) {433 t.CmpPanic(434 func() { tdhttp.NewXMLRequest("GET", "/path", func() {}) },435 td.Contains("XML encoding failed"))436 t.CmpPanic(437 func() { tdhttp.PostXML("/path", func() {}) },438 td.Contains("XML encoding failed"))439 t.CmpPanic(440 func() { tdhttp.PutXML("/path", func() {}) },441 td.Contains("XML encoding failed"))442 t.CmpPanic(443 func() { tdhttp.PatchXML("/path", func() {}) },444 td.Contains("XML encoding failed"))445 t.CmpPanic(446 func() { tdhttp.DeleteXML("/path", func() {}) },447 td.Contains("XML encoding failed"))448 })449 // Post450 t.Cmp(tdhttp.PostXML("/path", 42, "Foo", "Bar"),451 td.Struct(452 &http.Request{453 Method: "POST",454 Header: http.Header{455 "Foo": []string{"Bar"},456 "Content-Type": []string{"application/xml"},457 },458 },459 td.StructFields{460 "URL": td.String("/path"),461 }))462 // Put463 t.Cmp(tdhttp.PutXML("/path", 42, "Foo", "Bar"),464 td.Struct(465 &http.Request{466 Method: "PUT",467 Header: http.Header{468 "Foo": []string{"Bar"},469 "Content-Type": []string{"application/xml"},470 },471 },472 td.StructFields{473 "URL": td.String("/path"),474 }))475 // Patch476 t.Cmp(tdhttp.PatchXML("/path", 42, "Foo", "Bar"),477 td.Struct(...

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m := minify.New()4 m.AddFunc("text/css", css.Minify)5 m.AddFunc("text/html", html.Minify)6 m.AddFunc("application/json", json.Minify)7 m.AddFunc("image/svg+xml", svg.Minify)8 m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)9 m.AddFunc("application/javascript", js.Minify)10 client := &http.Client{}11 if err != nil {12 fmt.Println(err)13 }14 resp, err := client.Do(req)15 if err != nil {16 fmt.Println(err)17 }18 file, err := os.Create("test.html")19 if err != nil {20 fmt.Println(err)21 }22 err = m.Minify("text/html", file, resp.Body)23 if err != nil {24 fmt.Println(err)25 }26 file.Close()27 resp.Body.Close()28}29import (

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lgr := logrus.New()4 s := server.NewServer(lgr, server.NewOptions())5 c := tdhttp.NewClient(s, lgr)6 col := collection.NewCollection(s, "fleet", nil)7 if err != nil {8 lgr.Fatal(err)9 }10 req.Body = &tdhttp.RequestBody{11 Args: []interface{}{"POINT", 33.5123, -112.2693},12 }13 res := c.Do(req)14 value := gjson.GetBytes(body, "ok")15 if value.Bool() {16 value := gjson.GetBytes(body, "elapsed")17 fmt.Println(value.String())18 }19 if err != nil {20 lgr.Fatal(err)21 }22 req.Body = &tdhttp.RequestBody{

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import "github.com/tdhttp"2func main() {3}4import "github.com/tdhttp"5func main() {6}7import "github.com/tdhttp"8func main() {9}10import "github.com/tdhttp"11func main() {12}13import "github.com/tdhttp"14func main() {15}16import "github.com/tdhttp"17func main() {18}19import "github.com/tdhttp"20func main() {21}22import "github.com/tdhttp"23func main() {24}25import "github.com/tdhttp"26func main() {27}28import "github.com/tdhttp"29func main() {30}

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1tdhttp tdhttp = new tdhttp();2tdhttp tdhttp = new tdhttp();3tdhttp tdhttp = new tdhttp();4tdhttp tdhttp = new tdhttp();5tdhttp tdhttp = new tdhttp();6tdhttp tdhttp = new tdhttp();7tdhttp tdhttp = new tdhttp();8tdhttp tdhttp = new tdhttp();9tdhttp tdhttp = new tdhttp();

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