github.com/blend/go-sdk@v1.20220411.3/r2/opt_max_redirects_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package r2
     9  
    10  import (
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestOptMaxRedirects(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	var pingURL, pongURL string
    22  	var pingCount, pongCount int
    23  	ping := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    24  		pingCount++
    25  		http.Redirect(rw, r, pongURL, http.StatusTemporaryRedirect)
    26  	}))
    27  	defer ping.Close()
    28  
    29  	pong := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    30  		pongCount++
    31  		http.Redirect(rw, r, pingURL, http.StatusTemporaryRedirect)
    32  	}))
    33  	defer pong.Close()
    34  
    35  	pingURL = ping.URL
    36  	pongURL = pong.URL
    37  
    38  	res, err := New(pingURL, OptMaxRedirects(32)).Discard()
    39  	assert.Nil(res)
    40  	assert.True(ErrIsTooManyRedirects(err))
    41  	assert.Equal(32, pingCount+pongCount)
    42  }