|
1 | 1 | package fxelasticsearch
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/elastic/go-elasticsearch/v8/esapi" |
5 |
| - "github.com/stretchr/testify/mock" |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/elastic/go-elasticsearch/v8" |
6 | 9 | )
|
7 | 10 |
|
8 |
| -// ElasticsearchClientInterface defines the methods for the Elasticsearch client. |
9 |
| -// Extend this interface as needed. |
10 |
| -type ElasticsearchClientInterface interface { |
11 |
| - Search(indices []string, body interface{}) (*esapi.Response, error) |
| 11 | +// MockResponse represents a single mock HTTP response. |
| 12 | +type MockResponse struct { |
| 13 | + StatusCode int |
| 14 | + ResponseBody string |
| 15 | + Error error |
| 16 | +} |
| 17 | + |
| 18 | +// MockTransport provides HTTP transport mock for testing Elasticsearch client. |
| 19 | +// This allows testing with the real elasticsearch.Client API without interface constraints. |
| 20 | +type MockTransport struct { |
| 21 | + responses []MockResponse |
| 22 | + index int |
12 | 23 | }
|
13 | 24 |
|
14 |
| -// ElasticsearchClientMock implements ElasticsearchClientInterface. |
15 |
| -// Add methods as needed for testing. |
16 |
| -type ElasticsearchClientMock struct { |
17 |
| - mock.Mock |
| 25 | +// NewMockTransport creates a new MockTransport with the given responses. |
| 26 | +// Responses are returned in order. If more requests are made than responses provided, |
| 27 | +// the last response is repeated. |
| 28 | +func NewMockTransport(responses []MockResponse) *MockTransport { |
| 29 | + if len(responses) == 0 { |
| 30 | + responses = []MockResponse{{StatusCode: 200, ResponseBody: "{}"}} |
| 31 | + } |
| 32 | + |
| 33 | + return &MockTransport{ |
| 34 | + responses: responses, |
| 35 | + index: 0, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// RoundTrip implements the http.RoundTripper interface. |
| 40 | +func (t *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 41 | + // Get current response (or last one if we've exceeded the list) |
| 42 | + responseIndex := t.index |
| 43 | + if responseIndex >= len(t.responses) { |
| 44 | + responseIndex = len(t.responses) - 1 |
| 45 | + } else { |
| 46 | + t.index++ |
| 47 | + } |
| 48 | + |
| 49 | + mockResp := t.responses[responseIndex] |
| 50 | + |
| 51 | + if mockResp.Error != nil { |
| 52 | + return nil, mockResp.Error |
| 53 | + } |
| 54 | + |
| 55 | + // Create HTTP response |
| 56 | + response := &http.Response{ |
| 57 | + StatusCode: mockResp.StatusCode, |
| 58 | + Body: io.NopCloser(strings.NewReader(mockResp.ResponseBody)), |
| 59 | + Header: make(http.Header), |
| 60 | + Request: req, |
| 61 | + } |
| 62 | + |
| 63 | + // Add Elasticsearch-specific headers that the client expects |
| 64 | + response.Header.Set("Content-Type", "application/json") |
| 65 | + response.Header.Set("X-Elastic-Product", "Elasticsearch") // Critical header for client validation |
| 66 | + |
| 67 | + return response, nil |
18 | 68 | }
|
19 | 69 |
|
20 |
| -func (m *ElasticsearchClientMock) Search(indices []string, body interface{}) (*esapi.Response, error) { |
21 |
| - args := m.Called(indices, body) |
22 |
| - if args.Get(0) == nil { |
23 |
| - return nil, args.Error(1) |
| 70 | +// NewMockESClient creates an Elasticsearch client with mocked HTTP transport. |
| 71 | +// This allows you to test with the real elasticsearch.Client API while controlling responses. |
| 72 | +func NewMockESClient(responses []MockResponse) (*elasticsearch.Client, error) { |
| 73 | + transport := NewMockTransport(responses) |
| 74 | + |
| 75 | + cfg := elasticsearch.Config{ |
| 76 | + Transport: transport, |
24 | 77 | }
|
25 | 78 |
|
26 |
| - resp, ok := args.Get(0).(*esapi.Response) |
27 |
| - if !ok { |
28 |
| - return nil, args.Error(1) |
| 79 | + return elasticsearch.NewClient(cfg) |
| 80 | +} |
| 81 | + |
| 82 | +// NewMockESClientWithSingleResponse creates an Elasticsearch client with a single mock response. |
| 83 | +// This is a convenience function for simple test cases. |
| 84 | +func NewMockESClientWithSingleResponse(responseBody string, statusCode int) (*elasticsearch.Client, error) { |
| 85 | + responses := []MockResponse{ |
| 86 | + { |
| 87 | + StatusCode: statusCode, |
| 88 | + ResponseBody: responseBody, |
| 89 | + }, |
29 | 90 | }
|
30 | 91 |
|
31 |
| - return resp, args.Error(1) |
| 92 | + return NewMockESClient(responses) |
32 | 93 | }
|
33 | 94 |
|
34 |
| -// Ensure ElasticsearchClientMock implements ElasticsearchClientInterface. |
35 |
| -var _ ElasticsearchClientInterface = (*ElasticsearchClientMock)(nil) |
| 95 | +// NewMockESClientWithError creates an Elasticsearch client that returns an error on requests. |
| 96 | +// This is useful for testing error handling. |
| 97 | +func NewMockESClientWithError(err error) (*elasticsearch.Client, error) { |
| 98 | + responses := []MockResponse{ |
| 99 | + { |
| 100 | + Error: err, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + return NewMockESClient(responses) |
| 105 | +} |
0 commit comments