| 795 | } |
| 796 | |
| 797 | func TestMetaHeader(t *testing.T) { |
| 798 | t.Run("MetaHeader with elastictransport", func(t *testing.T) { |
| 799 | tp, err := elastictransport.New(elastictransport.Config{ |
| 800 | URLs: []*url.URL{{Scheme: "http", Host: "foo"}}, |
| 801 | Transport: &mockTransp{ |
| 802 | RoundTripFunc: func(request *http.Request) (*http.Response, error) { |
| 803 | h := request.Header.Get(HeaderClientMeta) |
| 804 | if !metaHeaderReValidation.MatchString(h) { |
| 805 | t.Errorf("expected client metaheader to validate regexp, got: %s", h) |
| 806 | } |
| 807 | |
| 808 | return &http.Response{ |
| 809 | Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}}, |
| 810 | StatusCode: http.StatusOK, |
| 811 | Status: "OK", |
| 812 | Body: io.NopCloser(strings.NewReader("")), |
| 813 | }, nil |
| 814 | }, |
| 815 | }, |
| 816 | }) |
| 817 | if err != nil { |
| 818 | t.Fatalf("Unexpected error: %s", err) |
| 819 | } |
| 820 | |
| 821 | c, err := New() |
| 822 | if err != nil { |
| 823 | t.Fatalf("Unexpected error: %s", err) |
| 824 | } |
| 825 | c.Transport = tp |
| 826 | |
| 827 | _, err = c.Info() |
| 828 | if err != nil { |
| 829 | t.Fatalf("Unexpected error: %s", err) |
| 830 | } |
| 831 | }) |
| 832 | |
| 833 | t.Run("Metaheader with typedclient", func(t *testing.T) { |
| 834 | tp, err := elastictransport.New(elastictransport.Config{ |
| 835 | URLs: []*url.URL{{Scheme: "http", Host: "foo"}}, |
| 836 | Transport: &mockTransp{ |
| 837 | RoundTripFunc: func(request *http.Request) (*http.Response, error) { |
| 838 | h := request.Header.Get(HeaderClientMeta) |
| 839 | if !metaHeaderReValidation.MatchString(h) { |
| 840 | t.Errorf("expected client metaheader to validate regexp, got: %s", h) |
| 841 | } |
| 842 | if !strings.Contains(h, "hl=1") { |
| 843 | t.Errorf("invalid metaheader, should contain hl=1, got: %s", h) |
| 844 | } |
| 845 | |
| 846 | return &http.Response{ |
| 847 | Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}}, |
| 848 | StatusCode: http.StatusOK, |
| 849 | Status: "OK", |
| 850 | Body: io.NopCloser(strings.NewReader("")), |
| 851 | }, nil |
| 852 | }, |
| 853 | }, |
| 854 | }) |