| 1608 | } |
| 1609 | |
| 1610 | func TestIntercepts(t *testing.T) { |
| 1611 | successTp := func(request *http.Request) (*http.Response, error) { |
| 1612 | h := request.Header.Clone() |
| 1613 | h.Add("X-Elastic-Product", "Elasticsearch") |
| 1614 | h.Add("X-Found-Handling-Cluster", "foo-bar-cluster-id") |
| 1615 | h.Add("X-Found-Handling-Instance", "0123456789") |
| 1616 | return &http.Response{ |
| 1617 | StatusCode: http.StatusOK, |
| 1618 | Header: h, |
| 1619 | Body: io.NopCloser(strings.NewReader(`{}`)), |
| 1620 | }, nil |
| 1621 | } |
| 1622 | |
| 1623 | const ( |
| 1624 | testInterceptReqHeader = "X-TEST-INTERCEPT-REQ" |
| 1625 | testInterceptRespHeader = "X-TEST-INTERCEPT-RESP" |
| 1626 | ) |
| 1627 | |
| 1628 | dummyInterceptorFactory := func(num int) elastictransport.InterceptorFunc { |
| 1629 | return func(next elastictransport.RoundTripFunc) elastictransport.RoundTripFunc { |
| 1630 | return func(r *http.Request) (*http.Response, error) { |
| 1631 | r.Header.Add(testInterceptReqHeader, strconv.Itoa(num)) |
| 1632 | resp, err := next(r) |
| 1633 | resp.Header.Add(testInterceptRespHeader, strconv.Itoa(num)) |
| 1634 | return resp, err |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | validateReqRespH := func(expectReq, expectResp string) func(t *testing.T, resp *esapi.Response) { |
| 1640 | return func(t *testing.T, resp *esapi.Response) { |
| 1641 | if strings.Join(resp.Header.Values(testInterceptReqHeader), ",") != expectReq { |
| 1642 | t.Error("expected header value", resp.Header.Values(testInterceptReqHeader)) |
| 1643 | } |
| 1644 | if strings.Join(resp.Header.Values(testInterceptRespHeader), ",") != expectResp { |
| 1645 | t.Error("expected header value", resp.Header.Values(testInterceptRespHeader)) |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | type args struct { |
| 1651 | interceptors []elastictransport.InterceptorFunc |
| 1652 | } |
| 1653 | tests := []struct { |
| 1654 | name string |
| 1655 | args args |
| 1656 | wantErr bool |
| 1657 | validateResponse func(t *testing.T, resp *esapi.Response) |
| 1658 | }{ |
| 1659 | { |
| 1660 | name: "interceptor array nil", |
| 1661 | args: args{interceptors: nil}, |
| 1662 | wantErr: false, |
| 1663 | }, |
| 1664 | { |
| 1665 | name: "interceptor array empty", |
| 1666 | args: args{interceptors: []elastictransport.InterceptorFunc{}}, |
| 1667 | wantErr: false, |