Skip to content

Commit 0b1fb03

Browse files
author
cty
committed
using endpoint directly instead of pick it from discovery again
add tests
1 parent 8cd9cdf commit 0b1fb03

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/server/balancer/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ function _sendSockJsInfo(req, res, cookies) {
103103
}
104104

105105
var endpointHash = Cluster.discovery.endpointToHash(endpoint);
106-
var balancer = Cluster.discovery.pickBalancer(endpointHash) || Cluster.discovery.pickEndpoint(endpointHash) || "";
107-
if (!/(\w+)\/$/.test(balancer)) balancer += '/';
106+
// when no balancer url is provided, using endpoint's absolute path instead of relative path
107+
var balancer = Cluster.discovery.pickBalancer(endpointHash) || endpoint || "";
108+
if (!/(\w+)\/$/.test(balancer)) balancer += '/'; // add tail backslash if path not end with it
108109
var base_url =
109110
urlResolve(balancer, "cluster-ddp/" + endpointHash + "/" + serviceName);
110111

tests/server/balancer/utils.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ function(test) {
483483
var discovery = {
484484
endpointToHash: sinon.stub().returns(hash),
485485
pickBalancer: sinon.stub().returns(balancer),
486-
pickEndpoint: sinon.stub().returns(endpoint),
487486
};
488487

489488
var cookies = {
@@ -504,7 +503,47 @@ function(test) {
504503

505504
test.isTrue(discovery.endpointToHash.calledWith(endpoint));
506505
var info = JSON.parse(res.end.firstCall.args[0]);
507-
test.equal(info.base_url, format("epoint/cluster-ddp/%s/web", hash));
506+
test.equal(info.base_url, format(endpoint + "/cluster-ddp/%s/web", hash));
507+
test.equal(info.websocket, true);
508+
509+
balancerMock.verify();
510+
balancerMock.restore();
511+
});
512+
});
513+
});
514+
515+
Tinytest.add(
516+
'Balancer - _sendSockJsInfo - correct base_url, web, with no balancer, endpoint has sub path',
517+
function(test) {
518+
var balancer = null;
519+
// endpoint has a sub path
520+
var endpoint = "http://localhost:8001/private";
521+
var hash = "hashhh";
522+
var uiService = "web";
523+
var discovery = {
524+
endpointToHash: sinon.stub().returns(hash),
525+
pickBalancer: sinon.stub().returns(balancer),
526+
};
527+
528+
var cookies = {
529+
get: sinon.stub().returns(hash)
530+
};
531+
532+
var balancerMock = sinon.mock(Balancer);
533+
balancerMock.expects('_pickJustEndpoint')
534+
.withArgs(hash, "web")
535+
.returns(endpoint);
536+
537+
var req = {url: "/web/sockjs/info"};
538+
var res = {writeHead: sinon.stub(), end: sinon.stub()};
539+
540+
WithCluster({_uiService: uiService}, function() {
541+
WithDiscovery(discovery, function() {
542+
Balancer._sendSockJsInfo(req, res, cookies);
543+
544+
test.isTrue(discovery.endpointToHash.calledWith(endpoint));
545+
var info = JSON.parse(res.end.firstCall.args[0]);
546+
test.equal(info.base_url, format(endpoint + "/cluster-ddp/%s/web", hash));
508547
test.equal(info.websocket, true);
509548

510549
balancerMock.verify();

0 commit comments

Comments
 (0)