Skip to content

Commit 518e222

Browse files
authored
Make headers isomorphic (#87)
Make headers isomorphic Move to cross-fetch Get rid of require statement Import Headers from cross-fetch Part of #86 Acceptance Cleaned! PASS tests/unit/service.test.ts PASS tests/unit/signer.test.ts PASS tests/unit/types.test.ts PASS tests/unit/http.test.ts Test Suites: 4 passed, 4 total Tests: 16 passed, 16 total Snapshots: 0 total Time: 3.249 s, estimated 5 s Ran all test suites matching /.\/tests\/unit/i. PASS tests/integration/client.test.ts ✓ Client: register service (1 ms) ✓ Client: no auth opts (5 ms) ✓ Client: abs URL (2 ms) ✓ Client: abs URL with base (1 ms) ✓ Client: merge handlers ✓ Client: ak/sk auth; request headers (9 ms) ○ skipped Client: authToken ○ skipped Client: authAKSK ○ skipped Client: get not registered service ○ skipped Client: no ak/sk opts ○ skipped Client: ak/sk opts Test Suites: 1 passed, 1 total Tests: 5 skipped, 6 passed, 11 total Snapshots: 0 total Time: 1.447 s, estimated 5 s Ran all test suites matching /.\/tests\/integration/i. PASS tests/functional/services/identity.test.ts PASS tests/functional/client.test.ts (6.194 s) PASS tests/functional/services/compute.test.ts (8.168 s) PASS tests/functional/services/swift.test.ts (8.928 s) PASS tests/functional/services/image.test.ts (40.03 s) ● Console console.log Loaded 7 pages at Pager.<anonymous> (src/oms/services/base.ts:87:17) console.log Loaded 7 pages at Pager.<anonymous> (src/oms/services/base.ts:87:17) PASS tests/functional/services/networkv1.test.ts (51.4 s) Test Suites: 6 passed, 6 total Tests: 6 skipped, 36 passed, 42 total Snapshots: 0 total Time: 51.726 s, estimated 53 s Ran all test suites matching /.\/tests\/functional/i. coverage files in coverage merged into coverage/merged-coverage.json Reviewed-by: Rodion Gyrbu <[email protected]> Reviewed-by: Anton Sidelnikov <None> Reviewed-by: None <None>
1 parent 9a7abf8 commit 518e222

File tree

10 files changed

+72
-60
lines changed

10 files changed

+72
-60
lines changed

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opentelekomcloud/oms",
3-
"version": "0.1.3",
3+
"version": "0.1.5-beta.1",
44
"description": "Micro SDK for OpenTelekomCloud",
55
"repository": {
66
"type": "git",
@@ -59,7 +59,10 @@
5959
"collectCoverageFrom": [
6060
"src/**/*.ts"
6161
],
62-
"testEnvironment": "node"
62+
"testEnvironment": "node",
63+
"setupFiles": [
64+
"./tests/setupJest.ts"
65+
]
6366
},
6467
"scripts": {
6568
"cov-rename": "mv ./coverage/coverage-final.json ./coverage/coverage-$0.json",
@@ -76,8 +79,8 @@
7679
"release": "yarn clean && yarn build && webpack --mode production"
7780
},
7881
"dependencies": {
82+
"cross-fetch": "^3.1.4",
7983
"is-cidr": "^4.0.2",
80-
"isomorphic-fetch": "^3.0.0",
8184
"lodash": "^4.17.20",
8285
"query-string": "^6.13.2"
8386
},

src/oms/core/http.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import isEmpty from 'lodash/isEmpty'
77
import { ParsedQuery, stringifyUrl } from 'query-string'
88
import { validate } from 'json-schema'
99
import { JSONSchema } from './types'
10-
11-
require('isomorphic-fetch')
10+
import fetch, { Headers } from 'cross-fetch'
1211

1312
export type RequestConfigHandler = (i: RequestOpts) => RequestOpts
1413

src/oms/core/signer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sha256, { hmac } from 'fast-sha256'
2+
import { Headers } from 'cross-fetch'
23

34
export interface CredentialInfo {
45
readonly accessKeyId: string,

src/oms/services/object-storage/swift/v1/accounts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import HttpClient from '../../../../core/http'
2-
import { AccountMetadata, Account, ContainerMetadata } from './types'
2+
import { Account, AccountMetadata, ContainerMetadata } from './types'
33
import { Metadata } from '../../../../core'
4+
import { Headers } from 'cross-fetch'
45

56
const url = ''
67

src/oms/services/object-storage/swift/v1/container.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import HttpClient, { joinURL } from '../../../../core/http'
22
import { Metadata } from '../../../../core'
33
import { Container, ContainerMetadata, ObjectEntity, ObjectListOpts } from './types'
4+
import { Headers } from 'cross-fetch'
45

56
const url = ''
67

tests/integration/client.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ import { authServerUrl, fakeAuthServer, fakeServiceServer, fakeToken } from '../
33
import { json, randomString } from '../utils/helpers'
44
import Service from '../../src/oms/services/base'
55
import HttpClient from '../../src/oms/core/http'
6-
import { disableFetchMocks, enableFetchMocks } from 'jest-fetch-mock'
6+
import fetchMock from 'jest-fetch-mock'
77

88
beforeAll(() => {
99
fakeAuthServer.listen()
1010
fakeServiceServer.listen()
11-
enableFetchMocks()
11+
fetchMock.enableMocks()
12+
})
13+
14+
beforeEach(() => {
15+
fetchMock.resetMocks()
1216
})
1317

1418
afterAll(() => {
1519
fakeAuthServer.close()
1620
fakeServiceServer.close()
17-
disableFetchMocks()
21+
fetchMock.disableMocks()
1822
})
1923

2024
test.skip('Client: authToken', async () => {

tests/setupJest.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Thanks to @ctaylo21 at https://github.com/jefflau/jest-fetch-mock/issues/122#issuecomment-542273089
2+
3+
import { GlobalWithFetchMock } from 'jest-fetch-mock';
4+
5+
const customGlobal = global as unknown as GlobalWithFetchMock;
6+
customGlobal.fetch = require('jest-fetch-mock');
7+
customGlobal.fetchMock = customGlobal.fetch;
8+
9+
jest.setMock('cross-fetch', fetch);
10+
fetchMock.dontMock()

tests/unit/http.test.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import HttpClient, { mergeHeaders, RequestOpts } from '../../src/oms/core/http'
22
import { Client, cloud } from '../../src/oms'
33

4-
import { disableFetchMocks, enableFetchMocks } from 'jest-fetch-mock'
4+
import fetchMock from 'jest-fetch-mock'
55
import { json } from '../utils/helpers'
66

77
test('RequestOpts: nothing', () => {
@@ -50,31 +50,41 @@ test('Client: header merging', () => {
5050

5151
const simpleBody = '{"token": {"user": {"domain": {"id": ""}}, "catalog": []}}'
5252

53-
test('Client: required headers', async () => {
54-
const authUrl = 'https://google.com/'
55-
const config = cloud(authUrl).withToken('t').config
56-
const client = new Client(config)
57-
enableFetchMocks()
58-
fetchMock.mockOnce(async r => {
59-
expect(r.headers.get('Accept')).toBeDefined()
60-
expect(r.headers.get('Content-Type')).toBeDefined()
61-
expect(r.headers.get('Host')).toBeDefined()
62-
expect(r.headers.get('User-Agent')).toBeDefined()
63-
return json(simpleBody)
53+
describe('Test requests', () => {
54+
beforeAll(() => {
55+
fetchMock.enableMocks()
56+
})
57+
beforeEach(() => {
58+
fetchMock.resetMocks()
59+
})
60+
afterAll(() => {
61+
fetchMock.disableMocks()
6462
})
65-
await client.authenticate()
66-
disableFetchMocks()
67-
})
6863

69-
test('Client: complex URL', async () => {
70-
const authUrl = 'https://rtest.outcatcher.com/meta/proxy/https:/iam.eu-de.otc.t-systems.com/v3'
71-
const config = cloud(authUrl).withToken('t').config
72-
const client = new Client(config)
73-
enableFetchMocks()
74-
fetchMock.mockOnce(async r => {
75-
expect(r.url).toBe(authUrl + '/auth/tokens')
76-
return json(simpleBody)
64+
65+
test('Client: required headers', async () => {
66+
const authUrl = 'https://acme.com/'
67+
const config = cloud(authUrl).withToken('t').config
68+
const client = new Client(config)
69+
fetchMock.mockOnce(async r => {
70+
expect(r.headers.get('Accept')).toBeDefined()
71+
expect(r.headers.get('Content-Type')).toBeDefined()
72+
expect(r.headers.get('Host')).toBeDefined()
73+
expect(r.headers.get('User-Agent')).toBeDefined()
74+
return json(simpleBody)
75+
})
76+
await client.authenticate()
77+
})
78+
79+
test('Client: complex URL', async () => {
80+
const authUrl = 'https://rtest.outcatcher.com/meta/proxy/https:/iam.eu-de.otc.t-systems.com/v3'
81+
const config = cloud(authUrl).withToken('t').config
82+
const client = new Client(config)
83+
fetchMock.mockOnce(async r => {
84+
expect(r.url).toBe(authUrl + '/auth/tokens')
85+
return json(simpleBody)
86+
})
87+
await client.authenticate()
7788
})
78-
await client.authenticate()
79-
disableFetchMocks()
8089
})
90+

tests/unit/signer.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { getSignHeaders } from '../../src/oms'
2-
2+
import { Headers } from 'cross-fetch'
33

44
test('aws-signature test', () => {
55
const myUrl = new URL('https://iam.eu-de.otc.t-systems.com/v3/projects?name=eu-de_test_dmd')
66
const date = new Date('Wed, 21 Oct 2020 11:54:11 GMT')
77
const headers = new Headers()
88
headers.set('accept', 'application/json')
9-
headers.set( 'user-agent', 'OpenTelekomCloud JS/v1.0' )
10-
headers.set( 'content-type', 'application/json' )
9+
headers.set('user-agent', 'OpenTelekomCloud JS/v1.0')
10+
headers.set('content-type', 'application/json')
1111
const signedHeaderGet = getSignHeaders(
1212
{
1313
accessKeyId: 'AKIDEXAMPLE',
1414
secretAccessKey: 'BYBYIiF3WUZGlorXmcTEDtNjB40JTibEXAMPLE',
15-
regionName: ''
15+
regionName: '',
1616
},
1717
{
1818
method: 'GET',
1919
url: myUrl,
2020
serviceName: '',
2121
headers: headers,
2222
},
23-
date
24-
);
23+
date,
24+
)
2525
expect(signedHeaderGet.Authorization).toBe(
2626
'SDK-HMAC-SHA256 Credential=AKIDEXAMPLE/20201021///sdk_request,' +
2727
' SignedHeaders=accept;content-type;host;user-agent;x-sdk-date,' +

yarn.lock

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,13 @@ __metadata:
714714
babel-register: ^6.26.0
715715
browserify: ^16.5.2
716716
codecov: ^3.7.2
717+
cross-fetch: ^3.1.4
717718
eslint: ^7.10.0
718719
eslint-import-resolver-typescript: ^2.3.0
719720
eslint-plugin-import: ^2.22.1
720721
eslint-plugin-node: ^11.1.0
721722
fast-sha256: ^1.3.0
722723
is-cidr: ^4.0.2
723-
isomorphic-fetch: ^3.0.0
724724
jest: ^26.6.3
725725
jest-fetch-mock: ^3.0.3
726726
jsonschema: ^1.2.7
@@ -3297,7 +3297,7 @@ __metadata:
32973297
languageName: node
32983298
linkType: hard
32993299

3300-
"cross-fetch@npm:^3.0.4":
3300+
"cross-fetch@npm:^3.0.4, cross-fetch@npm:^3.1.4":
33013301
version: 3.1.4
33023302
resolution: "cross-fetch@npm:3.1.4"
33033303
dependencies:
@@ -5784,16 +5784,6 @@ fsevents@^1.2.7:
57845784
languageName: node
57855785
linkType: hard
57865786

5787-
"isomorphic-fetch@npm:^3.0.0":
5788-
version: 3.0.0
5789-
resolution: "isomorphic-fetch@npm:3.0.0"
5790-
dependencies:
5791-
node-fetch: ^2.6.1
5792-
whatwg-fetch: ^3.4.1
5793-
checksum: e5ab79a56ce5af6ddd21265f59312ad9a4bc5a72cebc98b54797b42cb30441d5c5f8d17c5cd84a99e18101c8af6f90c081ecb8d12fd79e332be1778d58486d75
5794-
languageName: node
5795-
linkType: hard
5796-
57975787
"isstream@npm:~0.1.2":
57985788
version: 0.1.2
57995789
resolution: "isstream@npm:0.1.2"
@@ -7293,7 +7283,7 @@ fsevents@^1.2.7:
72937283
languageName: node
72947284
linkType: hard
72957285

7296-
"node-fetch@npm:2.6.1, node-fetch@npm:^2.2.0, node-fetch@npm:^2.6.1":
7286+
"node-fetch@npm:2.6.1, node-fetch@npm:^2.2.0":
72977287
version: 2.6.1
72987288
resolution: "node-fetch@npm:2.6.1"
72997289
checksum: 91075bedd57879117e310fbcc36983ad5d699e522edb1ebcdc4ee5294c982843982652925c3532729fdc86b2d64a8a827797a745f332040d91823c8752ee4d7c
@@ -10529,13 +10519,6 @@ webpack@webpack-4:
1052910519
languageName: node
1053010520
linkType: hard
1053110521

10532-
"whatwg-fetch@npm:^3.4.1":
10533-
version: 3.6.2
10534-
resolution: "whatwg-fetch@npm:3.6.2"
10535-
checksum: ee976b7249e7791edb0d0a62cd806b29006ad7ec3a3d89145921ad8c00a3a67e4be8f3fb3ec6bc7b58498724fd568d11aeeeea1f7827e7e1e5eae6c8a275afed
10536-
languageName: node
10537-
linkType: hard
10538-
1053910522
"whatwg-mimetype@npm:^2.3.0":
1054010523
version: 2.3.0
1054110524
resolution: "whatwg-mimetype@npm:2.3.0"

0 commit comments

Comments
 (0)