@@ -1779,80 +1779,91 @@ func TestResponseBody(t *testing.T) {
17791779}
17801780
17811781func testResponseBody (t * testing.T , port int ) {
1782- tests := []struct {
1783- name string
1784- url string
1785- wantStatus int
1786- wantBody string
1787- }{{
1788- name : "unary case" ,
1789- url : "http://localhost:%d/responsebody/foo" ,
1790- wantStatus : http .StatusOK ,
1791- wantBody : `{"data":"foo"}` ,
1792- }}
1793-
1794- for _ , tt := range tests {
1795- t .Run (tt .name , func (t * testing.T ) {
1796- apiURL := fmt .Sprintf (tt .url , port )
1797- resp , err := http .Get (apiURL )
1798- if err != nil {
1799- t .Fatalf ("http.Get(%q) failed with %v; want success" , apiURL , err )
1800- }
1782+ apiURL := fmt .Sprintf ("http://localhost:%d/responsebody/foo" , port )
1783+ resp , err := http .Get (apiURL )
1784+ if err != nil {
1785+ t .Fatalf ("http.Get(%q) failed with %v; want success" , apiURL , err )
1786+ }
18011787
1802- defer resp .Body .Close ()
1803- buf , err := io .ReadAll (resp .Body )
1804- if err != nil {
1805- t .Fatalf ("io.ReadAll(resp.Body) failed with %v; want success" , err )
1806- }
1788+ defer resp .Body .Close ()
1789+ buf , err := io .ReadAll (resp .Body )
1790+ if err != nil {
1791+ t .Fatalf ("io.ReadAll(resp.Body) failed with %v; want success" , err )
1792+ }
18071793
1808- if got , want := resp .StatusCode , tt . wantStatus ; got != want {
1809- t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
1810- t .Logf ("%s" , buf )
1811- }
1794+ if got , want := resp .StatusCode , http . StatusOK ; got != want {
1795+ t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
1796+ t .Logf ("%s" , buf )
1797+ }
18121798
1813- if got , want := string (buf ), tt .wantBody ; got != want {
1814- t .Errorf ("response = %q; want %q" , got , want )
1815- }
1816- })
1799+ if diff := cmp .Diff (string (buf ), `{"data":"foo"}` ); diff != "" {
1800+ t .Errorf (diff )
18171801 }
18181802}
18191803
18201804func TestResponseBodyStream (t * testing.T ) {
1821- tests := []struct {
1822- name string
1823- url string
1824- wantStatus int
1825- wantBody []string
1826- }{{
1827- name : "stream case" ,
1828- url : "http://localhost:%d/responsebody/stream/foo" ,
1829- wantStatus : http .StatusOK ,
1830- wantBody : []string {`{"result":{"data":"first foo"}}` , `{"result":{"data":"second foo"}}` },
1831- }}
1805+ apiURL := "http://localhost:8088/responsebody/stream/foo"
1806+ resp , err := http .Get (apiURL )
1807+ if err != nil {
1808+ t .Fatalf ("http.Get(%q) failed with %v; want success" , apiURL , err )
1809+ }
18321810
1833- port := 8088
1834- for _ , tt := range tests {
1835- t .Run (tt .name , func (t * testing.T ) {
1836- apiURL := fmt .Sprintf (tt .url , port )
1837- resp , err := http .Get (apiURL )
1838- if err != nil {
1839- t .Fatalf ("http.Get(%q) failed with %v; want success" , apiURL , err )
1840- }
1811+ defer resp .Body .Close ()
1812+ body , err := readAll (resp .Body )
1813+ if err != nil {
1814+ t .Fatalf ("readAll(resp.Body) failed with %v; want success" , err )
1815+ }
18411816
1842- defer resp .Body .Close ()
1843- body , err := readAll (resp .Body )
1844- if err != nil {
1845- t .Fatalf ("readAll(resp.Body) failed with %v; want success" , err )
1846- }
1817+ if got , want := resp .StatusCode , http .StatusOK ; got != want {
1818+ t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
1819+ }
18471820
1848- if got , want := resp .StatusCode , tt .wantStatus ; got != want {
1849- t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
1850- }
1821+ if diff := cmp .Diff (body , []string {`{"result":{"data":"first foo"}}` , `{"result":{"data":"second foo"}}` }); diff != "" {
1822+ t .Errorf (diff )
1823+ }
1824+ }
18511825
1852- if ! reflect .DeepEqual (tt .wantBody , body ) {
1853- t .Errorf ("response = %v; want %v" , body , tt .wantBody )
1854- }
1855- })
1826+ func TestResponseBodyStreamHttpBody (t * testing.T ) {
1827+ apiURL := "http://localhost:8088/v1/example/download"
1828+ resp , err := http .Get (apiURL )
1829+ if err != nil {
1830+ t .Fatalf ("http.Get(%q) failed with %v; want success" , apiURL , err )
1831+ }
1832+
1833+ defer resp .Body .Close ()
1834+ body , err := readAll (resp .Body )
1835+ if err != nil {
1836+ t .Fatalf ("readAll(resp.Body) failed with %v; want success" , err )
1837+ }
1838+
1839+ if got , want := resp .StatusCode , http .StatusOK ; got != want {
1840+ t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
1841+ }
1842+
1843+ if diff := cmp .Diff (body , []string {"Hello 1" , "Hello 2" }); diff != "" {
1844+ t .Errorf (diff )
1845+ }
1846+ }
1847+
1848+ func TestResponseBodyStreamHttpBodyError (t * testing.T ) {
1849+ apiURL := "http://localhost:8088/v1/example/download?error=true"
1850+ resp , err := http .Get (apiURL )
1851+ if err != nil {
1852+ t .Fatalf ("http.Get(%q) failed with %v; want success" , apiURL , err )
1853+ }
1854+
1855+ defer resp .Body .Close ()
1856+ body , err := readAll (resp .Body )
1857+ if err != nil {
1858+ t .Fatalf ("readAll(resp.Body) failed with %v; want success" , err )
1859+ }
1860+
1861+ if got , want := resp .StatusCode , http .StatusOK ; got != want {
1862+ t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
1863+ }
1864+
1865+ if diff := cmp .Diff (body , []string {"Hello 1" , "Hello 2" , `{"error":{"code":3,"message":"error","details":[]}}` }); diff != "" {
1866+ t .Errorf (diff )
18561867 }
18571868}
18581869
0 commit comments