Skip to content

Commit 110c91f

Browse files
Add before send option
1 parent 60a591d commit 110c91f

5 files changed

Lines changed: 70 additions & 3 deletions

File tree

api/admin/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ func (a *API) executeRequest(ctx context.Context, method string, path interface{
198198

199199
req = req.WithContext(ctx)
200200

201+
if a.Config.API.BeforeSend != nil {
202+
a.Config.API.BeforeSend(req)
203+
}
201204
resp, err := a.Client.Do(req)
202205
if err != nil {
203206
return nil, err

api/admin/api_acceptance_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package admin_test
55
import (
66
"context"
77
"fmt"
8+
"net/http"
9+
"reflect"
10+
"testing"
11+
812
"github.com/cloudinary/cloudinary-go/v2/api"
913
"github.com/cloudinary/cloudinary-go/v2/api/admin"
1014
"github.com/cloudinary/cloudinary-go/v2/config"
1115
"github.com/cloudinary/cloudinary-go/v2/internal/cldtest"
12-
"reflect"
13-
"testing"
1416
)
1517

1618
var oAuthTokenConfig, _ = config.NewFromOAuthToken(cldtest.CloudName, "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4")
@@ -148,10 +150,34 @@ func getAuthorizationTestCases() []AdminAPIAcceptanceTestCase {
148150
}
149151
}
150152

153+
// Acceptance test cases for before send
154+
func getBeforeSendTestCases() []AdminAPIAcceptanceTestCase {
155+
return []AdminAPIAcceptanceTestCase{
156+
{
157+
Name: "Ping Test Before Send",
158+
RequestTest: func(api *admin.API, ctx context.Context) (interface{}, error) {
159+
api.Config.API.BeforeSend = func(req *http.Request) {
160+
req.Header.Set("X-Test-Header", "test")
161+
}
162+
return api.Ping(ctx)
163+
},
164+
ResponseTest: func(response interface{}, t *testing.T) {},
165+
ExpectedRequest: cldtest.ExpectedRequestParams{
166+
Method: "GET",
167+
URI: "/ping",
168+
Headers: &map[string]string{"X-Test-Header": "test"},
169+
},
170+
JsonResponse: "{\"status\": \"OK\"}",
171+
ExpectedCallCount: 1,
172+
},
173+
}
174+
}
175+
151176
// Run tests
152177
func TestAdminAPI_Acceptance(t *testing.T) {
153178
t.Parallel()
154179
testAdminAPIByTestCases(getPingTestCases(), t)
155180
testAdminAPIByTestCases(getUserAgentTestCases(), t)
156181
testAdminAPIByTestCases(getAuthorizationTestCases(), t)
182+
testAdminAPIByTestCases(getBeforeSendTestCases(), t)
157183
}

api/uploader/upload.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ func (u *API) postBody(ctx context.Context, urlPath interface{}, bodyReader io.R
350350

351351
req = req.WithContext(ctx)
352352

353+
if u.Config.API.BeforeSend != nil {
354+
u.Config.API.BeforeSend(req)
355+
}
353356
resp, err := u.Client.Do(req)
354357
if err != nil {
355358
return nil, err

api/uploader/upload_acceptance_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package uploader_test
55
import (
66
"context"
77
"fmt"
8+
"net/http"
9+
"testing"
10+
811
"github.com/cloudinary/cloudinary-go/v2/api"
912
"github.com/cloudinary/cloudinary-go/v2/api/uploader"
1013
"github.com/cloudinary/cloudinary-go/v2/config"
1114
"github.com/cloudinary/cloudinary-go/v2/internal/cldtest"
1215
"github.com/cloudinary/cloudinary-go/v2/internal/signature"
13-
"testing"
1416
)
1517

1618
var oAuthTokenConfig, _ = config.NewFromOAuthToken(cldtest.CloudName, "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4")
@@ -186,6 +188,35 @@ func getUploadConfigTestCases() []UploadAPIAcceptanceTestCase {
186188
}
187189
}
188190

191+
// Acceptance test cases for before send
192+
func getBeforeSendTestCases() []UploadAPIAcceptanceTestCase {
193+
body := "file=data%3Aimage%2Fgif%3Bbase64%2CR0lGODlhAQABAIAAAAAAAP%2F%2F%2FyH5BAEAAAAALAAAAAABAAEAAAIBRAA7" +
194+
"&timestamp=123456789&unsigned=true"
195+
196+
return []UploadAPIAcceptanceTestCase{
197+
{
198+
Name: "Upload Test Before Send",
199+
RequestTest: func(uploadAPI *uploader.API, ctx context.Context) (interface{}, error) {
200+
uploadAPI.Config.API.BeforeSend = func(req *http.Request) {
201+
req.Header.Set("X-Test-Header", "test")
202+
}
203+
return uploadAPI.Upload(ctx, cldtest.Base64Image, uploader.UploadParams{
204+
Timestamp: 123456789,
205+
Unsigned: api.Bool(true),
206+
})
207+
},
208+
ResponseTest: func(response interface{}, t *testing.T) {},
209+
ExpectedRequest: cldtest.ExpectedRequestParams{
210+
Method: "POST",
211+
URI: "/auto/upload",
212+
Body: &body,
213+
Headers: &map[string]string{"X-Test-Header": "test"},
214+
},
215+
ExpectedCallCount: 1,
216+
},
217+
}
218+
}
219+
189220
// Run tests
190221
func TestUploadAPI_Acceptance(t *testing.T) {
191222
t.Parallel()
@@ -195,4 +226,5 @@ func TestUploadAPI_Acceptance(t *testing.T) {
195226
testUploadAPIByTestCases(getBooleanValuesTestCases(), t)
196227
testUploadAPIByTestCases(getVariousValuesTestCases(), t)
197228
testUploadAPIByTestCases(getUploadConfigTestCases(), t)
229+
testUploadAPIByTestCases(getBeforeSendTestCases(), t)
198230
}

config/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package config
22

3+
import "net/http"
4+
35
// API defines the configuration for making requests to the Cloudinary API.
46
type API struct {
57
UploadPrefix string `schema:"upload_prefix" default:"https://api.cloudinary.com"`
68
Timeout int64 `schema:"timeout" default:"60"` // seconds
79
UploadTimeout int64 `schema:"upload_timeout"`
810
ChunkSize int64 `schema:"chunk_size" default:"20000000"` // bytes
11+
BeforeSend func(req *http.Request)
912
}

0 commit comments

Comments
 (0)