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

chore: Updates in test without nock update #5118

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ HTTP/1.1 200 OK
const response = await batch(
operations.testFunctionImportGet({} as any)
).execute({ url: baseUrl });
expect(response[0].isReadResponse()).toBeTruthy();
if (response[0].isReadResponse()) {
const casted = testFunctionImportGet({} as any).responseTransformer(
response[0].body
);
expect(casted).toEqual('MyText');
} else {
throw new Error('Should be readResponse');
}
});

Expand Down Expand Up @@ -119,13 +118,12 @@ HTTP/1.1 200 OK
requestBuilder
]);
const response = await batch(changeSet).execute({ url: baseUrl });
expect(response[0].isWriteResponses()).toBeTruthy();
if (response[0].isWriteResponses()) {
const casted = testFunctionImportPost({} as any).responseTransformer(
response[0].responses[0].body
);
expect(casted).toBe(true);
} else {
throw new Error('Should be writeResponse');
}
});

Expand All @@ -138,11 +136,10 @@ HTTP/1.1 200 OK
const response = await batch(
testEntityApi.requestBuilder().getAll()
).execute({ url: baseUrl });
expect(response[0].isReadResponse()).toBeTruthy();
if (response[0].isReadResponse()) {
const casted = response[0].as(testEntityApi);
expect(casted[0].stringProperty).toEqual('4711');
} else {
throw new Error('Should be readResponse');
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ describe('CreateRequestBuilder', () => {
it('throws an error when request execution fails', async () => {
mockCreateRequest(
{
body: () => true,
statusCode: 500
statusCode: 500,
path: 'A_TestEntity'
},
testEntityApi
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ describe('DeleteRequestBuilder', () => {
testEntityApi
);

const deleteRequest = new DeleteRequestBuilder(
testEntityApi,
entity
).execute(defaultDestination);
const deleteRequest = new DeleteRequestBuilder(testEntityApi, entity);

await expect(deleteRequest).resolves.toBe(undefined);
await expect(deleteRequest.execute(defaultDestination)).resolves.toBe(
undefined
);
});

it('delete request with version identifier on the request should resolve', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,18 @@ describe('GetAllRequestBuilder', () => {
mockGetRequest(
{
responseBody: { error: 'ERROR' },
statusCode: 500
statusCode: 500,
path: 'A_TestEntity'
},
testEntityApi
);

const getAllRequest = requestBuilder.execute(defaultDestination);

await expect(getAllRequest).rejects.toThrowErrorMatchingInlineSnapshot(
'"get request to http://example.com/sap/opu/odata/sap/API_TEST_SRV failed! "'
);
await expect(getAllRequest).rejects.toThrowErrorMatchingInlineSnapshot(`
"get request to http://example.com/sap/opu/odata/sap/API_TEST_SRV failed!
"ERROR""
`);
});

it('considers custom timeout on the request', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ describe('GetByKeyRequestBuilder', () => {
expect(actual.versionIdentifier).toBeUndefined();
});

it('ETag should be pulled from __metadata', async () => {
it('eTag should be pulled from __metadata', async () => {
const entityData = createOriginalTestEntityData1();
const versionIdentifier = 'etagInMetadata';
const versionIdentifier = 'eTagInMetadata';
entityData['__metadata'] = { etag: versionIdentifier };
const expected = createTestEntity(entityData);

Expand All @@ -95,10 +95,10 @@ describe('GetByKeyRequestBuilder', () => {
expect(actual).toEqual(expected);
});

it('ETag should be pulled from response header when __metadata has no ETag property', async () => {
it('eTag should be pulled from response header when __metadata has no eTag property', async () => {
const entityData = createOriginalTestEntityData1();
const expected = createTestEntity(entityData);
const versionIdentifier = 'etagInHeader';
const versionIdentifier = 'eTagInHeader';
expected.setVersionIdentifier(versionIdentifier);

mockGetRequest(
Expand All @@ -108,7 +108,7 @@ describe('GetByKeyRequestBuilder', () => {
expected.keyPropertyString
),
responseBody: { d: entityData },
responseHeaders: { Etag: versionIdentifier }
responseHeaders: { etag: versionIdentifier }
},
testEntityApi
);
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('GetByKeyRequestBuilder', () => {
});

it('throws a useful error when request execution fails', async () => {
nock(/.*/).get(/.*/).reply(500);
nock(defaultDestination.url).get(/.*/).reply(500);

const getByKeyRequest = new GetByKeyRequestBuilder(testEntityApi, {
KeyPropertyGuid: uuid(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ describe('OperationRequestBuilder', () => {
const requestBuilder = testFunctionImportPost({ simpleParam });

nock(defaultHost)
.get(`${serviceUrl}/TestFunctionImportPOST`)
.query({ SimpleParam: `'${simpleParam}'` })
.head(`${serviceUrl}/TestFunctionImportPOST/`)
.reply(200, undefined, mockedBuildHeaderResponse);

nock(defaultHost)
Expand Down Expand Up @@ -168,9 +167,9 @@ describe('OperationRequestBuilder', () => {
expect(returnValue).toEqual(expected);
});

it('returns undefined or throw in failure case', async () => {
it('returns undefined', async () => {
nock(defaultHost)
.get(`${serviceUrl}/TestFunctionImportNoReturnType`)
.head(`${serviceUrl}/TestFunctionImportNoReturnType/`)
.reply(200, undefined, mockedBuildHeaderResponse);

nock(defaultHost)
Expand All @@ -181,6 +180,12 @@ describe('OperationRequestBuilder', () => {
defaultDestination
);
expect(response).toBe(undefined);
});

it('throws in failure case', async () => {
nock(defaultHost)
.head(`${serviceUrl}/TestFunctionImportNoReturnType/`)
.reply(200, undefined, mockedBuildHeaderResponse);

nock(defaultHost)
.post(`${serviceUrl}/TestFunctionImportNoReturnType`)
Expand Down Expand Up @@ -213,15 +218,14 @@ describe('OperationRequestBuilder', () => {

it('throws an error when shared entity type is used as return type', async () => {
nock(defaultHost)
.get(`${serviceUrl}/TestFunctionImportSharedEntityReturnType()`)
.query({})
.get(`${serviceUrl}/TestFunctionImportSharedEntityReturnType`)
.reply(200, {});
const requestBuilder = testFunctionImportSharedEntityReturnType({}) as any;

await expect(
requestBuilder.execute(defaultDestination)
).rejects.toThrowErrorMatchingInlineSnapshot(
'"get request to http://example.com/sap/opu/odata/sap/API_TEST_SRV failed! "'
"\"Failed to build an entity from the response of the function import or action import: TestFunctionImportSharedEntityReturnType, because the entity type of the return type is shared by multiple entity sets. Please use 'executeRaw' instead of 'execute' to get the raw response. Original response body: {}.\""
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,21 +476,19 @@ describe('UpdateRequestBuilder', () => {
it('returns request and raw response when sending non-key properties', async () => {
const entity = createTestEntity();
entity.booleanProperty = false;
const requestBody = {
const body = {
Int32Property: entity.int32Property,
BooleanProperty: false,
StringProperty: null
};
const response = { d: requestBody };

mockUpdateRequest(
{
body: requestBody,
body,
path: testEntityResourcePath(
entity.keyPropertyGuid,
entity.keyPropertyString
),
responseBody: response
)
},
testEntityApi
);
Expand All @@ -499,8 +497,8 @@ describe('UpdateRequestBuilder', () => {
testEntityApi,
entity
).executeRaw(defaultDestination);
expect(actual!.data).toEqual(response);
expect(actual!.request.method).toEqual('PATCH');
expect(actual?.status).toEqual(204);
expect(actual?.request.method).toEqual('PATCH');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ describe('GetByKeyRequestBuilder', () => {
expect(actual[0].somethingTheSdkDoesNotSupport).toBe('SomeValue');
});

it('ETag should be pulled from @odata.etag', async () => {
it('eTag should be pulled from @odata.etag', async () => {
const entityData = createOriginalTestEntityDataV4_1();
const versionIdentifier = 'etagInMetadata';
const versionIdentifier = 'eTagInMetadata';
entityData['@odata.etag'] = versionIdentifier;
const expected = createTestEntity(entityData);
const response = {
Expand Down Expand Up @@ -129,10 +129,10 @@ describe('GetByKeyRequestBuilder', () => {
expect(actual).toEqual(expected);
});

it('ETag should be pulled from response header when json payload has no @odata.etag property', async () => {
it('eTag should be pulled from response header when json payload has no @odata.etag property', async () => {
const entityData = createOriginalTestEntityDataV4_1();
const expected = createTestEntity(entityData);
const versionIdentifier = 'etagInHeader';
const versionIdentifier = 'eTagInHeader';
expected.setVersionIdentifier(versionIdentifier);
const response = {
KeyPropertyGuid: entityData.KeyPropertyGuid,
Expand All @@ -150,7 +150,7 @@ describe('GetByKeyRequestBuilder', () => {
expected.keyDateProperty
),
responseBody: response,
responseHeaders: { Etag: versionIdentifier }
responseHeaders: { etag: versionIdentifier }
},
testEntityApi
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ describe('operation request builder', () => {

nock(host)
.post(`${basePath}/TestActionImportNoParameterNoReturnType`)
.reply(204, {});
.reply(204);

const actual = await testActionImportNoParameterNoReturnType(
{}
).executeRaw(destination);
expect(actual.data).toEqual({});
expect(actual.status).toEqual(204);
expect(actual.request.method).toBe('POST');
});
});
Expand Down
11 changes: 2 additions & 9 deletions packages/openapi/src/openapi-request-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,7 @@ describe('openapi-request-builder', () => {
.get(/.*/)
.reply(200, 'iss token used on the way')
];
const requestBuilder = new OpenApiRequestBuilder('get', '/test', {
body: {
limit: 100
}
});
const requestBuilder = new OpenApiRequestBuilder('get', '/test');
const response = await requestBuilder.executeRaw({
destinationName: 'ERNIE-UND-CERT',
iss: onlyIssuerXsuaaUrl
Expand All @@ -227,10 +223,7 @@ describe('openapi-request-builder', () => {
middleware: [],
url: '/test',
headers: { requestConfig: {} },
params: { requestConfig: {} },
data: {
limit: 100
}
params: { requestConfig: {} }
},
{ fetchCsrfToken: false }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ describe('odata negative tests', () => {
await promises.mkdir(testOutputRootDir);
}
if (existsSync(testDir)) {
await promises.rmdir(testDir, { recursive: true });
await promises.rm(testDir, { recursive: true });
}
await promises.mkdir(testDir);
});

afterAll(async () => {
await promises.rmdir(testDir, { recursive: true });
await promises.rm(testDir, { recursive: true });
});

it('should fail on faulty edmx', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ describe('openapi negative tests', () => {
await promises.mkdir(testOutputRootDir);
}
if (existsSync(testDir)) {
await promises.rmdir(testDir, { recursive: true });
await promises.rm(testDir, { recursive: true });
}
await promises.mkdir(testDir);
});

afterAll(async () => {
await promises.rmdir(testDir, { recursive: true });
await promises.rm(testDir, { recursive: true });
});

it('should fail on generation for faulty spec file', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test-packages/integration-tests/test/v2/batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function mockCsrfTokenRequest(host: string, sapClient: string) {
'sap-client': sapClient
}
})
.head(`${basePath}/$batch`)
.head(`${basePath}/$batch/`)
.reply(200, '', {
'x-csrf-token': csrfToken,
'Set-Cookie': ['key1=val1', 'key2=val2', 'key3=val3']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function mockCsrfTokenRequest(
})
.head(
path
? `${TestEntity._defaultBasePath}/${path}`
: TestEntity._defaultBasePath
? `${TestEntity._defaultBasePath}/${path}/`
: `${TestEntity._defaultBasePath}/`
)
.reply(200, '', {
'x-csrf-token': csrfToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function mockCsrfTokenRequest(host: string, sapClient: string, path?: string) {
'sap-client': sapClient
}
})
.head(path ? `${basePath}/${path}` : basePath)
.head(path ? `${basePath}/${path}/` : `${basePath}/`)
.reply(200, '', {
'x-csrf-token': csrfToken,
'Set-Cookie': ['key1=val1', 'key2=val2', 'key3=val3']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function mockCsrfTokenRequest(url: string) {
'x-csrf-token': 'Fetch'
}
})
.get(basePath)
.head(`${basePath}/`)
.reply(200, '', {
'x-csrf-token': csrfToken,
'Set-Cookie': ['key1=val1', 'key2=val2', 'key3=val3']
Expand Down
Loading
Loading