|
13 | 13 |
|
14 | 14 | package io.nats.client; |
15 | 15 |
|
| 16 | +import static org.junit.Assert.assertEquals; |
16 | 17 | import static org.junit.Assert.assertTrue; |
17 | 18 |
|
18 | 19 | import java.io.IOException; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.concurrent.TimeUnit; |
19 | 22 |
|
20 | 23 | import org.junit.Test; |
21 | 24 |
|
| 25 | +import io.nats.client.ConnectionListener.Events; |
| 26 | + |
22 | 27 | public class AuthTests { |
23 | 28 | @Test |
24 | 29 | public void testUserPass() throws Exception { |
@@ -84,6 +89,128 @@ public void testUserPassInURL() throws Exception { |
84 | 89 | } |
85 | 90 | } |
86 | 91 |
|
| 92 | + @Test |
| 93 | + public void testUserPassInURLClusteredWithDifferentUser() throws Exception { |
| 94 | + String[] customArgs1 = {"--user","stephen","--pass","password"}; |
| 95 | + String[] customArgs2 = {"--user","alberto","--pass","casadecampo"}; |
| 96 | + TestHandler handler = new TestHandler(); |
| 97 | + try (NatsTestServer ts1 = new NatsTestServer(customArgs1, false); |
| 98 | + NatsTestServer ts2 = new NatsTestServer(customArgs2, false)) { |
| 99 | + // See config file for user/pass |
| 100 | + Options options = new Options.Builder(). |
| 101 | + server("nats://stephen:password@localhost:"+ts1.getPort()). |
| 102 | + server("nats://alberto:casadecampo@localhost:"+ts2.getPort()). |
| 103 | + maxReconnects(4). |
| 104 | + noRandomize(). |
| 105 | + connectionListener(handler). |
| 106 | + pingInterval(Duration.ofMillis(100)). |
| 107 | + build(); |
| 108 | + Connection nc = Nats.connect(options); |
| 109 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 110 | + assertEquals(nc.getConnectedUrl(), "nats://stephen:password@localhost:"+ts1.getPort()); |
| 111 | + |
| 112 | + handler.prepForStatusChange(Events.RESUBSCRIBED); |
| 113 | + ts1.close(); |
| 114 | + handler.waitForStatusChange(2, TimeUnit.SECONDS); |
| 115 | + |
| 116 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 117 | + assertEquals(nc.getConnectedUrl(), "nats://alberto:casadecampo@localhost:"+ts2.getPort()); |
| 118 | + nc.close(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testUserPassInURLWithFallback() throws Exception { |
| 124 | + String[] customArgs1 = {"--user","stephen","--pass","password"}; |
| 125 | + String[] customArgs2 = {"--user","alberto","--pass","casadecampo"}; |
| 126 | + TestHandler handler = new TestHandler(); |
| 127 | + try (NatsTestServer ts1 = new NatsTestServer(customArgs1, false); |
| 128 | + NatsTestServer ts2 = new NatsTestServer(customArgs2, false)) { |
| 129 | + // See config file for user/pass |
| 130 | + Options options = new Options.Builder(). |
| 131 | + server("nats://stephen:password@localhost:"+ts1.getPort()). |
| 132 | + server("nats://localhost:"+ts2.getPort()). |
| 133 | + userInfo("alberto", "casadecampo"). |
| 134 | + maxReconnects(4). |
| 135 | + noRandomize(). |
| 136 | + connectionListener(handler). |
| 137 | + pingInterval(Duration.ofMillis(100)). |
| 138 | + build(); |
| 139 | + Connection nc = Nats.connect(options); |
| 140 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 141 | + assertEquals(nc.getConnectedUrl(), "nats://stephen:password@localhost:"+ts1.getPort()); |
| 142 | + |
| 143 | + handler.prepForStatusChange(Events.RESUBSCRIBED); |
| 144 | + ts1.close(); |
| 145 | + handler.waitForStatusChange(2, TimeUnit.SECONDS); |
| 146 | + |
| 147 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 148 | + assertEquals(nc.getConnectedUrl(), "nats://localhost:"+ts2.getPort()); |
| 149 | + nc.close(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testTokenInURLClusteredWithDifferentUser() throws Exception { |
| 155 | + String[] customArgs1 = {"--auth","token_one"}; |
| 156 | + String[] customArgs2 = {"--auth","token_two"}; |
| 157 | + TestHandler handler = new TestHandler(); |
| 158 | + try (NatsTestServer ts1 = new NatsTestServer(customArgs1, false); |
| 159 | + NatsTestServer ts2 = new NatsTestServer(customArgs2, false)) { |
| 160 | + // See config file for user/pass |
| 161 | + Options options = new Options.Builder(). |
| 162 | + server("nats://token_one@localhost:"+ts1.getPort()). |
| 163 | + server("nats://token_two@localhost:"+ts2.getPort()). |
| 164 | + maxReconnects(4). |
| 165 | + noRandomize(). |
| 166 | + connectionListener(handler). |
| 167 | + pingInterval(Duration.ofMillis(100)). |
| 168 | + build(); |
| 169 | + Connection nc = Nats.connect(options); |
| 170 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 171 | + assertEquals(nc.getConnectedUrl(), "nats://token_one@localhost:"+ts1.getPort()); |
| 172 | + |
| 173 | + handler.prepForStatusChange(Events.RESUBSCRIBED); |
| 174 | + ts1.close(); |
| 175 | + handler.waitForStatusChange(2, TimeUnit.SECONDS); |
| 176 | + |
| 177 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 178 | + assertEquals(nc.getConnectedUrl(), "nats://token_two@localhost:"+ts2.getPort()); |
| 179 | + nc.close(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testTokenInURLWithFallback() throws Exception { |
| 185 | + String[] customArgs1 = {"--auth","token_one"}; |
| 186 | + String[] customArgs2 = {"--auth","token_two"}; |
| 187 | + TestHandler handler = new TestHandler(); |
| 188 | + try (NatsTestServer ts1 = new NatsTestServer(customArgs1, false); |
| 189 | + NatsTestServer ts2 = new NatsTestServer(customArgs2, false)) { |
| 190 | + // See config file for user/pass |
| 191 | + Options options = new Options.Builder(). |
| 192 | + server("nats://token_one@localhost:"+ts1.getPort()). |
| 193 | + server("nats://localhost:"+ts2.getPort()). |
| 194 | + token("token_two"). |
| 195 | + maxReconnects(4). |
| 196 | + noRandomize(). |
| 197 | + connectionListener(handler). |
| 198 | + pingInterval(Duration.ofMillis(100)). |
| 199 | + build(); |
| 200 | + Connection nc = Nats.connect(options); |
| 201 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 202 | + assertEquals(nc.getConnectedUrl(), "nats://token_one@localhost:"+ts1.getPort()); |
| 203 | + |
| 204 | + handler.prepForStatusChange(Events.RESUBSCRIBED); |
| 205 | + ts1.close(); |
| 206 | + handler.waitForStatusChange(2, TimeUnit.SECONDS); |
| 207 | + |
| 208 | + assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); |
| 209 | + assertEquals(nc.getConnectedUrl(), "nats://localhost:"+ts2.getPort()); |
| 210 | + nc.close(); |
| 211 | + } |
| 212 | + } |
| 213 | + |
87 | 214 | @Test |
88 | 215 | public void testToken() throws Exception { |
89 | 216 | String[] customArgs = {"--auth","derek"}; |
|
0 commit comments