@@ -1553,6 +1553,87 @@ describe('web socket server URL', () => {
1553
1553
} ) ;
1554
1554
} ) ;
1555
1555
1556
+ const resolvedFreePort = server . options . port ;
1557
+
1558
+ const { page, browser } = await runBrowser ( ) ;
1559
+
1560
+ const pageErrors = [ ] ;
1561
+ const consoleMessages = [ ] ;
1562
+
1563
+ page
1564
+ . on ( 'console' , ( message ) => {
1565
+ consoleMessages . push ( message ) ;
1566
+ } )
1567
+ . on ( 'pageerror' , ( error ) => {
1568
+ pageErrors . push ( error ) ;
1569
+ } ) ;
1570
+
1571
+ const webSocketRequests = [ ] ;
1572
+
1573
+ if ( webSocketServer === 'ws' ) {
1574
+ const client = page . _client ;
1575
+
1576
+ client . on ( 'Network.webSocketCreated' , ( request ) => {
1577
+ webSocketRequests . push ( request ) ;
1578
+ } ) ;
1579
+ } else {
1580
+ page . on ( 'request' , ( request ) => {
1581
+ if ( / \/ w s \/ / . test ( request . url ( ) ) ) {
1582
+ webSocketRequests . push ( { url : request . url ( ) } ) ;
1583
+ }
1584
+ } ) ;
1585
+ }
1586
+
1587
+ await page . goto ( `http://127.0.0.1:${ resolvedFreePort } /main` , {
1588
+ waitUntil : 'networkidle0' ,
1589
+ } ) ;
1590
+
1591
+ const webSocketRequest = webSocketRequests [ 0 ] ;
1592
+
1593
+ expect ( webSocketRequest . url ) . toContain (
1594
+ `${ websocketURLProtocol } ://127.0.0.1:${ resolvedFreePort } /ws`
1595
+ ) ;
1596
+ expect ( consoleMessages . map ( ( message ) => message . text ( ) ) ) . toMatchSnapshot (
1597
+ 'console messages'
1598
+ ) ;
1599
+ expect ( pageErrors ) . toMatchSnapshot ( 'page errors' ) ;
1600
+
1601
+ await browser . close ( ) ;
1602
+ await new Promise ( ( resolve , reject ) => {
1603
+ server . close ( ( error ) => {
1604
+ if ( error ) {
1605
+ reject ( error ) ;
1606
+
1607
+ return ;
1608
+ }
1609
+
1610
+ resolve ( ) ;
1611
+ } ) ;
1612
+ } ) ;
1613
+ } ) ;
1614
+
1615
+ it ( `should work when "host" option is IPv4 ("${ webSocketServer } ")` , async ( ) => {
1616
+ const hostname = internalIp . v4 . sync ( ) ;
1617
+ const compiler = webpack ( config ) ;
1618
+ const devServerOptions = {
1619
+ webSocketServer,
1620
+ port : port1 ,
1621
+ host : hostname ,
1622
+ } ;
1623
+ const server = new Server ( devServerOptions , compiler ) ;
1624
+
1625
+ await new Promise ( ( resolve , reject ) => {
1626
+ server . listen ( devServerOptions . port , devServerOptions . host , ( error ) => {
1627
+ if ( error ) {
1628
+ reject ( error ) ;
1629
+
1630
+ return ;
1631
+ }
1632
+
1633
+ resolve ( ) ;
1634
+ } ) ;
1635
+ } ) ;
1636
+
1556
1637
const { page, browser } = await runBrowser ( ) ;
1557
1638
1558
1639
const pageErrors = [ ] ;
@@ -1582,14 +1663,14 @@ describe('web socket server URL', () => {
1582
1663
} ) ;
1583
1664
}
1584
1665
1585
- await page . goto ( `http://127.0.0.1:8080 /main` , {
1666
+ await page . goto ( `http://${ hostname } : ${ port1 } /main` , {
1586
1667
waitUntil : 'networkidle0' ,
1587
1668
} ) ;
1588
1669
1589
1670
const webSocketRequest = webSocketRequests [ 0 ] ;
1590
1671
1591
1672
expect ( webSocketRequest . url ) . toContain (
1592
- `${ websocketURLProtocol } ://127.0.0.1:8080 /ws`
1673
+ `${ websocketURLProtocol } ://${ hostname } : ${ port1 } /ws`
1593
1674
) ;
1594
1675
expect ( consoleMessages . map ( ( message ) => message . text ( ) ) ) . toMatchSnapshot (
1595
1676
'console messages'
@@ -1610,12 +1691,92 @@ describe('web socket server URL', () => {
1610
1691
} ) ;
1611
1692
} ) ;
1612
1693
1613
- it ( `should work when "host" option is IPv4 ("${ webSocketServer } ")` , async ( ) => {
1694
+ it ( `should work when "host" option is "local-ip" ("${ webSocketServer } ")` , async ( ) => {
1695
+ const hostname = internalIp . v4 . sync ( ) ;
1696
+ const compiler = webpack ( config ) ;
1697
+ const devServerOptions = {
1698
+ webSocketServer,
1699
+ port : port1 ,
1700
+ host : 'local-ip' ,
1701
+ } ;
1702
+ const server = new Server ( devServerOptions , compiler ) ;
1703
+
1704
+ await new Promise ( ( resolve , reject ) => {
1705
+ server . listen ( devServerOptions . port , devServerOptions . host , ( error ) => {
1706
+ if ( error ) {
1707
+ reject ( error ) ;
1708
+
1709
+ return ;
1710
+ }
1711
+
1712
+ resolve ( ) ;
1713
+ } ) ;
1714
+ } ) ;
1715
+
1716
+ const { page, browser } = await runBrowser ( ) ;
1717
+
1718
+ const pageErrors = [ ] ;
1719
+ const consoleMessages = [ ] ;
1720
+
1721
+ page
1722
+ . on ( 'console' , ( message ) => {
1723
+ consoleMessages . push ( message ) ;
1724
+ } )
1725
+ . on ( 'pageerror' , ( error ) => {
1726
+ pageErrors . push ( error ) ;
1727
+ } ) ;
1728
+
1729
+ const webSocketRequests = [ ] ;
1730
+
1731
+ if ( webSocketServer === 'ws' ) {
1732
+ const client = page . _client ;
1733
+
1734
+ client . on ( 'Network.webSocketCreated' , ( test ) => {
1735
+ webSocketRequests . push ( test ) ;
1736
+ } ) ;
1737
+ } else {
1738
+ page . on ( 'request' , ( request ) => {
1739
+ if ( / \/ w s \/ / . test ( request . url ( ) ) ) {
1740
+ webSocketRequests . push ( { url : request . url ( ) } ) ;
1741
+ }
1742
+ } ) ;
1743
+ }
1744
+
1745
+ await page . goto ( `http://${ hostname } :${ port1 } /main` , {
1746
+ waitUntil : 'networkidle0' ,
1747
+ } ) ;
1748
+
1749
+ const webSocketRequest = webSocketRequests [ 0 ] ;
1750
+
1751
+ expect ( webSocketRequest . url ) . toContain (
1752
+ `${ websocketURLProtocol } ://${ hostname } :${ port1 } /ws`
1753
+ ) ;
1754
+ expect ( consoleMessages . map ( ( message ) => message . text ( ) ) ) . toMatchSnapshot (
1755
+ 'console messages'
1756
+ ) ;
1757
+ expect ( pageErrors ) . toMatchSnapshot ( 'page errors' ) ;
1758
+
1759
+ await browser . close ( ) ;
1760
+ await new Promise ( ( resolve , reject ) => {
1761
+ server . close ( ( error ) => {
1762
+ if ( error ) {
1763
+ reject ( error ) ;
1764
+
1765
+ return ;
1766
+ }
1767
+
1768
+ resolve ( ) ;
1769
+ } ) ;
1770
+ } ) ;
1771
+ } ) ;
1772
+
1773
+ it ( `should work when "host" option is "local-ipv4" ("${ webSocketServer } ")` , async ( ) => {
1774
+ const hostname = internalIp . v4 . sync ( ) ;
1614
1775
const compiler = webpack ( config ) ;
1615
1776
const devServerOptions = {
1616
1777
webSocketServer,
1617
1778
port : port1 ,
1618
- host : internalIp . v4 . sync ( ) ,
1779
+ host : 'local-ipv4' ,
1619
1780
} ;
1620
1781
const server = new Server ( devServerOptions , compiler ) ;
1621
1782
@@ -1660,14 +1821,14 @@ describe('web socket server URL', () => {
1660
1821
} ) ;
1661
1822
}
1662
1823
1663
- await page . goto ( `http://${ internalIp . v4 . sync ( ) } :${ port1 } /main` , {
1824
+ await page . goto ( `http://${ hostname } :${ port1 } /main` , {
1664
1825
waitUntil : 'networkidle0' ,
1665
1826
} ) ;
1666
1827
1667
1828
const webSocketRequest = webSocketRequests [ 0 ] ;
1668
1829
1669
1830
expect ( webSocketRequest . url ) . toContain (
1670
- `${ websocketURLProtocol } ://${ internalIp . v4 . sync ( ) } :${ port1 } /ws`
1831
+ `${ websocketURLProtocol } ://${ hostname } :${ port1 } /ws`
1671
1832
) ;
1672
1833
expect ( consoleMessages . map ( ( message ) => message . text ( ) ) ) . toMatchSnapshot (
1673
1834
'console messages'
0 commit comments