mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Set Content-Type in client http request header when posting objects
This commit is contained in:
parent
59f15bbd74
commit
583246a22f
@ -463,7 +463,7 @@ func (r *Request) Timeout(d time.Duration) *Request {
|
|||||||
// If obj is a string, try to read a file of that name.
|
// If obj is a string, try to read a file of that name.
|
||||||
// If obj is a []byte, send it directly.
|
// If obj is a []byte, send it directly.
|
||||||
// If obj is an io.Reader, use it directly.
|
// If obj is an io.Reader, use it directly.
|
||||||
// If obj is a runtime.Object, marshal it correctly.
|
// If obj is a runtime.Object, marshal it correctly, and set Content-Type header.
|
||||||
// Otherwise, set an error.
|
// Otherwise, set an error.
|
||||||
func (r *Request) Body(obj interface{}) *Request {
|
func (r *Request) Body(obj interface{}) *Request {
|
||||||
if r.err != nil {
|
if r.err != nil {
|
||||||
@ -491,6 +491,7 @@ func (r *Request) Body(obj interface{}) *Request {
|
|||||||
}
|
}
|
||||||
glog.V(8).Infof("Request Body: %s", string(data))
|
glog.V(8).Infof("Request Body: %s", string(data))
|
||||||
r.body = bytes.NewBuffer(data)
|
r.body = bytes.NewBuffer(data)
|
||||||
|
r.SetHeader("Content-Type", "application/json")
|
||||||
default:
|
default:
|
||||||
r.err = fmt.Errorf("unknown type used for body: %+v", obj)
|
r.err = fmt.Errorf("unknown type used for body: %+v", obj)
|
||||||
}
|
}
|
||||||
|
@ -1034,6 +1034,9 @@ func TestUnacceptableParamNames(t *testing.T) {
|
|||||||
func TestBody(t *testing.T) {
|
func TestBody(t *testing.T) {
|
||||||
const data = "test payload"
|
const data = "test payload"
|
||||||
|
|
||||||
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
||||||
|
bodyExpected, _ := testapi.Default.Codec().Encode(obj)
|
||||||
|
|
||||||
f, err := ioutil.TempFile("", "test_body")
|
f, err := ioutil.TempFile("", "test_body")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("TempFile error: %v", err)
|
t.Fatalf("TempFile error: %v", err)
|
||||||
@ -1044,21 +1047,37 @@ func TestBody(t *testing.T) {
|
|||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
c := NewOrDie(&Config{})
|
c := NewOrDie(&Config{})
|
||||||
tests := []interface{}{[]byte(data), f.Name(), strings.NewReader(data)}
|
tests := []struct {
|
||||||
|
input interface{}
|
||||||
|
expected string
|
||||||
|
headers map[string]string
|
||||||
|
}{
|
||||||
|
{[]byte(data), data, nil},
|
||||||
|
{f.Name(), data, nil},
|
||||||
|
{strings.NewReader(data), data, nil},
|
||||||
|
{obj, string(bodyExpected), map[string]string{"Content-Type": "application/json"}},
|
||||||
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
r := c.Post().Body(tt)
|
r := c.Post().Body(tt.input)
|
||||||
if r.err != nil {
|
if r.err != nil {
|
||||||
t.Errorf("%d: r.Body(%#v) error: %v", i, tt, r.err)
|
t.Errorf("%d: r.Body(%#v) error: %v", i, tt, r.err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
buf := make([]byte, len(data))
|
buf := make([]byte, len(tt.expected))
|
||||||
if _, err := r.body.Read(buf); err != nil {
|
if _, err := r.body.Read(buf); err != nil {
|
||||||
t.Errorf("%d: r.body.Read error: %v", i, err)
|
t.Errorf("%d: r.body.Read error: %v", i, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
body := string(buf)
|
body := string(buf)
|
||||||
if body != data {
|
if body != tt.expected {
|
||||||
t.Errorf("%d: r.body = %q; want %q", i, body, data)
|
t.Errorf("%d: r.body = %q; want %q", i, body, tt.expected)
|
||||||
|
}
|
||||||
|
if tt.headers != nil {
|
||||||
|
for k, v := range tt.headers {
|
||||||
|
if r.headers.Get(k) != v {
|
||||||
|
t.Errorf("%d: r.headers[%q] = %q; want %q", i, k, v, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user