diff --git a/package.json b/package.json index ace2322..3628dee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@opentelekomcloud/oms", - "version": "0.1.3", + "version": "0.1.5-beta.1", "description": "Micro SDK for OpenTelekomCloud", "repository": { "type": "git", @@ -59,7 +59,10 @@ "collectCoverageFrom": [ "src/**/*.ts" ], - "testEnvironment": "node" + "testEnvironment": "node", + "setupFiles": [ + "./tests/setupJest.ts" + ] }, "scripts": { "cov-rename": "mv ./coverage/coverage-final.json ./coverage/coverage-$0.json", @@ -76,8 +79,8 @@ "release": "yarn clean && yarn build && webpack --mode production" }, "dependencies": { + "cross-fetch": "^3.1.4", "is-cidr": "^4.0.2", - "isomorphic-fetch": "^3.0.0", "lodash": "^4.17.20", "query-string": "^6.13.2" }, diff --git a/src/oms/core/http.ts b/src/oms/core/http.ts index 5cb3123..2a69ee7 100644 --- a/src/oms/core/http.ts +++ b/src/oms/core/http.ts @@ -7,8 +7,7 @@ import isEmpty from 'lodash/isEmpty' import { ParsedQuery, stringifyUrl } from 'query-string' import { validate } from 'json-schema' import { JSONSchema } from './types' - -require('isomorphic-fetch') +import fetch, { Headers } from 'cross-fetch' export type RequestConfigHandler = (i: RequestOpts) => RequestOpts diff --git a/src/oms/core/signer.ts b/src/oms/core/signer.ts index 54ed44b..793cb9a 100644 --- a/src/oms/core/signer.ts +++ b/src/oms/core/signer.ts @@ -1,4 +1,5 @@ import sha256, { hmac } from 'fast-sha256' +import { Headers } from 'cross-fetch' export interface CredentialInfo { readonly accessKeyId: string, diff --git a/src/oms/services/object-storage/swift/v1/accounts.ts b/src/oms/services/object-storage/swift/v1/accounts.ts index 19a6e83..fe1da8f 100644 --- a/src/oms/services/object-storage/swift/v1/accounts.ts +++ b/src/oms/services/object-storage/swift/v1/accounts.ts @@ -1,6 +1,7 @@ import HttpClient from '../../../../core/http' -import { AccountMetadata, Account, ContainerMetadata } from './types' +import { Account, AccountMetadata, ContainerMetadata } from './types' import { Metadata } from '../../../../core' +import { Headers } from 'cross-fetch' const url = '' diff --git a/src/oms/services/object-storage/swift/v1/container.ts b/src/oms/services/object-storage/swift/v1/container.ts index 16e7573..e474d62 100644 --- a/src/oms/services/object-storage/swift/v1/container.ts +++ b/src/oms/services/object-storage/swift/v1/container.ts @@ -1,6 +1,7 @@ import HttpClient, { joinURL } from '../../../../core/http' import { Metadata } from '../../../../core' import { Container, ContainerMetadata, ObjectEntity, ObjectListOpts } from './types' +import { Headers } from 'cross-fetch' const url = '' diff --git a/tests/integration/client.test.ts b/tests/integration/client.test.ts index 2c721e6..e902de1 100644 --- a/tests/integration/client.test.ts +++ b/tests/integration/client.test.ts @@ -3,18 +3,22 @@ import { authServerUrl, fakeAuthServer, fakeServiceServer, fakeToken } from '../ import { json, randomString } from '../utils/helpers' import Service from '../../src/oms/services/base' import HttpClient from '../../src/oms/core/http' -import { disableFetchMocks, enableFetchMocks } from 'jest-fetch-mock' +import fetchMock from 'jest-fetch-mock' beforeAll(() => { fakeAuthServer.listen() fakeServiceServer.listen() - enableFetchMocks() + fetchMock.enableMocks() +}) + +beforeEach(() => { + fetchMock.resetMocks() }) afterAll(() => { fakeAuthServer.close() fakeServiceServer.close() - disableFetchMocks() + fetchMock.disableMocks() }) test.skip('Client: authToken', async () => { diff --git a/tests/setupJest.ts b/tests/setupJest.ts new file mode 100644 index 0000000..240c96c --- /dev/null +++ b/tests/setupJest.ts @@ -0,0 +1,10 @@ +// Thanks to @ctaylo21 at https://github.com/jefflau/jest-fetch-mock/issues/122#issuecomment-542273089 + +import { GlobalWithFetchMock } from 'jest-fetch-mock'; + +const customGlobal = global as unknown as GlobalWithFetchMock; +customGlobal.fetch = require('jest-fetch-mock'); +customGlobal.fetchMock = customGlobal.fetch; + +jest.setMock('cross-fetch', fetch); +fetchMock.dontMock() diff --git a/tests/unit/http.test.ts b/tests/unit/http.test.ts index 4317947..3821440 100644 --- a/tests/unit/http.test.ts +++ b/tests/unit/http.test.ts @@ -1,7 +1,7 @@ import HttpClient, { mergeHeaders, RequestOpts } from '../../src/oms/core/http' import { Client, cloud } from '../../src/oms' -import { disableFetchMocks, enableFetchMocks } from 'jest-fetch-mock' +import fetchMock from 'jest-fetch-mock' import { json } from '../utils/helpers' test('RequestOpts: nothing', () => { @@ -50,31 +50,41 @@ test('Client: header merging', () => { const simpleBody = '{"token": {"user": {"domain": {"id": ""}}, "catalog": []}}' -test('Client: required headers', async () => { - const authUrl = 'https://google.com/' - const config = cloud(authUrl).withToken('t').config - const client = new Client(config) - enableFetchMocks() - fetchMock.mockOnce(async r => { - expect(r.headers.get('Accept')).toBeDefined() - expect(r.headers.get('Content-Type')).toBeDefined() - expect(r.headers.get('Host')).toBeDefined() - expect(r.headers.get('User-Agent')).toBeDefined() - return json(simpleBody) +describe('Test requests', () => { + beforeAll(() => { + fetchMock.enableMocks() + }) + beforeEach(() => { + fetchMock.resetMocks() + }) + afterAll(() => { + fetchMock.disableMocks() }) - await client.authenticate() - disableFetchMocks() -}) -test('Client: complex URL', async () => { - const authUrl = 'https://rtest.outcatcher.com/meta/proxy/https:/iam.eu-de.otc.t-systems.com/v3' - const config = cloud(authUrl).withToken('t').config - const client = new Client(config) - enableFetchMocks() - fetchMock.mockOnce(async r => { - expect(r.url).toBe(authUrl + '/auth/tokens') - return json(simpleBody) + + test('Client: required headers', async () => { + const authUrl = 'https://acme.com/' + const config = cloud(authUrl).withToken('t').config + const client = new Client(config) + fetchMock.mockOnce(async r => { + expect(r.headers.get('Accept')).toBeDefined() + expect(r.headers.get('Content-Type')).toBeDefined() + expect(r.headers.get('Host')).toBeDefined() + expect(r.headers.get('User-Agent')).toBeDefined() + return json(simpleBody) + }) + await client.authenticate() + }) + + test('Client: complex URL', async () => { + const authUrl = 'https://rtest.outcatcher.com/meta/proxy/https:/iam.eu-de.otc.t-systems.com/v3' + const config = cloud(authUrl).withToken('t').config + const client = new Client(config) + fetchMock.mockOnce(async r => { + expect(r.url).toBe(authUrl + '/auth/tokens') + return json(simpleBody) + }) + await client.authenticate() }) - await client.authenticate() - disableFetchMocks() }) + diff --git a/tests/unit/signer.test.ts b/tests/unit/signer.test.ts index d205d4d..c329a1c 100644 --- a/tests/unit/signer.test.ts +++ b/tests/unit/signer.test.ts @@ -1,18 +1,18 @@ import { getSignHeaders } from '../../src/oms' - +import { Headers } from 'cross-fetch' test('aws-signature test', () => { const myUrl = new URL('https://iam.eu-de.otc.t-systems.com/v3/projects?name=eu-de_test_dmd') const date = new Date('Wed, 21 Oct 2020 11:54:11 GMT') const headers = new Headers() headers.set('accept', 'application/json') - headers.set( 'user-agent', 'OpenTelekomCloud JS/v1.0' ) - headers.set( 'content-type', 'application/json' ) + headers.set('user-agent', 'OpenTelekomCloud JS/v1.0') + headers.set('content-type', 'application/json') const signedHeaderGet = getSignHeaders( { accessKeyId: 'AKIDEXAMPLE', secretAccessKey: 'BYBYIiF3WUZGlorXmcTEDtNjB40JTibEXAMPLE', - regionName: '' + regionName: '', }, { method: 'GET', @@ -20,8 +20,8 @@ test('aws-signature test', () => { serviceName: '', headers: headers, }, - date - ); + date, + ) expect(signedHeaderGet.Authorization).toBe( 'SDK-HMAC-SHA256 Credential=AKIDEXAMPLE/20201021///sdk_request,' + ' SignedHeaders=accept;content-type;host;user-agent;x-sdk-date,' + diff --git a/yarn.lock b/yarn.lock index 99f8c7e..cb72cc5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -714,13 +714,13 @@ __metadata: babel-register: ^6.26.0 browserify: ^16.5.2 codecov: ^3.7.2 + cross-fetch: ^3.1.4 eslint: ^7.10.0 eslint-import-resolver-typescript: ^2.3.0 eslint-plugin-import: ^2.22.1 eslint-plugin-node: ^11.1.0 fast-sha256: ^1.3.0 is-cidr: ^4.0.2 - isomorphic-fetch: ^3.0.0 jest: ^26.6.3 jest-fetch-mock: ^3.0.3 jsonschema: ^1.2.7 @@ -3297,7 +3297,7 @@ __metadata: languageName: node linkType: hard -"cross-fetch@npm:^3.0.4": +"cross-fetch@npm:^3.0.4, cross-fetch@npm:^3.1.4": version: 3.1.4 resolution: "cross-fetch@npm:3.1.4" dependencies: @@ -5784,16 +5784,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"isomorphic-fetch@npm:^3.0.0": - version: 3.0.0 - resolution: "isomorphic-fetch@npm:3.0.0" - dependencies: - node-fetch: ^2.6.1 - whatwg-fetch: ^3.4.1 - checksum: e5ab79a56ce5af6ddd21265f59312ad9a4bc5a72cebc98b54797b42cb30441d5c5f8d17c5cd84a99e18101c8af6f90c081ecb8d12fd79e332be1778d58486d75 - languageName: node - linkType: hard - "isstream@npm:~0.1.2": version: 0.1.2 resolution: "isstream@npm:0.1.2" @@ -7293,7 +7283,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"node-fetch@npm:2.6.1, node-fetch@npm:^2.2.0, node-fetch@npm:^2.6.1": +"node-fetch@npm:2.6.1, node-fetch@npm:^2.2.0": version: 2.6.1 resolution: "node-fetch@npm:2.6.1" checksum: 91075bedd57879117e310fbcc36983ad5d699e522edb1ebcdc4ee5294c982843982652925c3532729fdc86b2d64a8a827797a745f332040d91823c8752ee4d7c @@ -10529,13 +10519,6 @@ webpack@webpack-4: languageName: node linkType: hard -"whatwg-fetch@npm:^3.4.1": - version: 3.6.2 - resolution: "whatwg-fetch@npm:3.6.2" - checksum: ee976b7249e7791edb0d0a62cd806b29006ad7ec3a3d89145921ad8c00a3a67e4be8f3fb3ec6bc7b58498724fd568d11aeeeea1f7827e7e1e5eae6c8a275afed - languageName: node - linkType: hard - "whatwg-mimetype@npm:^2.3.0": version: 2.3.0 resolution: "whatwg-mimetype@npm:2.3.0"