(t *testing.T)
| 686 | } |
| 687 | |
| 688 | func TestContextGetAndSetPathValuesMutability(t *testing.T) { |
| 689 | t.Run("c.PathValues() does not return copy and modifying raw slice mutates value in context", func(t *testing.T) { |
| 690 | e := New() |
| 691 | e.contextPathParamAllocSize.Store(1) |
| 692 | |
| 693 | req := httptest.NewRequest(http.MethodGet, "/:foo", nil) |
| 694 | c := e.NewContext(req, nil) |
| 695 | |
| 696 | params := PathValues{{Name: "foo", Value: "101"}} |
| 697 | c.SetPathValues(params) |
| 698 | |
| 699 | // round-trip param values with modification |
| 700 | paramVals := c.PathValues() |
| 701 | assert.Equal(t, params, c.PathValues()) |
| 702 | |
| 703 | // PathValues() does not return copy and modifying raw slice mutates value in context |
| 704 | paramVals[0] = PathValue{Name: "xxx", Value: "yyy"} |
| 705 | assert.Equal(t, PathValues{PathValue{Name: "xxx", Value: "yyy"}}, c.PathValues()) |
| 706 | }) |
| 707 | |
| 708 | t.Run("calling SetPathValues with bigger size changes capacity in context", func(t *testing.T) { |
| 709 | e := New() |
| 710 | e.contextPathParamAllocSize.Store(1) |
| 711 | |
| 712 | req := httptest.NewRequest(http.MethodGet, "/:foo", nil) |
| 713 | c := e.NewContext(req, nil) |
| 714 | // increase path param capacity in context |
| 715 | pathValues := PathValues{ |
| 716 | {Name: "aaa", Value: "bbb"}, |
| 717 | {Name: "ccc", Value: "ddd"}, |
| 718 | } |
| 719 | c.SetPathValues(pathValues) |
| 720 | assert.Equal(t, pathValues, c.PathValues()) |
| 721 | |
| 722 | // shouldn't explode during Reset() afterwards! |
| 723 | assert.NotPanics(t, func() { |
| 724 | c.Reset(nil, nil) |
| 725 | }) |
| 726 | assert.Equal(t, PathValues{}, c.PathValues()) |
| 727 | assert.Len(t, *c.pathValues, 0) |
| 728 | assert.Equal(t, 2, cap(*c.pathValues)) |
| 729 | }) |
| 730 | |
| 731 | t.Run("calling SetPathValues with smaller size slice does not change capacity in context", func(t *testing.T) { |
| 732 | e := New() |
| 733 | |
| 734 | req := httptest.NewRequest(http.MethodGet, "/:foo", nil) |
| 735 | c := e.NewContext(req, nil) |
| 736 | c.pathValues = &PathValues{ |
| 737 | {Name: "aaa", Value: "bbb"}, |
| 738 | {Name: "ccc", Value: "ddd"}, |
| 739 | } |
| 740 | |
| 741 | pathValues := PathValues{ |
| 742 | {Name: "aaa", Value: "bbb"}, |
| 743 | } |
| 744 | // given pathValues slice is smaller. this should not decrease c.pathValues capacity |
| 745 | c.SetPathValues(pathValues) |
nothing calls this directly
no test coverage detected
searching dependent graphs…