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

feat: strict mode is now properly evaluated on the request #639

Merged
merged 2 commits into from
Jun 19, 2023
Merged
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: 5 additions & 0 deletions .changeset/lemon-bears-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/fe-mockserver-core': patch
---

Strict mode is now properly evaluated on the odataRequest
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export type MockDataContributor<T extends object> = {
removeEntry?: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => void;
hasEntry?: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => boolean;
hasEntries?: (odataRequest: ODataRequest) => boolean;
fetchEntries?: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => object[];
getAllEntries?: (odataRequest: ODataRequest) => object[];
getEmptyObject?: (odataRequest: ODataRequest) => object;
getDefaultElement?: (odataRequest: ODataRequest) => object;
fetchEntries?: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => T[];
getAllEntries?: (odataRequest: ODataRequest) => T[];
getEmptyObject?: (odataRequest: ODataRequest) => T;
getDefaultElement?: (odataRequest: ODataRequest) => T;

getReferentialConstraints?: (
_navigationProperty: NavigationProperty
Expand Down Expand Up @@ -86,11 +86,11 @@ export type MockDataContributor<T extends object> = {
updateEntry: (keyValues: KeyDefinitions, newData: T, odataRequest: ODataRequest) => Promise<void>;
removeEntry: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => Promise<void>;
hasEntry: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => boolean;
fetchEntries: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => object[];
fetchEntries: (keyValues: KeyDefinitions, odataRequest: ODataRequest) => T[];
hasEntries: (odataRequest: ODataRequest) => boolean;
getAllEntries: (odataRequest: ODataRequest) => object[];
getEmptyObject: (odataRequest: ODataRequest) => object;
getDefaultElement: (odataRequest: ODataRequest) => object;
getAllEntries: (odataRequest: ODataRequest) => T[];
getEmptyObject: (odataRequest: ODataRequest) => T;
getDefaultElement: (odataRequest: ODataRequest) => T;
getParentEntityInterface: () => Promise<FileBasedMockData | undefined>;
getEntityInterface: (entityName: string) => Promise<FileBasedMockData | undefined>;
checkSearchQuery: (mockData: any, searchQuery: string, odataRequest: ODataRequest) => boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/fe-mockserver-core/src/request/odataRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type QueryPath = {

export default class ODataRequest {
private isMinimalRepresentation: boolean;
public isStrictMode: boolean;
public tenantId: string;
public queryPath: QueryPath[];
public searchQuery: string[];
Expand Down Expand Up @@ -75,6 +76,7 @@ export default class ODataRequest {
this.addResponseHeader('sap-tenantid', this.tenantId);
}
this.isMinimalRepresentation = requestContent.headers?.['prefer'] === 'return=minimal';
this.isStrictMode = requestContent.headers?.['prefer']?.includes('handling=strict') ?? false;
this.queryPath = this.parsePath(parsedUrl.pathname.substring(1));
this.parseParameters(parsedUrl.searchParams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class ODataV4ObjectRequest<T> extends ODataRequest<T> {
targetPath: string,
protected objectKey: ODataKey = {},
protected method: string = 'GET',
protected headers: Record<string, string> = {}
public headers: Record<string, string> = {}
) {
super(odataRootUri, targetPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = {
case 'boundActionReturnsVoid':
return undefined;
case 'baseFunction':
if (odataRequest.isStrictMode) {
return `STRICT :: ${actionData.data}`;
}
return actionData.data;
default:
this.throwError('Not implemented', 501, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"ID": 1,
"Prop1": "SomethingElse",
"Prop1": "First Prop",
"Prop2": "Second Prop",
"isBoundAction1Hidden": false,
"isBoundAction2Hidden": false,
Expand Down
13 changes: 13 additions & 0 deletions packages/fe-mockserver-core/test/unit/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ describe('V4 Requestor', function () {
)
.execute('GET');
expect(dataRes).toMatchInlineSnapshot(`"I am data"`);
const dataResStrict = dataRequestor.callGETAction(
"/RootElement(ID=1,IsActiveEntity=true)/sap.fe.core.ActionVisibility.baseFunction(data='I am data')"
);
dataResStrict.headers['Prefer'] = 'handling=strict';
const strictResult = await dataResStrict.execute('GET');
expect(strictResult).toMatchInlineSnapshot(`"STRICT :: I am data"`);
const dataRes2 = await dataRequestor
.callGETAction('/RootElement(ID=1,IsActiveEntity=true)/_Elements')
.execute('GET');
Expand Down Expand Up @@ -350,4 +356,11 @@ describe('V4 Requestor', function () {
myJSON[0].Prop1 = 'First Prop';
fs.writeFileSync(path.join(__dirname, '__testData', 'RootElement.json'), JSON.stringify(myJSON, null, 4));
});
afterAll(() => {
const myJSON = JSON.parse(
fs.readFileSync(path.join(__dirname, '__testData', 'RootElement.json')).toString('utf-8')
);
myJSON[0].Prop1 = 'First Prop';
fs.writeFileSync(path.join(__dirname, '__testData', 'RootElement.json'), JSON.stringify(myJSON, null, 4));
});
});