Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #25

Merged
merged 2 commits into from
Jul 27, 2021
Merged

Fix #25

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# 3.4.1 (July 28, 2021)
# 3.4.2 (July 27, 2021)
* Update headers validation

# 3.4.1 (July 27, 2021)
* `ObjectStorage` method `getById` now supports content-types
* `ObjectStorageWrapper` method `lookupObjectById` now supports content-types
* Added additional validation for headers
Expand Down
2 changes: 1 addition & 1 deletion 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,6 +1,6 @@
{
"name": "@elastic.io/maester-client",
"version": "3.4.1",
"version": "3.4.2",
"description": "The official object-storage client",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
42 changes: 28 additions & 14 deletions spec/ObjectStorageWrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ describe('ObjectStorageWrapper', () => {
.reply(201, createObjectWithQueriableField);
await objectStorageWrapper.createObject(data, genHeaders(1), [], ttl);
});
it('Should save the data correctly', async () => {
nock(maesterUri)
.post('/objects')
.reply(201, createObjectWithQueriableField);
await objectStorageWrapper.createObject(data);
});
it('Should save the data correctly', async () => {
nock(maesterUri)
.post('/objects')
.reply(201, createObjectWithQueriableField);
await objectStorageWrapper.createObject(data, []);
});
it('Should save the data correctly', async () => {
nock(maesterUri)
.post('/objects')
.reply(201, createObjectWithQueriableField);
await objectStorageWrapper.createObject(data, null);
});
it('Should save the data correctly', async () => {
nock(maesterUri)
.post('/objects')
Expand Down Expand Up @@ -184,13 +202,6 @@ describe('ObjectStorageWrapper', () => {
});
});
});
it('At least one header must be present', async () => {
try {
await objectStorageWrapper.createObject(data, [], [], ttl);
} catch (error) {
expect(error.message).to.be.equal('At least one query header must be present');
}
});
});
});
describe('Lookup object by ID', () => {
Expand Down Expand Up @@ -303,6 +314,16 @@ describe('ObjectStorageWrapper', () => {
const result = await objectStorageWrapper.updateObject(id, updatedData);
expect(result).to.deep.equal(updatedData);
});
it('Should successfully update an object', async () => {
nock(maesterUri).put(`/objects/${id}`).reply(200, updatedData);
const result = await objectStorageWrapper.updateObject(id, updatedData, []);
expect(result).to.deep.equal(updatedData);
});
it('Should successfully update an object', async () => {
nock(maesterUri).put(`/objects/${id}`).reply(200, updatedData);
const result = await objectStorageWrapper.updateObject(id, updatedData, null);
expect(result).to.deep.equal(updatedData);
});
it('Should successfully update an object with headers', async () => {
nock(maesterUri)
.put(`/objects/${id}`)
Expand Down Expand Up @@ -348,13 +369,6 @@ describe('ObjectStorageWrapper', () => {
expect(error.message).to.equal('header key "key0" was already added');
});
});
it('At least one header must be present', async () => {
try {
await objectStorageWrapper.updateObject('id', data, []);
} catch (error) {
expect(error.message).to.be.equal('At least one query header must be present');
}
});
});
});
describe('Delete object by ID', () => {
Expand Down
9 changes: 5 additions & 4 deletions src/ObjectStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ObjectStorage, ResponseType, DEFAULT_RESPONSE_TYPE } from './ObjectStor

export const MAESTER_MAX_SUPPORTED_COUNT_OF_QUERY_HEADERS = 5;
export const TTL_HEADER = 'x-eio-ttl';
const isHeaders = (headers?: Header[]): boolean => headers && headers.length > 0;

export interface Scope {
logger: object;
Expand Down Expand Up @@ -36,8 +37,8 @@ export class ObjectStorageWrapper {

async createObject(data: object, queryHeaders?: Header[], metaHeaders?: Header[], ttl?: number) {
this.logger.debug('Going to create an object...');
if (queryHeaders) ObjectStorageWrapper.validateQueryHeaders(queryHeaders);
if (metaHeaders) ObjectStorageWrapper.validateMetaHeaders(metaHeaders);
if (isHeaders(queryHeaders)) ObjectStorageWrapper.validateQueryHeaders(queryHeaders);
if (isHeaders(metaHeaders)) ObjectStorageWrapper.validateMetaHeaders(metaHeaders);
const resultHeaders = ObjectStorageWrapper.formHeadersToAdd(queryHeaders, metaHeaders);
if (ttl) resultHeaders[TTL_HEADER] = ttl.toString();
return this.objectStorage.postObject(data, resultHeaders);
Expand Down Expand Up @@ -69,8 +70,8 @@ export class ObjectStorageWrapper {

async updateObject(id: string, data: object, queryHeaders?: Header[], metaHeaders?: Header[]) {
this.logger.debug(`Going to update and object with id ${id}...`);
if (queryHeaders) ObjectStorageWrapper.validateQueryHeaders(queryHeaders);
if (metaHeaders) ObjectStorageWrapper.validateMetaHeaders(metaHeaders);
if (isHeaders(queryHeaders)) ObjectStorageWrapper.validateQueryHeaders(queryHeaders);
if (isHeaders(metaHeaders)) ObjectStorageWrapper.validateMetaHeaders(metaHeaders);
const resultHeaders = ObjectStorageWrapper.formHeadersToAdd(queryHeaders, metaHeaders);
return this.objectStorage.updateOne(id, data, resultHeaders);
}
Expand Down