binding_body_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package binding
  2. /*
  3. func TestBindingBody(t *testing.T) {
  4. for _, tt := range []struct {
  5. name string
  6. binding BindingBody
  7. body string
  8. want string
  9. }{
  10. {
  11. name: "JSON binding",
  12. binding: JSON,
  13. body: `{"foo":"FOO"}`,
  14. },
  15. {
  16. name: "XML binding",
  17. binding: XML,
  18. body: `<?xml version="1.0" encoding="UTF-8"?>
  19. <root>
  20. <foo>FOO</foo>
  21. </root>`,
  22. },
  23. {
  24. name: "MsgPack binding",
  25. binding: MsgPack,
  26. body: msgPackBody(t),
  27. },
  28. {
  29. name: "YAML binding",
  30. binding: YAML,
  31. body: `foo: FOO`,
  32. },
  33. } {
  34. t.Logf("testing: %s", tt.name)
  35. req := requestWithBody("POST", "/", tt.body)
  36. form := FooStruct{}
  37. body, _ := ioutil.ReadAll(req.Body)
  38. assert.NoError(t, tt.binding.BindBody(body, &form))
  39. assert.Equal(t, FooStruct{"FOO"}, form)
  40. }
  41. }
  42. func msgPackBody(t *testing.T) string {
  43. test := FooStruct{"FOO"}
  44. h := new(codec.MsgpackHandle)
  45. buf := bytes.NewBuffer(nil)
  46. assert.NoError(t, codec.NewEncoder(buf, h).Encode(test))
  47. return buf.String()
  48. }
  49. func TestBindingBodyProto(t *testing.T) {
  50. test := protoexample.Test{
  51. Label: proto.String("FOO"),
  52. }
  53. data, _ := proto.Marshal(&test)
  54. req := requestWithBody("POST", "/", string(data))
  55. form := protoexample.Test{}
  56. body, _ := ioutil.ReadAll(req.Body)
  57. assert.NoError(t, ProtoBuf.BindBody(body, &form))
  58. assert.Equal(t, test, form)
  59. }
  60. // */