From 2cd9a05329d0937f5d81c73570edc8aeb19f1778 Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Fri, 11 Dec 2020 17:09:46 -0500 Subject: [PATCH] Support ws or wss proto --- awx/ui_next/src/components/JobList/useWsJobs.test.jsx | 6 +++--- .../Inventory/InventoryList/useWsInventories.test.jsx | 10 +++++----- .../InventorySources/useWsInventorySources.test.jsx | 4 ++-- awx/ui_next/src/screens/Job/JobOutput/JobOutput.jsx | 6 +++++- .../screens/Project/ProjectList/useWsProjects.test.jsx | 4 ++-- .../useWsWorkflowApprovals.test.jsx | 8 ++++---- awx/ui_next/src/util/useWebsocket.js | 6 +++++- awx/ui_next/src/util/useWsTemplates.test.jsx | 6 +++--- 8 files changed, 29 insertions(+), 21 deletions(-) diff --git a/awx/ui_next/src/components/JobList/useWsJobs.test.jsx b/awx/ui_next/src/components/JobList/useWsJobs.test.jsx index 77e5b714a3..150009c808 100644 --- a/awx/ui_next/src/components/JobList/useWsJobs.test.jsx +++ b/awx/ui_next/src/components/JobList/useWsJobs.test.jsx @@ -44,7 +44,7 @@ describe('useWsJobs hook', () => { test('should establish websocket connection', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const jobs = [{ id: 1 }]; await act(async () => { @@ -67,7 +67,7 @@ describe('useWsJobs hook', () => { test('should update job status', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const jobs = [{ id: 1, status: 'running' }]; await act(async () => { @@ -105,7 +105,7 @@ describe('useWsJobs hook', () => { test('should fetch new job', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const jobs = [{ id: 1 }]; const fetch = jest.fn(() => []); await act(async () => { diff --git a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx index fa18e5b175..7841d71fa9 100644 --- a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx +++ b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx @@ -59,7 +59,7 @@ describe('useWsInventories hook', () => { test('should establish websocket connection', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const inventories = [{ id: 1 }]; await act(async () => { @@ -83,7 +83,7 @@ describe('useWsInventories hook', () => { test('should update inventory sync status', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const inventories = [{ id: 1 }]; await act(async () => { @@ -121,7 +121,7 @@ describe('useWsInventories hook', () => { test('should fetch fresh inventory after sync runs', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const inventories = [{ id: 1 }]; const fetchInventories = jest.fn(() => []); const fetchInventoriesById = jest.fn(() => []); @@ -152,7 +152,7 @@ describe('useWsInventories hook', () => { test('should update inventory pending_deletion', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const inventories = [{ id: 1, pending_deletion: false }]; await act(async () => { @@ -190,7 +190,7 @@ describe('useWsInventories hook', () => { test('should refetch inventories after an inventory is deleted', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const inventories = [{ id: 1 }, { id: 2 }]; const fetchInventories = jest.fn(() => []); const fetchInventoriesById = jest.fn(() => []); diff --git a/awx/ui_next/src/screens/Inventory/InventorySources/useWsInventorySources.test.jsx b/awx/ui_next/src/screens/Inventory/InventorySources/useWsInventorySources.test.jsx index b0e5668624..dcca078a5e 100644 --- a/awx/ui_next/src/screens/Inventory/InventorySources/useWsInventorySources.test.jsx +++ b/awx/ui_next/src/screens/Inventory/InventorySources/useWsInventorySources.test.jsx @@ -43,7 +43,7 @@ describe('useWsInventorySources hook', () => { test('should establish websocket connection', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const sources = [{ id: 1 }]; await act(async () => { @@ -65,7 +65,7 @@ describe('useWsInventorySources hook', () => { test('should update last job status', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const sources = [ { diff --git a/awx/ui_next/src/screens/Job/JobOutput/JobOutput.jsx b/awx/ui_next/src/screens/Job/JobOutput/JobOutput.jsx index 4d78d3b364..2bc991b969 100644 --- a/awx/ui_next/src/screens/Job/JobOutput/JobOutput.jsx +++ b/awx/ui_next/src/screens/Job/JobOutput/JobOutput.jsx @@ -170,7 +170,11 @@ const OutputFooter = styled.div` let ws; function connectJobSocket({ type, id }, onMessage) { - ws = new WebSocket(`wss://${window.location.host}/websocket/`); + ws = new WebSocket( + `${window.location.protocol === 'http:' ? 'ws:' : 'wss:'}//${ + window.location.host + }/websocket/` + ); ws.onopen = () => { const xrftoken = `; ${document.cookie}` diff --git a/awx/ui_next/src/screens/Project/ProjectList/useWsProjects.test.jsx b/awx/ui_next/src/screens/Project/ProjectList/useWsProjects.test.jsx index e32a31ab70..0a41c39689 100644 --- a/awx/ui_next/src/screens/Project/ProjectList/useWsProjects.test.jsx +++ b/awx/ui_next/src/screens/Project/ProjectList/useWsProjects.test.jsx @@ -36,7 +36,7 @@ describe('useWsProjects', () => { test('should establish websocket connection', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const projects = [{ id: 1 }]; await act(async () => { @@ -58,7 +58,7 @@ describe('useWsProjects', () => { test('should update project status', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const projects = [ { diff --git a/awx/ui_next/src/screens/WorkflowApproval/WorkflowApprovalList/useWsWorkflowApprovals.test.jsx b/awx/ui_next/src/screens/WorkflowApproval/WorkflowApprovalList/useWsWorkflowApprovals.test.jsx index 49a7f44acd..49d670964f 100644 --- a/awx/ui_next/src/screens/WorkflowApproval/WorkflowApprovalList/useWsWorkflowApprovals.test.jsx +++ b/awx/ui_next/src/screens/WorkflowApproval/WorkflowApprovalList/useWsWorkflowApprovals.test.jsx @@ -53,7 +53,7 @@ describe('useWsWorkflowApprovals hook', () => { test('should establish websocket connection', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const workflowApprovals = [{ id: 1, status: 'successful' }]; await act(async () => { @@ -79,7 +79,7 @@ describe('useWsWorkflowApprovals hook', () => { test('should refetch after new approval job is created', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const workflowApprovals = [{ id: 1, status: 'successful' }]; const fetchWorkflowApprovals = jest.fn(() => []); await act(async () => { @@ -107,7 +107,7 @@ describe('useWsWorkflowApprovals hook', () => { test('should refetch after approval job in current list is updated', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const workflowApprovals = [{ id: 1, status: 'pending' }]; const fetchWorkflowApprovals = jest.fn(() => []); await act(async () => { @@ -135,7 +135,7 @@ describe('useWsWorkflowApprovals hook', () => { test('should not refetch when message is not workflow approval', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const workflowApprovals = [{ id: 1, status: 'successful' }]; const fetchWorkflowApprovals = jest.fn(() => []); await act(async () => { diff --git a/awx/ui_next/src/util/useWebsocket.js b/awx/ui_next/src/util/useWebsocket.js index c04d086f35..b2e34916e6 100644 --- a/awx/ui_next/src/util/useWebsocket.js +++ b/awx/ui_next/src/util/useWebsocket.js @@ -5,7 +5,11 @@ export default function useWebsocket(subscribeGroups) { const ws = useRef(null); useEffect(function setupSocket() { - ws.current = new WebSocket(`wss://${window.location.host}/websocket/`); + ws.current = new WebSocket( + `${window.location.protocol === 'http:' ? 'ws:' : 'wss:'}//${ + window.location.host + }/websocket/` + ); const connect = () => { const xrftoken = `; ${document.cookie}` diff --git a/awx/ui_next/src/util/useWsTemplates.test.jsx b/awx/ui_next/src/util/useWsTemplates.test.jsx index 9b32fb3eac..e98d66c49a 100644 --- a/awx/ui_next/src/util/useWsTemplates.test.jsx +++ b/awx/ui_next/src/util/useWsTemplates.test.jsx @@ -43,7 +43,7 @@ describe('useWsTemplates hook', () => { test('should establish websocket connection', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const templates = [{ id: 1 }]; await act(async () => { @@ -65,7 +65,7 @@ describe('useWsTemplates hook', () => { test('should update recent job status', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const templates = [ { @@ -125,7 +125,7 @@ describe('useWsTemplates hook', () => { test('should add new job status', async () => { global.document.cookie = 'csrftoken=abc123'; - const mockServer = new WS('wss://localhost/websocket/'); + const mockServer = new WS('ws://localhost/websocket/'); const templates = [ {