MCPcopy Index your code
hub / github.com/labstack/echo / TestValuesFromForm

Function TestValuesFromForm

middleware/extractor_test.go:491–625  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

489}
490
491func TestValuesFromForm(t *testing.T) {
492 examplePostFormRequest := func(mod func(v *url.Values)) *http.Request {
493 f := make(url.Values)
494 f.Set("name", "Jon Snow")
495 f.Set("emails[]", "jon@labstack.com")
496 if mod != nil {
497 mod(&f)
498 }
499
500 req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
501 req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
502
503 return req
504 }
505 exampleGetFormRequest := func(mod func(v *url.Values)) *http.Request {
506 f := make(url.Values)
507 f.Set("name", "Jon Snow")
508 f.Set("emails[]", "jon@labstack.com")
509 if mod != nil {
510 mod(&f)
511 }
512
513 req := httptest.NewRequest(http.MethodGet, "/?"+f.Encode(), nil)
514 return req
515 }
516
517 exampleMultiPartFormRequest := func(mod func(w *multipart.Writer)) *http.Request {
518 var b bytes.Buffer
519 w := multipart.NewWriter(&b)
520 w.WriteField("name", "Jon Snow")
521 w.WriteField("emails[]", "jon@labstack.com")
522 if mod != nil {
523 mod(w)
524 }
525
526 fw, _ := w.CreateFormFile("upload", "my.file")
527 fw.Write([]byte(`<div>hi</div>`))
528 w.Close()
529
530 req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(b.String()))
531 req.Header.Add(echo.HeaderContentType, w.FormDataContentType())
532
533 return req
534 }
535
536 var testCases = []struct {
537 name string
538 givenRequest *http.Request
539 whenName string
540 whenLimit uint
541 expectValues []string
542 expectError string
543 }{
544 {
545 name: "ok, POST form, single value",
546 givenRequest: examplePostFormRequest(nil),
547 whenName: "emails[]",
548 expectValues: []string{"jon@labstack.com"},

Callers

nothing calls this directly

Calls 8

valuesFromFormFunction · 0.85
SetMethod · 0.80
NewContextMethod · 0.80
AddMethod · 0.65
WriteMethod · 0.45
CloseMethod · 0.45
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…