@@ -18,9 +18,14 @@ import (
18
18
"bytes"
19
19
"encoding/json"
20
20
"encoding/xml"
21
+ "errors"
22
+ "io"
21
23
"io/ioutil"
22
24
"mime"
23
25
"mime/multipart"
26
+ "net/http"
27
+ "net/http/httptest"
28
+ "net/url"
24
29
"os"
25
30
"path/filepath"
26
31
"strings"
@@ -29,6 +34,7 @@ import (
29
34
"github.com/go-openapi/runtime"
30
35
"github.com/go-openapi/strfmt"
31
36
"github.com/stretchr/testify/assert"
37
+ "github.com/stretchr/testify/require"
32
38
)
33
39
34
40
var testProducers = map [string ]runtime.Producer {
@@ -509,3 +515,78 @@ func TestBuildRequest_BuildHTTP_EscapedPath(t *testing.T) {
509
515
assert .Equal (t , runtime .JSONMime , req .Header .Get (runtime .HeaderContentType ))
510
516
}
511
517
}
518
+
519
+ type testReqFn func (* testing.T , * http.Request )
520
+
521
+ type testRoundTripper struct {
522
+ tr http.RoundTripper
523
+ testFn testReqFn
524
+ testHarness * testing.T
525
+ }
526
+
527
+ func (t * testRoundTripper ) RoundTrip (req * http.Request ) (resp * http.Response , err error ) {
528
+ t .testFn (t .testHarness , req )
529
+ return t .tr .RoundTrip (req )
530
+ }
531
+
532
+ func TestGetBodyCallsBeforeRoundTrip (t * testing.T ) {
533
+
534
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
535
+ rw .WriteHeader (http .StatusCreated )
536
+ rw .Write ([]byte ("test result" ))
537
+ }))
538
+ defer server .Close ()
539
+ hu , _ := url .Parse (server .URL )
540
+
541
+ client := http .DefaultClient
542
+ transport := http .DefaultTransport
543
+
544
+ client .Transport = & testRoundTripper {
545
+ tr : transport ,
546
+ testHarness : t ,
547
+ testFn : func (t * testing.T , req * http.Request ) {
548
+ // Read the body once before sending the request
549
+ body , err := req .GetBody ()
550
+ require .NoError (t , err )
551
+ bodyContent , err := ioutil .ReadAll (io .Reader (body ))
552
+ require .EqualValues (t , req .ContentLength , len (bodyContent ))
553
+ require .EqualValues (t , "\" test body\" \n " , string (bodyContent ))
554
+
555
+ // Read the body a second time before sending the request
556
+ body , err = req .GetBody ()
557
+ require .NoError (t , err )
558
+ bodyContent , err = ioutil .ReadAll (io .Reader (body ))
559
+ require .EqualValues (t , req .ContentLength , len (bodyContent ))
560
+ require .EqualValues (t , "\" test body\" \n " , string (bodyContent ))
561
+ },
562
+ }
563
+
564
+ rwrtr := runtime .ClientRequestWriterFunc (func (req runtime.ClientRequest , _ strfmt.Registry ) error {
565
+ return req .SetBodyParam ("test body" )
566
+ })
567
+
568
+ operation := & runtime.ClientOperation {
569
+ ID : "getSites" ,
570
+ Method : "POST" ,
571
+ PathPattern : "/" ,
572
+ Params : rwrtr ,
573
+ Client : client ,
574
+ Reader : runtime .ClientResponseReaderFunc (func (response runtime.ClientResponse , consumer runtime.Consumer ) (interface {}, error ) {
575
+ if response .Code () == http .StatusCreated {
576
+ var result string
577
+ if err := consumer .Consume (response .Body (), & result ); err != nil {
578
+ return nil , err
579
+ }
580
+ return result , nil
581
+ }
582
+ return nil , errors .New ("Unexpected error code" )
583
+ }),
584
+ }
585
+
586
+ openAPIClient := New (hu .Host , "/" , []string {"http" })
587
+ res , err := openAPIClient .Submit (operation )
588
+
589
+ require .NoError (t , err )
590
+ actual := res .(string )
591
+ require .EqualValues (t , "test result" , actual )
592
+ }
0 commit comments