diff --git a/src/shared/modules/app/appDuck.js b/src/shared/modules/app/appDuck.js index 0614ddd14fe..fdbd7788418 100644 --- a/src/shared/modules/app/appDuck.js +++ b/src/shared/modules/app/appDuck.js @@ -33,6 +33,8 @@ export const CLOUD = 'CLOUD' // Selectors export const getHostedUrl = state => (state[NAME] || {}).hostedUrl || null export const getEnv = state => (state[NAME] || {}).env || WEB +export const hasDiscoveryEndpoint = state => + [WEB, CLOUD].includes(getEnv(state)) export const inWebEnv = state => getEnv(state) === WEB // Reducer diff --git a/src/shared/modules/discovery/discoveryDuck.js b/src/shared/modules/discovery/discoveryDuck.js index e478e9bbc2f..9a3d26a9783 100644 --- a/src/shared/modules/discovery/discoveryDuck.js +++ b/src/shared/modules/discovery/discoveryDuck.js @@ -21,7 +21,12 @@ import Rx from 'rxjs/Rx' import remote from 'services/remote' import { updateConnection } from 'shared/modules/connections/connectionsDuck' -import { APP_START, USER_CLEAR, inWebEnv } from 'shared/modules/app/appDuck' +import { + APP_START, + USER_CLEAR, + hasDiscoveryEndpoint, + getHostedUrl +} from 'shared/modules/app/appDuck' import { getDiscoveryEndpoint } from 'services/bolt/boltHelpers' import { getUrlParamValue } from 'services/utils' import { getUrlInfo } from 'shared/services/utils' @@ -105,7 +110,10 @@ export const discoveryOnStartupEpic = (some$, store) => { }) .merge(some$.ofType(USER_CLEAR)) .mergeMap(action => { - if (!inWebEnv(store.getState())) return Promise.resolve({ type: 'NOOP' }) // Only when in a web environment + // Only when in a environment were we can guess discovery endpoint + if (!hasDiscoveryEndpoint(store.getState())) { + return Promise.resolve({ type: 'NOOP' }) + } if (action.forceURL) { const { username, password, protocol, host } = getUrlInfo( action.forceURL @@ -123,7 +131,7 @@ export const discoveryOnStartupEpic = (some$, store) => { } return Rx.Observable.fromPromise( remote - .getJSON(getDiscoveryEndpoint()) + .getJSON(getDiscoveryEndpoint(getHostedUrl(store.getState()))) // Uncomment below and comment out above when doing manual tests in dev mode to // fake discovery response // Promise.resolve({ bolt: 'bolt+routing://localhost:7687' }) diff --git a/src/shared/modules/discovery/discoveryDuck.test.js b/src/shared/modules/discovery/discoveryDuck.test.js index 79a44c14eff..c292d5f03b7 100644 --- a/src/shared/modules/discovery/discoveryDuck.test.js +++ b/src/shared/modules/discovery/discoveryDuck.test.js @@ -25,7 +25,7 @@ import { createBus, createReduxMiddleware } from 'suber' import nock from 'nock' import * as discovery from './discoveryDuck' -import { APP_START, WEB } from 'shared/modules/app/appDuck' +import { APP_START, WEB, CLOUD } from 'shared/modules/app/appDuck' import { getDiscoveryEndpoint } from 'services/bolt/boltHelpers' describe('discoveryOnStartupEpic', () => { @@ -38,7 +38,10 @@ describe('discoveryOnStartupEpic', () => { ]) beforeAll(() => { store = mockStore({ - connections: {} + connections: {}, + app: { + env: WEB + } }) }) afterEach(() => { @@ -168,6 +171,49 @@ describe('discoveryOnStartupEpic', () => { }) }) +describe('discoveryOnStartupEpic cloud env', () => { + let store + const bus = createBus() + const epicMiddleware = createEpicMiddleware(discovery.discoveryOnStartupEpic) + const mockStore = configureMockStore([ + epicMiddleware, + createReduxMiddleware(bus) + ]) + beforeAll(() => { + store = mockStore({ + connections: {}, + app: { + env: CLOUD + } + }) + }) + afterEach(() => { + nock.cleanAll() + bus.reset() + store.clearActions() + }) + test('listens on APP_START and finds a bolt host and dispatches an action with the found host in cloud env', done => { + // Given + const expectedHost = 'bolt://myhost:7777' + const action = { type: APP_START, env: CLOUD } + nock(getDiscoveryEndpoint()) + .get('/') + .reply(200, { bolt: expectedHost }) + bus.take(discovery.DONE, currentAction => { + // Then + expect(store.getActions()).toEqual([ + action, + discovery.updateDiscoveryConnection({ host: expectedHost }), + { type: discovery.DONE } + ]) + done() + }) + + // When + store.dispatch(action) + }) +}) + describe('injectDiscoveryEpic', () => { let store const bus = createBus() diff --git a/src/shared/services/bolt/boltHelpers.js b/src/shared/services/bolt/boltHelpers.js index 7d8253be856..e16a7a89b4b 100644 --- a/src/shared/services/bolt/boltHelpers.js +++ b/src/shared/services/bolt/boltHelpers.js @@ -31,9 +31,8 @@ export const getEncryptionMode = options => { return location.protocol === 'https:' } -export const getDiscoveryEndpoint = () => { - const url = location.host ? location.href : 'http://localhost:7474/' - const info = getUrlInfo(url) +export const getDiscoveryEndpoint = url => { + const info = getUrlInfo(url || 'http://localhost:7474/') return `${info.protocol}//${info.host}/` }