Skip to content

Commit

Permalink
Make headers isomorphic (#87)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
outcatcher authored Jul 9, 2021
1 parent 9a7abf8 commit 518e222
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 60 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
3 changes: 1 addition & 2 deletions src/oms/core/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/oms/core/signer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sha256, { hmac } from 'fast-sha256'
import { Headers } from 'cross-fetch'

export interface CredentialInfo {
readonly accessKeyId: string,
Expand Down
3 changes: 2 additions & 1 deletion src/oms/services/object-storage/swift/v1/accounts.ts
Original file line number Diff line number Diff line change
@@ -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 = ''

Expand Down
1 change: 1 addition & 0 deletions src/oms/services/object-storage/swift/v1/container.ts
Original file line number Diff line number Diff line change
@@ -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 = ''

Expand Down
10 changes: 7 additions & 3 deletions tests/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
10 changes: 10 additions & 0 deletions tests/setupJest.ts
Original file line number Diff line number Diff line change
@@ -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()
60 changes: 35 additions & 25 deletions tests/unit/http.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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()
})

12 changes: 6 additions & 6 deletions tests/unit/signer.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
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',
url: myUrl,
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,' +
Expand Down
23 changes: 3 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 518e222

Please sign in to comment.