github.com/blend/go-sdk@v1.20220411.3/webutil/no_follow_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 webutil 9 10 import ( 11 "fmt" 12 "net/http" 13 "net/http/httptest" 14 "testing" 15 16 "github.com/blend/go-sdk/assert" 17 ) 18 19 func TestNoFollowRedirects(t *testing.T) { 20 assert := assert.New(t) 21 22 assert.Equal(http.ErrUseLastResponse, NoFollowRedirects()(nil, nil)) 23 24 second := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 25 w.WriteHeader(http.StatusOK) 26 fmt.Fprint(w, "OK!") 27 })) 28 defer second.Close() 29 30 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 31 http.Redirect(w, r, second.URL, http.StatusTemporaryRedirect) 32 })) 33 defer server.Close() 34 35 client := http.Client{ 36 CheckRedirect: NoFollowRedirects(), 37 } 38 39 res, err := client.Get(server.URL) 40 assert.Nil(err) 41 defer res.Body.Close() 42 assert.Equal(307, res.StatusCode, "the redirect status code should be returned by the server") 43 }