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

Method ToContextRecorder

echotest/context.go:81–164  ·  view source on GitHub ↗

ToContextRecorder converts ContextConfig to echo.Context and httptest.ResponseRecorder

(t *testing.T)

Source from the content-addressed store, hash-verified

79
80// ToContextRecorder converts ContextConfig to echo.Context and httptest.ResponseRecorder
81func (conf ContextConfig) ToContextRecorder(t *testing.T) (*echo.Context, *httptest.ResponseRecorder) {
82 if conf.Response == nil {
83 conf.Response = httptest.NewRecorder()
84 }
85 isDefaultRequest := false
86 if conf.Request == nil {
87 isDefaultRequest = true
88 conf.Request = httptest.NewRequest(http.MethodGet, "/", nil)
89 }
90
91 if len(conf.QueryValues) > 0 {
92 conf.Request.URL.RawQuery = conf.QueryValues.Encode()
93 }
94 if len(conf.Headers) > 0 {
95 conf.Request.Header = conf.Headers
96 }
97 if len(conf.FormValues) > 0 {
98 body := strings.NewReader(url.Values(conf.FormValues).Encode())
99 conf.Request.Body = io.NopCloser(body)
100 conf.Request.ContentLength = int64(body.Len())
101
102 if conf.Request.Header.Get(echo.HeaderContentType) == "" {
103 conf.Request.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
104 }
105 if isDefaultRequest {
106 conf.Request.Method = http.MethodPost
107 }
108 } else if conf.MultipartForm != nil {
109 var body bytes.Buffer
110 mw := multipart.NewWriter(&body)
111 for field, value := range conf.MultipartForm.Fields {
112 if err := mw.WriteField(field, value); err != nil {
113 t.Fatal(err)
114 }
115 }
116 for _, file := range conf.MultipartForm.Files {
117 fw, err := mw.CreateFormFile(file.Fieldname, file.Filename)
118 if err != nil {
119 t.Fatal(err)
120 }
121 if _, err = fw.Write(file.Content); err != nil {
122 t.Fatal(err)
123 }
124 }
125 if err := mw.Close(); err != nil {
126 t.Fatal(err)
127 }
128
129 conf.Request.Body = io.NopCloser(&body)
130 conf.Request.ContentLength = int64(body.Len())
131 if conf.Request.Header.Get(echo.HeaderContentType) == "" {
132 conf.Request.Header.Set(echo.HeaderContentType, mw.FormDataContentType())
133 }
134 if isDefaultRequest {
135 conf.Request.Method = http.MethodPost
136 }
137 } else if conf.JSONBody != nil {
138 body := bytes.NewReader(conf.JSONBody)

Callers 2

ToContextMethod · 0.95
ServeWithHandlerMethod · 0.95

Calls 6

InitializeRouteMethod · 0.95
SetMethod · 0.80
NewContextMethod · 0.80
GetMethod · 0.45
WriteMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected