Skip to content

Commit

Permalink
Request discovery endpoint in cloud env
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarhane committed Oct 28, 2019
1 parent abe5248 commit 467a061
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/shared/modules/app/appDuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/shared/modules/discovery/discoveryDuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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' })
Expand Down
50 changes: 48 additions & 2 deletions src/shared/modules/discovery/discoveryDuck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -38,7 +38,10 @@ describe('discoveryOnStartupEpic', () => {
])
beforeAll(() => {
store = mockStore({
connections: {}
connections: {},
app: {
env: WEB
}
})
})
afterEach(() => {
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions src/shared/services/bolt/boltHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}/`
}

Expand Down

0 comments on commit 467a061

Please sign in to comment.