@@ -18,6 +18,7 @@ package ffapi
1818
1919import (
2020 "context"
21+ "encoding/json"
2122 "fmt"
2223 "io"
2324 "net/http"
@@ -112,6 +113,29 @@ var utAPIRoute2 = &Route{
112113 },
113114}
114115
116+ type testInputStruct struct {
117+ Input1 string `json:"input1,omitempty"`
118+ }
119+
120+ var utAPIRoute3 = & Route {
121+ Name : "utAPIRoute3" ,
122+ Path : "ut/utresource/{resourceid}/postbatch" ,
123+ Method : http .MethodPost ,
124+ Description : "post an array to check arrays go through ok" ,
125+ JSONInputDecoder : func (req * http.Request , body io.Reader ) (interface {}, error ) {
126+ var arrayInput []* testInputStruct
127+ err := json .NewDecoder (body ).Decode (& arrayInput )
128+ return arrayInput , err
129+ },
130+ JSONInputValue : func () interface {} { return []* testInputStruct {} },
131+ JSONOutputValue : func () interface {} { return []* testInputStruct {} },
132+ Extensions : & APIServerRouteExt [* utManager ]{
133+ JSONHandler : func (a * APIRequest , um * utManager ) (output interface {}, err error ) {
134+ return a .Input .([]* testInputStruct ), nil
135+ },
136+ },
137+ }
138+
115139func initUTConfig () (config.Section , config.Section , config.Section ) {
116140 config .RootConfigReset ()
117141 apiConfig := config .RootSection ("ut.api" )
@@ -129,7 +153,7 @@ func newTestAPIServer(t *testing.T, start bool) (*utManager, *apiServer[*utManag
129153 um := & utManager {t : t }
130154 as := NewAPIServer (ctx , APIServerOptions [* utManager ]{
131155 MetricsRegistry : metric .NewPrometheusMetricsRegistry ("ut" ),
132- Routes : []* Route {utAPIRoute1 , utAPIRoute2 },
156+ Routes : []* Route {utAPIRoute1 , utAPIRoute2 , utAPIRoute3 },
133157 EnrichRequest : func (r * APIRequest ) (* utManager , error ) {
134158 // This could be some dynamic object based on extra processing in the request,
135159 // but the most common case is you just have a "manager" that you inject into each
@@ -176,6 +200,30 @@ func TestAPIServerInvokeAPIRouteStream(t *testing.T) {
176200 assert .Equal (t , "a stream!" , string (res .Body ()))
177201}
178202
203+ func TestAPIServerInvokeAPIPostEmptyArray (t * testing.T ) {
204+ _ , as , done := newTestAPIServer (t , true )
205+ defer done ()
206+
207+ <- as .Started ()
208+
209+ var o []* testInputStruct
210+ res , err := resty .New ().R ().
211+ SetBody ([]* testInputStruct {}).
212+ SetResult (& o ).
213+ Post (fmt .Sprintf ("%s/api/v1/ut/utresource/id12345/postbatch" , as .APIPublicURL ()))
214+ assert .NoError (t , err )
215+ assert .Equal (t , 200 , res .StatusCode ())
216+ assert .Equal (t , []* testInputStruct {}, o )
217+
218+ res , err = resty .New ().R ().
219+ SetBody ([]* testInputStruct {{Input1 : "in1" }}).
220+ SetResult (& o ).
221+ Post (fmt .Sprintf ("%s/api/v1/ut/utresource/id12345/postbatch" , as .APIPublicURL ()))
222+ assert .NoError (t , err )
223+ assert .Equal (t , 200 , res .StatusCode ())
224+ assert .Equal (t , []* testInputStruct {{Input1 : "in1" }}, o )
225+ }
226+
179227func TestAPIServerInvokeAPIRouteLiveness (t * testing.T ) {
180228 _ , as , done := newTestAPIServer (t , true )
181229 defer done ()
0 commit comments