github.com/qiniu/go-sdk/v7@v7.27.0/internal/clientv2/interceptor_auth_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package clientv2 5 6 import ( 7 "net/http" 8 "strings" 9 "testing" 10 11 "github.com/qiniu/go-sdk/v7/auth" 12 clientV1 "github.com/qiniu/go-sdk/v7/client" 13 ) 14 15 func TestAuthInterceptor(t *testing.T) { 16 clientV1.DebugMode = true 17 defer func() { 18 clientV1.DebugMode = false 19 }() 20 21 interceptor := NewAuthInterceptor(AuthConfig{ 22 Credentials: auth.New("ak", "sk"), 23 TokenType: auth.TokenQiniu, 24 BeforeSign: func(req *http.Request) { 25 if authorization := req.Header.Get("Authorization"); authorization != "" { 26 t.Fatal("Authorization header should be empty") 27 } 28 }, 29 AfterSign: func(req *http.Request) { 30 if authorization := req.Header.Get("Authorization"); authorization == "" { 31 t.Fatal("Authorization header should not be empty") 32 } else if !strings.HasPrefix(authorization, "Qiniu ak:") { 33 t.Fatal("Unexpected Authorization header") 34 } 35 }, 36 }) 37 c := NewClient(&testClient{statusCode: http.StatusOK}, interceptor) 38 resp, err := Do(c, RequestParams{ 39 Context: nil, 40 Method: RequestMethodGet, 41 Url: "https://test.qiniu.com/path/123", 42 Header: nil, 43 GetBody: nil, 44 }) 45 if err != nil { 46 t.Fatal(err) 47 } 48 if resp.StatusCode != http.StatusOK { 49 t.Fatal("status code not 200") 50 } 51 }