How to use Delete method of tdhttp Package

Best Go-testdeep code snippet using tdhttp.Delete

request.go

Source:request.go Github

copy

Full Screen

...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...)574 if err != nil {575 panic(err)576 }577 return req578}579// DeleteXML creates a HTTP DELETE with body marshaled to580// XML. "Content-Type" header is automatically set to581// "application/xml". It is a shortcut for:582//583// tdhttp.NewXMLRequest(http.MethodDelete, target, body, headersQueryParams...)584//585// See [NewRequest] for all possible formats accepted in headersQueryParams.586func DeleteXML(target string, body any, headersQueryParams ...any) *http.Request {587 req, err := newXMLRequest(http.MethodDelete, target, body, headersQueryParams...)588 if err != nil {589 panic(err)590 }591 return req592}...

Full Screen

Full Screen

request_test.go

Source:request_test.go Github

copy

Full Screen

...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(478 &http.Request{479 Method: "PATCH",480 Header: http.Header{481 "Foo": []string{"Bar"},482 "Content-Type": []string{"application/xml"},483 },484 },485 td.StructFields{486 "URL": td.String("/path"),487 }))488 // Delete489 t.Cmp(tdhttp.DeleteXML("/path", 42, "Foo", "Bar"),490 td.Struct(491 &http.Request{492 Method: "DELETE",493 Header: http.Header{494 "Foo": []string{"Bar"},495 "Content-Type": []string{"application/xml"},496 },497 },498 td.StructFields{499 "URL": td.String("/path"),500 }))501}...

Full Screen

Full Screen

api_delete_test.go

Source:api_delete_test.go Github

copy

Full Screen

...10)11type deleteSuite struct {12 suite.Suite13}14func TestDelete(t *testing.T) {15 suite.Run(t, new(deleteSuite))16}17func (suite *deleteSuite) BeforeTest(suiteName, testName string) {18 TestContext.BeforeTest()19}20func (suite *deleteSuite) TestDeleteWithValidSlugReturns204() {21 t := suite.T()22 testServer := TestContext.server23 testAPI := tdhttp.NewTestAPI(t, testServer)24 var slug string25 testAPI.PostJSON("/api/v1/shorturls", gin.H{"long_url": "https://www.google.com"}).26 CmpStatus(http.StatusCreated).27 CmpJSONBody(28 td.SuperJSONOf(29 `{"slug": "$slug"}`,30 td.Tag("slug", td.Catch(&slug, td.Ignore())),31 td.Tag("shortUrl", td.Ignore()),32 ),33 )34 testAPI.Delete(fmt.Sprintf("/api/v1/shorturls/%s", slug), nil).35 CmpStatus(http.StatusNoContent)36 testAPI.Get(fmt.Sprintf("/%s", slug)).CmpStatus(http.StatusNotFound)37}38func (suite *deleteSuite) TestDeleteWithInvalidSlugReturns404() {39 t := suite.T()40 testServer := TestContext.server41 testAPI := tdhttp.NewTestAPI(t, testServer)42 testAPI.Delete("/api/v1/shorturls/invalid", nil).43 CmpStatus(http.StatusNotFound).44 CmpJSONBody(45 td.JSON(46 `{47 "errors": [48 {49 "field": "Slug",50 "reason": "not found"51 }52 ],53 }`,54 ),55 )56}...

Full Screen

Full Screen

Delete

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("text/javascript", js.Minify)7 m.AddFunc("application/json", json.Minify)8 m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)9 m.AddFuncRegexp(regexp.MustCompile("[/+]svg$"), svg.Minify)10 minified, err := m.String("text/html", `<!DOCTYPE html>11 body {12 background-color: #fff;13 }14 console.log("Test");15 if err != nil {16 panic(err)17 }18 fmt.Println(minified)19 if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil {20 panic(err)21 }22}23import (24func main() {25 client := &http.Client{}26 if err != nil {27 panic(err)28 }29 resp, err := client.Do(req)30 if err != nil {31 panic(err)32 }33 defer resp.Body.Close()34 respBody, _ := ioutil.ReadAll(resp.Body)35 fmt.Println("response Status : ", resp.Status)36 fmt.Println("response Headers

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tdhttp := tdhttp.New()4 fmt.Println(tdhttp.Response)5}6import (7func main() {8 tdhttp := tdhttp.New()9 fmt.Println(tdhttp.Response)10}11import (12func main() {13 tdhttp := tdhttp.New()

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4type tdhttp struct {5}6func (tdhttp) Delete(url string, accept string, contentType string) {7 client := &http.Client{}8 req, err := http.NewRequest("DELETE", url, nil)9 if err != nil {10 }11 req.Header.Set("Accept", accept)12 req.Header.Set("Content-Type", contentType)13 resp, err := client.Do(req)14 if err != nil {15 }16 defer resp.Body.Close()17 body, err := ioutil.ReadAll(resp.Body)18 if err != nil {19 }20 fmt.Println(string(body))21}22import (23func main() {

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 resp := client.Delete("/delete")4 fmt.Println(resp.String())5}6import (7func main() {8 resp := client.Put("/put")9 fmt.Println(resp.String())10}11import (12func main() {13 resp := client.Patch("/patch")14 fmt.Println(resp.String())15}16import (17func main() {18 resp := client.PostForm("/post", tdhttp.Values{19 "foo": {"bar"},20 "baz": {"qux"},21 })22 fmt.Println(resp.String())23}24import (25func main() {26 resp := client.PostJson("/post", tdhttp.Values{27 "foo": {"bar"},28 "baz": {"qux"},29 })30 fmt.Println(resp.String())31}32import (33func main() {34 resp := client.Post("/post", tdhttp.Values{35 "foo": {"bar"},36 "baz": {"qux"},37 })38 fmt.Println(resp.String())39}

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := tdhttp.NewClient()4 resp, err := client.Delete(url, nil)5 if err != nil {6 fmt.Println(err)7 }8 defer resp.Body.Close()9 fmt.Println(resp)10}11import (12func main() {13 client := tdhttp.NewClient()14 resp, err := client.Head(url, nil)15 if err != nil {16 fmt.Println(err)17 }18 defer resp.Body.Close()19 fmt.Println(resp)20}21import (22func main() {23 client := tdhttp.NewClient()24 resp, err := client.Options(url, nil)25 if err != nil {26 fmt.Println(err)27 }28 defer resp.Body.Close()29 fmt.Println(resp)30}31import (32func main() {33 client := tdhttp.NewClient()34 resp, err := client.Patch(url, nil)35 if err != nil {36 fmt.Println(err)37 }38 defer resp.Body.Close()39 fmt.Println(resp)40}41import (42func main() {43 client := tdhttp.NewClient()44 resp, err := client.Post(url, nil)45 if err != nil {46 fmt.Println(err)47 }

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 result, err = td.Delete(url)4 if err != nil {5 fmt.Println("Error: ", err)6 } else {7 fmt.Println("Result: ", result)8 }9}10import (11func main() {12 result, err = td.Put(url)13 if err != nil {14 fmt.Println("Error: ", err)15 } else {16 fmt.Println("Result: ", result)17 }18}19import (20func main() {21 result, err = td.Get(url)22 if err != nil {23 fmt.Println("Error: ", err)24 } else {25 fmt.Println("Result: ", result)26 }27}

Full Screen

Full Screen

Delete

Using AI Code Generation

copy

Full Screen

1import (2func main() {3td := new(tdhttp.Tdhttp)4td.SetMethod("DELETE")5td.SetBody("This is a sample body")6err := td.Send()7if err != nil {8fmt.Println(err)9}10fmt.Println(td.Response())11}

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