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

Function testImpl

internal/test/parameters/param_roundtrip_test.go:95–517  ·  view source on GitHub ↗

testImpl runs the full parameter roundtrip test suite against any http.Handler. The generated client serializes Go values into an HTTP request, the server deserializes them and echoes them back as JSON, and we compare the response body against the original values.

(t *testing.T, handler http.Handler)

Source from the content-addressed store, hash-verified

93// deserializes them and echoes them back as JSON, and we compare the response
94// body against the original values.
95func testImpl(t *testing.T, handler http.Handler) {
96 t.Helper()
97
98 server := "http://example.com"
99
100 expectedObject := paramclient.Object{
101 FirstName: "Alex",
102 Role: "admin",
103 }
104
105 expectedComplexObject := paramclient.ComplexObject{
106 Object: expectedObject,
107 Id: 12345,
108 IsAdmin: true,
109 }
110
111 expectedArray := []int32{3, 4, 5}
112
113 var expectedPrimitive int32 = 5
114
115 // doRoundTrip sends a request to the handler, asserts 200, and decodes the JSON response.
116 doRoundTrip := func(t *testing.T, req *http.Request, target interface{}) {
117 t.Helper()
118 // The generated client produces requests via http.NewRequest which
119 // leaves RequestURI empty. Some adapters (notably Fiber) need it
120 // set to route correctly.
121 req.RequestURI = req.URL.RequestURI()
122 rec := httptest.NewRecorder()
123 handler.ServeHTTP(rec, req)
124 if !assert.Equal(t, http.StatusOK, rec.Code, "server returned %d; body: %s", rec.Code, rec.Body.String()) {
125 return
126 }
127 if target != nil {
128 require.NoError(t, json.NewDecoder(rec.Body).Decode(target), "failed to decode response body")
129 }
130 }
131
132 // =========================================================================
133 // Path Parameters
134 // =========================================================================
135 t.Run("path", func(t *testing.T) {
136 t.Run("simple", func(t *testing.T) {
137 t.Run("primitive", func(t *testing.T) {
138 req, err := paramclient.NewGetSimplePrimitiveRequest(server, expectedPrimitive)
139 require.NoError(t, err)
140 var got int32
141 doRoundTrip(t, req, &got)
142 assert.Equal(t, expectedPrimitive, got)
143 })
144
145 t.Run("primitive explode", func(t *testing.T) {
146 req, err := paramclient.NewGetSimpleExplodePrimitiveRequest(server, expectedPrimitive)
147 require.NoError(t, err)
148 var got int32
149 doRoundTrip(t, req, &got)
150 assert.Equal(t, expectedPrimitive, got)
151 })
152

Calls 2

RunMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…