MCPcopy
hub / github.com/kubernetes/client-go / TestBody

Function TestBody

rest/request_test.go:1707–1767  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1705}
1706
1707func TestBody(t *testing.T) {
1708 const data = "test payload"
1709
1710 obj := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1711 bodyExpected, _ := runtime.Encode(scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), obj)
1712
1713 f, err := ioutil.TempFile("", "test_body")
1714 if err != nil {
1715 t.Fatalf("TempFile error: %v", err)
1716 }
1717 if _, err := f.WriteString(data); err != nil {
1718 t.Fatalf("TempFile.WriteString error: %v", err)
1719 }
1720 f.Close()
1721 defer os.Remove(f.Name())
1722
1723 var nilObject *metav1.DeleteOptions
1724 typedObject := interface{}(nilObject)
1725 c := testRESTClient(t, nil)
1726 tests := []struct {
1727 input interface{}
1728 expected string
1729 headers map[string]string
1730 }{
1731 {[]byte(data), data, nil},
1732 {f.Name(), data, nil},
1733 {strings.NewReader(data), data, nil},
1734 {obj, string(bodyExpected), map[string]string{"Content-Type": "application/json"}},
1735 {typedObject, "", nil},
1736 }
1737 for i, tt := range tests {
1738 r := c.Post().Body(tt.input)
1739 if r.err != nil {
1740 t.Errorf("%d: r.Body(%#v) error: %v", i, tt, r.err)
1741 continue
1742 }
1743 if tt.headers != nil {
1744 for k, v := range tt.headers {
1745 if r.headers.Get(k) != v {
1746 t.Errorf("%d: r.headers[%q] = %q; want %q", i, k, v, v)
1747 }
1748 }
1749 }
1750
1751 if r.body == nil {
1752 if len(tt.expected) != 0 {
1753 t.Errorf("%d: r.body = %q; want %q", i, r.body, tt.expected)
1754 }
1755 continue
1756 }
1757 buf := make([]byte, len(tt.expected))
1758 if _, err := r.body.Read(buf); err != nil {
1759 t.Errorf("%d: r.body.Read error: %v", i, err)
1760 continue
1761 }
1762 body := string(buf)
1763 if body != tt.expected {
1764 t.Errorf("%d: r.body = %q; want %q", i, body, tt.expected)

Callers

nothing calls this directly

Calls 9

testRESTClientFunction · 0.85
BodyMethod · 0.80
CloseMethod · 0.65
PostMethod · 0.65
ErrorfMethod · 0.65
GetMethod · 0.65
EncodeMethod · 0.45
NameMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected