Skip to content

Commit 4c4206d

Browse files
authored
fix(rest): do not override content type (#3024)
Signed-off-by: Jiyong Huang <[email protected]>
1 parent 5e18d8b commit 4c4206d

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

internal/io/http/rest_sink_test.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http/httptest"
2424
"net/url"
2525
"reflect"
26+
"strconv"
2627
"strings"
2728
"sync"
2829
"testing"
@@ -76,6 +77,9 @@ func TestRestSink_Apply(t *testing.T) {
7677
config: map[string]interface{}{
7778
"method": "post",
7879
//"url": "http://localhost/test", //set dynamically to the test server
80+
"headers": map[string]any{
81+
"Content-Type": "application/vnd.microsoft.servicebus.json",
82+
},
7983
},
8084
data: []map[string]interface{}{{
8185
"ab": "hello1",
@@ -85,7 +89,7 @@ func TestRestSink_Apply(t *testing.T) {
8589
result: []request{{
8690
Method: "POST",
8791
Body: `[{"ab":"hello1"},{"ab":"hello2"}]`,
88-
ContentType: "application/json",
92+
ContentType: "application/vnd.microsoft.servicebus.json",
8993
}},
9094
}, {
9195
config: map[string]interface{}{
@@ -202,28 +206,28 @@ func TestRestSink_Apply(t *testing.T) {
202206
tf, _ := transform.GenTransform("", "json", "", "", "", []string{})
203207
defer ts.Close()
204208
for i, tt := range tests {
205-
requests = nil
206-
ss, ok := tt.config["sendSingle"]
207-
if !ok {
208-
ss = false
209-
}
210-
s := &RestSink{}
211-
tt.config["url"] = ts.URL
212-
s.Configure(tt.config)
213-
s.Open(ctx)
214-
vCtx := context.WithValue(ctx, context.TransKey, tf)
215-
if ss.(bool) {
216-
for _, d := range tt.data {
217-
s.Collect(vCtx, d)
209+
t.Run(strconv.Itoa(i), func(t *testing.T) {
210+
requests = nil
211+
ss, ok := tt.config["sendSingle"]
212+
if !ok {
213+
ss = false
214+
}
215+
s := &RestSink{}
216+
tt.config["url"] = ts.URL
217+
s.Configure(tt.config)
218+
s.Open(ctx)
219+
vCtx := context.WithValue(ctx, context.TransKey, tf)
220+
if ss.(bool) {
221+
for _, d := range tt.data {
222+
s.Collect(vCtx, d)
223+
}
224+
} else {
225+
s.Collect(vCtx, tt.data)
218226
}
219-
} else {
220-
s.Collect(vCtx, tt.data)
221-
}
222227

223-
s.Close(ctx)
224-
if !reflect.DeepEqual(tt.result, requests) {
225-
t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
226-
}
228+
s.Close(ctx)
229+
assert.Equal(t, tt.result, requests)
230+
})
227231
}
228232
}
229233

internal/pkg/httpx/options.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ func WithBody(body any, bodyType string, retErrOnConvertFailed bool, compressor
111111
}
112112
req.Body = rc
113113
// set content type with body type
114-
req.Header.Set("Content-Type", BodyTypeMap[bodyType])
114+
if req.Header.Get("Content-Type") == "" {
115+
req.Header.Set("Content-Type", BodyTypeMap[bodyType])
116+
}
115117
case "form":
116118
form := url.Values{}
117119
im, err := convertToMap(body, retErrOnConvertFailed)
@@ -136,7 +138,9 @@ func WithBody(body any, bodyType string, retErrOnConvertFailed bool, compressor
136138

137139
encodedFormBody := form.Encode()
138140
req.Body = io.NopCloser(strings.NewReader(encodedFormBody))
139-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;param=value")
141+
if req.Header.Get("Content-Type") == "" {
142+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;param=value")
143+
}
140144
default:
141145
return fmt.Errorf("unsupported body type %s", bodyType)
142146
}

0 commit comments

Comments
 (0)