Skip to content

Commit

Permalink
Merge pull request #2492 from kuzzleio/2.27.1-proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko authored Oct 24, 2023
2 parents 5870c81 + e6648dd commit 75cb185
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 19 deletions.
159 changes: 159 additions & 0 deletions jest/api/controller/document/mCreate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { Kuzzle, WebSocket } from 'kuzzle-sdk';

const kuzzle = new Kuzzle(new WebSocket('localhost'));
const index = 'nyc-open-data';
const collection = 'yellow-taxi';

beforeAll(async () => {
await kuzzle.connect();
await kuzzle.index.create(index);
await kuzzle.collection.create(index, collection, {
mappings: {
properties: {
value: {
type: 'keyword'
},
field: {
properties: {
path: {
type: 'keyword'
}
}
}
}
}
});
});

afterAll(async () => {
await kuzzle.index.delete(index);
await kuzzle.disconnect();
});

describe('mCreate', () => {
afterEach(async () => {
await kuzzle.collection.truncate(index, collection);
});


it('It should create document if not exists', async () => {
const mCreateResult = await kuzzle.document.mCreate(index, collection, [
{
_id: 'A',
body: {
value: 'A'
}
}
], {
refresh: 'wait_for'
});

expect(mCreateResult.successes.length).toEqual(1);

const result = await kuzzle.document.mGet(index, collection, ['A']);

expect(result.successes.length).toEqual(1);
expect(result.successes[0]._source).toMatchObject({
value: 'A'
});
});

it('It should not replace the document if not exists', async () => {
let mCreateResult = await kuzzle.document.mCreate(index, collection, [
{
_id: 'A',
body: {
value: 'A'
}
}
], {
refresh: 'wait_for'
});

expect(mCreateResult.successes.length).toEqual(1);

mCreateResult = await kuzzle.document.mCreate(index, collection, [
{
_id: 'A',
body: {
value: 'FOO'
}
}
], {
refresh: 'wait_for'
});

expect(mCreateResult.successes.length).toEqual(0);
expect(mCreateResult.errors.length).toEqual(1);

const result = await kuzzle.document.mGet(index, collection, ['A']);

expect(result.successes.length).toEqual(1);
expect(result.successes[0]._source).toMatchObject({
value: 'A'
});
});


it('It should not replace the document even if the previous document did not exist', async () => {
let mCreateResult = await kuzzle.document.mCreate(index, collection, [
{
_id: 'A',
body: {
value: 'A'
}
},
{
_id: 'C',
body: {
value: 'C'
}
}
], {
refresh: 'wait_for'
});

expect(mCreateResult.successes.length).toEqual(2);

mCreateResult = await kuzzle.document.mCreate(index, collection, [
{
_id: 'A',
body: {
value: 'FOO'
}
},
{
_id: 'B',
body: {
value: 'B'
}
},
{
_id: 'C',
body: {
value: 'FOO'
}
}
], {
refresh: 'wait_for'
});

expect(mCreateResult.successes.length).toEqual(1);
expect(mCreateResult.errors.length).toEqual(2);

const result = await kuzzle.document.mGet(index, collection, ['A', 'B', 'C']);

expect(result.successes.length).toEqual(3);
expect(result.successes[0]._source).toMatchObject({
value: 'A'
});
expect(result.successes[1]._source).toMatchObject({
value: 'B'
});
expect(result.successes[2]._source).toMatchObject({
value: 'C'
});
});


});
35 changes: 21 additions & 14 deletions lib/core/network/protocols/httpwsProtocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,23 +498,30 @@ class HttpWsProtocol extends Protocol {
return;
}

const contentType = message.headers["content-type"];
const contentTypeHeader = message.headers["content-type"];

if (
contentType &&
!this.httpConfig.opts.allowedContentTypes.some((allowed) =>
contentType.includes(allowed)
)
) {
this.httpSendError(
message,
response,
kerrorHTTP.get("unsupported_content", contentType)
);
return;
if (contentTypeHeader) {
const contentTypeParamIndex = contentTypeHeader.indexOf(";");
const contentType =
contentTypeParamIndex !== -1
? contentTypeHeader.slice(0, contentTypeParamIndex).trim()
: contentTypeHeader.trim();

if (
!this.httpConfig.opts.allowedContentTypes.some(
(allowed) => contentType === allowed
)
) {
this.httpSendError(
message,
response,
kerrorHTTP.get("unsupported_content", contentType)
);
return;
}
}

const encoding = CHARSET_REGEX.exec(contentType);
const encoding = CHARSET_REGEX.exec(contentTypeHeader);

if (encoding !== null && encoding[1].toLowerCase() !== "utf-8") {
this.httpSendError(
Expand Down
3 changes: 1 addition & 2 deletions lib/service/storage/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2261,8 +2261,6 @@ class ElasticSearch extends Service {
reason: "document already exists",
status: 400,
});

idx++;
} else {
esRequest.body.push({
index: {
Expand All @@ -2274,6 +2272,7 @@ class ElasticSearch extends Service {

toImport.push(document);
}
idx++;
} else {
esRequest.body.push({ index: { _index: alias } });
esRequest.body.push(document._source);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kuzzle",
"author": "The Kuzzle Team <[email protected]>",
"version": "2.27.0",
"version": "2.27.1",
"description": "Kuzzle is an open-source solution that handles all the data management through a secured API, with a large choice of protocols.",
"bin": "bin/start-kuzzle-server",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions test/core/network/protocols/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ describe("core/network/protocols/http", () => {
});
});

it("should reject requests with supported content types with extraneous characters", () => {
httpWs.server._httpOnMessage("get", "/", "", {
"content-type": "serge application/jsoncheval",
});

should(entryPoint.newConnection).not.called();
should(global.kuzzle.router.http.route).not.called();
should(httpWs.httpSendError)
.calledOnce()
.calledWithMatch(sinon.match.object, httpWs.server._httpResponse, {
id: "network.http.unsupported_content",
});
});

it("should reject requests with unhandled charsets", () => {
httpWs.server._httpOnMessage("get", "/", "", {
"content-type": "application/json; charset=utf-82",
Expand Down

0 comments on commit 75cb185

Please sign in to comment.