MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / testImpl

Function testImpl

internal/test/strict-server/strict_test.go:63–267  ·  view source on GitHub ↗
(t *testing.T, handler http.Handler)

Source from the content-addressed store, hash-verified

61}
62
63func testImpl(t *testing.T, handler http.Handler) {
64 t.Run("JSONExample", func(t *testing.T) {
65 value := "123"
66 requestBody := clientAPI.Example{Value: &value}
67 rr := testutil.NewRequest().Post("/json").WithJsonBody(requestBody).GoWithHTTPHandler(t, handler).Recorder
68 assert.Equal(t, http.StatusOK, rr.Code)
69 assert.True(t, strings.HasPrefix(rr.Header().Get("Content-Type"), "application/json"))
70 var responseBody clientAPI.Example
71 err := json.NewDecoder(rr.Body).Decode(&responseBody)
72 assert.NoError(t, err)
73 assert.Equal(t, requestBody, responseBody)
74 })
75 t.Run("URLEncodedExample", func(t *testing.T) {
76 value := "456"
77 requestBody := clientAPI.Example{Value: &value}
78 requestBodyEncoded, err := runtime.MarshalForm(&requestBody, nil)
79 assert.NoError(t, err)
80 rr := testutil.NewRequest().Post("/urlencoded").WithContentType("application/x-www-form-urlencoded").WithBody([]byte(requestBodyEncoded.Encode())).GoWithHTTPHandler(t, handler).Recorder
81 assert.Equal(t, http.StatusOK, rr.Code)
82 assert.Equal(t, "application/x-www-form-urlencoded", rr.Header().Get("Content-Type"))
83 values, err := url.ParseQuery(rr.Body.String())
84 assert.NoError(t, err)
85 var responseBody clientAPI.Example
86 err = runtime.BindForm(&responseBody, values, nil, nil)
87 assert.NoError(t, err)
88 assert.Equal(t, requestBody, responseBody)
89 })
90 t.Run("MultipartExample", func(t *testing.T) {
91 value := "789"
92 fieldName := "value"
93 var writer bytes.Buffer
94 mw := multipart.NewWriter(&writer)
95 field, err := mw.CreateFormField(fieldName)
96 assert.NoError(t, err)
97 _, _ = field.Write([]byte(value))
98 assert.NoError(t, mw.Close())
99 rr := testutil.NewRequest().Post("/multipart").WithContentType(mw.FormDataContentType()).WithBody(writer.Bytes()).GoWithHTTPHandler(t, handler).Recorder
100 assert.Equal(t, http.StatusOK, rr.Code)
101 contentType, params, err := mime.ParseMediaType(rr.Header().Get("Content-Type"))
102 assert.NoError(t, err)
103 assert.Equal(t, "multipart/form-data", contentType)
104 reader := multipart.NewReader(rr.Body, params["boundary"])
105 part, err := reader.NextPart()
106 assert.NoError(t, err)
107 assert.Equal(t, part.FormName(), fieldName)
108 readValue, err := io.ReadAll(part)
109 assert.NoError(t, err)
110 assert.Equal(t, value, string(readValue))
111 _, err = reader.NextPart()
112 assert.Equal(t, io.EOF, err)
113 })
114 t.Run("MultipartRelatedExample", func(t *testing.T) {
115 value := "789"
116 fieldName := "value"
117 var writer bytes.Buffer
118 mw := multipart.NewWriter(&writer)
119 field, err := mw.CreateFormField(fieldName)
120 assert.NoError(t, err)

Callers 4

TestIrisServerFunction · 0.70
TestChiServerFunction · 0.70
TestEchoServerFunction · 0.70
TestGinServerFunction · 0.70

Calls 4

RunMethod · 0.80
GetMethod · 0.45
StringMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected