@@ -1566,6 +1566,56 @@ func TestClientFollowRedirects(t *testing.T) {
1566
1566
ReleaseResponse (resp )
1567
1567
}
1568
1568
1569
+ for i := 0 ; i < 10 ; i ++ {
1570
+ req := AcquireRequest ()
1571
+ resp := AcquireResponse ()
1572
+
1573
+ req .SetRequestURI ("http://xxx/foo" )
1574
+
1575
+ req .SetTimeout (time .Second )
1576
+ err := c .DoRedirects (req , resp , 16 )
1577
+ if err != nil {
1578
+ t .Fatalf ("unexpected error: %v" , err )
1579
+ }
1580
+
1581
+ if statusCode := resp .StatusCode (); statusCode != StatusOK {
1582
+ t .Fatalf ("unexpected status code: %d" , statusCode )
1583
+ }
1584
+
1585
+ if body := string (resp .Body ()); body != "/bar" {
1586
+ t .Fatalf ("unexpected response %q. Expecting %q" , body , "/bar" )
1587
+ }
1588
+
1589
+ ReleaseRequest (req )
1590
+ ReleaseResponse (resp )
1591
+ }
1592
+
1593
+ for i := 0 ; i < 10 ; i ++ {
1594
+ req := AcquireRequest ()
1595
+ resp := AcquireResponse ()
1596
+
1597
+ req .SetRequestURI ("http://xxx/foo" )
1598
+
1599
+ testConn , _ := net .Dial ("tcp" , ln .Addr ().String ())
1600
+ timeoutConn := & Client {
1601
+ Dial : func (addr string ) (net.Conn , error ) {
1602
+ return & readTimeoutConn {Conn : testConn , t : time .Second }, nil
1603
+ },
1604
+ }
1605
+
1606
+ req .SetTimeout (time .Millisecond )
1607
+ err := timeoutConn .DoRedirects (req , resp , 16 )
1608
+ if err == nil {
1609
+ t .Errorf ("expecting error" )
1610
+ }
1611
+ if err != ErrTimeout {
1612
+ t .Errorf ("unexpected error: %v. Expecting %v" , err , ErrTimeout )
1613
+ }
1614
+
1615
+ ReleaseRequest (req )
1616
+ ReleaseResponse (resp )
1617
+ }
1618
+
1569
1619
req := AcquireRequest ()
1570
1620
resp := AcquireResponse ()
1571
1621
@@ -1613,6 +1663,7 @@ func TestClientDoTimeoutSuccess(t *testing.T) {
1613
1663
defer s .Stop ()
1614
1664
1615
1665
testClientDoTimeoutSuccess (t , & defaultClient , "http://" + s .Addr (), 100 )
1666
+ testClientRequestSetTimeoutSuccess (t , & defaultClient , "http://" + s .Addr (), 100 )
1616
1667
}
1617
1668
1618
1669
func TestClientDoTimeoutSuccessConcurrent (t * testing.T ) {
@@ -1627,6 +1678,7 @@ func TestClientDoTimeoutSuccessConcurrent(t *testing.T) {
1627
1678
go func () {
1628
1679
defer wg .Done ()
1629
1680
testClientDoTimeoutSuccess (t , & defaultClient , "http://" + s .Addr (), 100 )
1681
+ testClientRequestSetTimeoutSuccess (t , & defaultClient , "http://" + s .Addr (), 100 )
1630
1682
}()
1631
1683
}
1632
1684
wg .Wait ()
@@ -1687,6 +1739,7 @@ func TestClientDoTimeoutError(t *testing.T) {
1687
1739
}
1688
1740
1689
1741
testClientDoTimeoutError (t , c , 100 )
1742
+ testClientRequestSetTimeoutError (t , c , 100 )
1690
1743
}
1691
1744
1692
1745
func TestClientDoTimeoutErrorConcurrent (t * testing.T ) {
@@ -1748,6 +1801,22 @@ func testClientGetTimeoutError(t *testing.T, c *Client, n int) {
1748
1801
}
1749
1802
}
1750
1803
1804
+ func testClientRequestSetTimeoutError (t * testing.T , c * Client , n int ) {
1805
+ var req Request
1806
+ var resp Response
1807
+ req .SetRequestURI ("http://foobar.com/baz" )
1808
+ for i := 0 ; i < n ; i ++ {
1809
+ req .SetTimeout (time .Millisecond )
1810
+ err := c .Do (& req , & resp )
1811
+ if err == nil {
1812
+ t .Errorf ("expecting error" )
1813
+ }
1814
+ if err != ErrTimeout {
1815
+ t .Errorf ("unexpected error: %v. Expecting %v" , err , ErrTimeout )
1816
+ }
1817
+ }
1818
+ }
1819
+
1751
1820
type readTimeoutConn struct {
1752
1821
net.Conn
1753
1822
t time.Duration
@@ -2398,6 +2467,30 @@ func testClientDoTimeoutSuccess(t *testing.T, c *Client, addr string, n int) {
2398
2467
}
2399
2468
}
2400
2469
2470
+ func testClientRequestSetTimeoutSuccess (t * testing.T , c * Client , addr string , n int ) {
2471
+ var req Request
2472
+ var resp Response
2473
+
2474
+ for i := 0 ; i < n ; i ++ {
2475
+ uri := fmt .Sprintf ("%s/foo/%d?bar=baz" , addr , i )
2476
+ req .SetRequestURI (uri )
2477
+ req .SetTimeout (time .Second )
2478
+ if err := c .Do (& req , & resp ); err != nil {
2479
+ t .Errorf ("unexpected error: %v" , err )
2480
+ }
2481
+ if resp .StatusCode () != StatusOK {
2482
+ t .Errorf ("unexpected status code: %d. Expecting %d" , resp .StatusCode (), StatusOK )
2483
+ }
2484
+ resultURI := string (resp .Body ())
2485
+ if strings .HasPrefix (uri , "https" ) {
2486
+ resultURI = uri [:5 ] + resultURI [4 :]
2487
+ }
2488
+ if resultURI != uri {
2489
+ t .Errorf ("unexpected uri %q. Expecting %q" , resultURI , uri )
2490
+ }
2491
+ }
2492
+ }
2493
+
2401
2494
func testClientGetTimeoutSuccess (t * testing.T , c * Client , addr string , n int ) {
2402
2495
var buf []byte
2403
2496
for i := 0 ; i < n ; i ++ {
0 commit comments