(t *testing.T)
| 2479 | } |
| 2480 | |
| 2481 | func TestContextShouldBindBodyWith(t *testing.T) { |
| 2482 | type typeA struct { |
| 2483 | Foo string `json:"foo" xml:"foo" binding:"required"` |
| 2484 | } |
| 2485 | type typeB struct { |
| 2486 | Bar string `json:"bar" xml:"bar" binding:"required"` |
| 2487 | } |
| 2488 | for _, tt := range []struct { |
| 2489 | name string |
| 2490 | bindingA, bindingB binding.BindingBody |
| 2491 | bodyA, bodyB string |
| 2492 | }{ |
| 2493 | { |
| 2494 | name: "JSON & JSON", |
| 2495 | bindingA: binding.JSON, |
| 2496 | bindingB: binding.JSON, |
| 2497 | bodyA: `{"foo":"FOO"}`, |
| 2498 | bodyB: `{"bar":"BAR"}`, |
| 2499 | }, |
| 2500 | { |
| 2501 | name: "JSON & XML", |
| 2502 | bindingA: binding.JSON, |
| 2503 | bindingB: binding.XML, |
| 2504 | bodyA: `{"foo":"FOO"}`, |
| 2505 | bodyB: `<?xml version="1.0" encoding="UTF-8"?> |
| 2506 | <root> |
| 2507 | <bar>BAR</bar> |
| 2508 | </root>`, |
| 2509 | }, |
| 2510 | { |
| 2511 | name: "XML & XML", |
| 2512 | bindingA: binding.XML, |
| 2513 | bindingB: binding.XML, |
| 2514 | bodyA: `<?xml version="1.0" encoding="UTF-8"?> |
| 2515 | <root> |
| 2516 | <foo>FOO</foo> |
| 2517 | </root>`, |
| 2518 | bodyB: `<?xml version="1.0" encoding="UTF-8"?> |
| 2519 | <root> |
| 2520 | <bar>BAR</bar> |
| 2521 | </root>`, |
| 2522 | }, |
| 2523 | } { |
| 2524 | t.Logf("testing: %s", tt.name) |
| 2525 | // bodyA to typeA and typeB |
| 2526 | { |
| 2527 | w := httptest.NewRecorder() |
| 2528 | c, _ := CreateTestContext(w) |
| 2529 | c.Request, _ = http.NewRequest( |
| 2530 | http.MethodPost, "http://example.com", strings.NewReader(tt.bodyA), |
| 2531 | ) |
| 2532 | // When it binds to typeA and typeB, it finds the body is |
| 2533 | // not typeB but typeA. |
| 2534 | objA := typeA{} |
| 2535 | require.NoError(t, c.ShouldBindBodyWith(&objA, tt.bindingA)) |
| 2536 | assert.Equal(t, typeA{"FOO"}, objA) |
| 2537 | objB := typeB{} |
| 2538 | require.Error(t, c.ShouldBindBodyWith(&objB, tt.bindingB)) |
nothing calls this directly
no test coverage detected