Skip to content

Commit 7dfa408

Browse files
Fix issue with adding sample data to data source (opensearch-project#9676)
* Fix issue with adding sample data to data source Signed-off-by: Zhongnan Su <szhongna@amazon.com> * Changeset file for PR opensearch-project#9676 created/updated --------- Signed-off-by: Zhongnan Su <szhongna@amazon.com> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
1 parent aa04bbc commit 7dfa408

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

changelogs/fragments/9676.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fix:
2+
- Fix issue with adding sample data to data source ([#9676](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9676))

src/plugins/data_source/server/legacy/http_aws_es/connector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class HttpAmazonESConnector extends HttpConnector {
104104
const body = params.body;
105105
if (body) {
106106
const contentLength = Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);
107-
request.headers['Content-Length'] = contentLength;
107+
request.headers['Content-Length'] = contentLength.toString();
108108
request.body = body;
109109
}
110110

src/plugins/data_source/server/legacy/http_aws_es/connector.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,66 @@ describe('createRequest', () => {
9595

9696
expect(request.path).to.equal(reqParams.path);
9797
});
98+
99+
it('should set Content-Length as a string for a string body', () => {
100+
const host = new Host();
101+
const connector = new Connector(host, {
102+
awsConfig: {
103+
region: 'us-east-1',
104+
credentials: defaultProvider(),
105+
},
106+
});
107+
108+
const bodyString = '{"name": "test"}';
109+
110+
// Pass the body in the first parameter (params) so that createRequest picks it up.
111+
const params = {
112+
method: 'POST',
113+
body: bodyString,
114+
};
115+
const reqParams = {
116+
method: 'POST',
117+
path: '/test',
118+
headers: {},
119+
};
120+
121+
const request = connector.createRequest(params, reqParams);
122+
123+
// Calculate the expected content length and convert it to string.
124+
const expectedLength = Buffer.byteLength(bodyString).toString();
125+
expect(request.headers).to.have.property('Content-Length');
126+
expect(request.headers['Content-Length']).to.be.a('string');
127+
expect(request.headers['Content-Length']).to.equal(expectedLength);
128+
});
129+
130+
it('should set Content-Length as a string for a Buffer body', () => {
131+
const host = new Host();
132+
const connector = new Connector(host, {
133+
awsConfig: {
134+
region: 'us-east-1',
135+
credentials: defaultProvider(),
136+
},
137+
});
138+
139+
const bodyBuffer = Buffer.from('This is a test');
140+
141+
// Again, place the body in the first parameter.
142+
const params = {
143+
method: 'POST',
144+
body: bodyBuffer,
145+
};
146+
const reqParams = {
147+
method: 'POST',
148+
path: '/test',
149+
headers: {},
150+
};
151+
152+
const request = connector.createRequest(params, reqParams);
153+
154+
// Calculate the expected content length for the Buffer and convert to string.
155+
const expectedLength = bodyBuffer.length.toString();
156+
expect(request.headers).to.have.property('Content-Length');
157+
expect(request.headers['Content-Length']).to.be.a('string');
158+
expect(request.headers['Content-Length']).to.equal(expectedLength);
159+
});
98160
});

0 commit comments

Comments
 (0)