Skip to content

Commit d987b20

Browse files
authored
Merge pull request #27 from arrlancore/feat-interceptor-options
Add Request and Response Interceptors
2 parents 6e4201a + a1ecc99 commit d987b20

File tree

2 files changed

+99
-15
lines changed

2 files changed

+99
-15
lines changed

axios4go_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,59 @@ func TestValidateStatus(t *testing.T) {
698698
})
699699

700700
}
701+
702+
func TestInterceptors(t *testing.T) {
703+
server := setupTestServer()
704+
defer server.Close()
705+
706+
var interceptedRequest *http.Request
707+
requestInterceptorCalled := false
708+
requestInterceptor := func(req *http.Request) error {
709+
req.Header.Set("X-Intercepted", "true")
710+
interceptedRequest = req
711+
requestInterceptorCalled = true
712+
return nil
713+
}
714+
715+
responseInterceptor := func(resp *http.Response) error {
716+
resp.Header.Set("X-Intercepted-Response", "true")
717+
return nil
718+
}
719+
720+
opts := &RequestOptions{
721+
headers: map[string]string{
722+
"Content-Type": "application/json",
723+
},
724+
params: map[string]string{
725+
"query": "myQuery",
726+
},
727+
}
728+
729+
opts.interceptorOptions = InterceptorOptions{
730+
requestInterceptors: []func(*http.Request) error{requestInterceptor},
731+
responseInterceptors: []func(*http.Response) error{responseInterceptor},
732+
}
733+
734+
t.Run("Interceptors Test", func(t *testing.T) {
735+
response, err := Get(server.URL+"/get", opts)
736+
if err != nil {
737+
t.Fatalf("Expected no error, got %v", err)
738+
}
739+
740+
if !requestInterceptorCalled {
741+
t.Error("Request interceptor was not called")
742+
}
743+
744+
if interceptedRequest != nil {
745+
if interceptedRequest.Header.Get("X-Intercepted") != "true" {
746+
t.Errorf("Expected request header 'X-Intercepted' to be 'true', got '%s'", interceptedRequest.Header.Get("X-Intercepted"))
747+
}
748+
} else {
749+
t.Error("Intercepted request is nil")
750+
}
751+
752+
if response.Headers.Get("X-Intercepted-Response") != "true" {
753+
t.Errorf("Expected response header 'X-Intercepted-Response' to be 'true', got '%s'", response.Headers.Get("X-Intercepted-Response"))
754+
}
755+
})
756+
}

client.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,30 @@ type Promise struct {
3535
mu sync.Mutex
3636
}
3737

38+
type RequestInterceptors []func(*http.Request) error
39+
type ResponseInterceptors []func(*http.Response) error
40+
type InterceptorOptions struct {
41+
requestInterceptors RequestInterceptors
42+
responseInterceptors ResponseInterceptors
43+
}
44+
3845
type RequestOptions struct {
39-
method string
40-
url string
41-
baseURL string
42-
params map[string]string
43-
body interface{}
44-
headers map[string]string
45-
timeout int
46-
auth *auth
47-
responseType string
48-
responseEncoding string
49-
maxRedirects int
50-
maxContentLength int
51-
maxBodyLength int
52-
decompress bool
53-
validateStatus func(int) bool
46+
method string
47+
url string
48+
baseURL string
49+
params map[string]string
50+
body interface{}
51+
headers map[string]string
52+
timeout int
53+
auth *auth
54+
responseType string
55+
responseEncoding string
56+
maxRedirects int
57+
maxContentLength int
58+
maxBodyLength int
59+
decompress bool
60+
validateStatus func(int) bool
61+
interceptorOptions InterceptorOptions
5462
}
5563

5664
type auth struct {
@@ -336,6 +344,13 @@ func (c *Client) Request(options *RequestOptions) (*Response, error) {
336344
return nil, err
337345
}
338346

347+
for _, interceptor := range options.interceptorOptions.requestInterceptors {
348+
err = interceptor(req)
349+
if err != nil {
350+
return nil, fmt.Errorf("request interceptor failed: %w", err)
351+
}
352+
}
353+
339354
if options.headers == nil {
340355
options.headers = make(map[string]string)
341356
}
@@ -394,6 +409,13 @@ func (c *Client) Request(options *RequestOptions) (*Response, error) {
394409
return nil, fmt.Errorf("Request failed with status code: %v", resp.StatusCode)
395410
}
396411

412+
for _, interceptor := range options.interceptorOptions.responseInterceptors {
413+
err = interceptor(resp)
414+
if err != nil {
415+
return nil, fmt.Errorf("response interceptor failed: %w", err)
416+
}
417+
}
418+
397419
return &Response{
398420
StatusCode: resp.StatusCode,
399421
Headers: resp.Header,
@@ -444,6 +466,12 @@ func mergeOptions(dst, src *RequestOptions) {
444466
if src.validateStatus != nil {
445467
dst.validateStatus = src.validateStatus
446468
}
469+
if src.interceptorOptions.requestInterceptors != nil {
470+
dst.interceptorOptions.requestInterceptors = src.interceptorOptions.requestInterceptors
471+
}
472+
if src.interceptorOptions.responseInterceptors != nil {
473+
dst.interceptorOptions.responseInterceptors = src.interceptorOptions.responseInterceptors
474+
}
447475
dst.decompress = src.decompress
448476
}
449477

0 commit comments

Comments
 (0)