This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added websocket support to testeefi-rest
- Loading branch information
Alex Stockinger
committed
Sep 13, 2017
1 parent
069ee8d
commit 4cce6a6
Showing
9 changed files
with
270 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
rest/src/main/java/fi/testee/rest/DelegatingConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2017 Alex Stockinger | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fi.testee.rest; | ||
|
||
import javax.websocket.Extension; | ||
import javax.websocket.HandshakeResponse; | ||
import javax.websocket.server.HandshakeRequest; | ||
import javax.websocket.server.ServerEndpointConfig; | ||
import java.util.List; | ||
|
||
public class DelegatingConfigurator extends ServerEndpointConfig.Configurator { | ||
private ServerEndpointConfig.Configurator delegate; | ||
|
||
public DelegatingConfigurator(final ServerEndpointConfig.Configurator delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public String getNegotiatedSubprotocol(List<String> supported, List<String> requested) { | ||
return delegate.getNegotiatedSubprotocol(supported, requested); | ||
} | ||
|
||
@Override | ||
public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) { | ||
return delegate.getNegotiatedExtensions(installed, requested); | ||
} | ||
|
||
@Override | ||
public boolean checkOrigin(String originHeaderValue) { | ||
return delegate.checkOrigin(originHeaderValue); | ||
} | ||
|
||
@Override | ||
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { | ||
delegate.modifyHandshake(sec, request, response); | ||
} | ||
|
||
@Override | ||
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { | ||
return delegate.getEndpointInstance(endpointClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2017 Alex Stockinger | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fi.testee.rest; | ||
|
||
import fi.testee.junit4.TestEEfi; | ||
import fi.testee.rest.app.ManagedBean; | ||
import org.eclipse.jetty.util.component.LifeCycle; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
|
||
import javax.annotation.Resource; | ||
import javax.websocket.ClientEndpoint; | ||
import javax.websocket.ContainerProvider; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.Session; | ||
import javax.websocket.WebSocketContainer; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static junit.framework.TestCase.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(TestEEfi.class) | ||
public class WebsocketTest { | ||
@Resource | ||
private RestServer restServer; | ||
@Mock | ||
private ManagedBean managedBean; | ||
|
||
@Test | ||
public void roundtrip_works() throws Exception { | ||
when(managedBean.getValue()).thenReturn("mocked"); | ||
|
||
final URI uri = URI.create("ws://localhost:" + restServer.getPort() + "/websockets/echo"); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
final WebSocketContainer container = ContainerProvider.getWebSocketContainer(); | ||
final Client client = new Client(() -> latch.countDown()); | ||
try (final Session session = container.connectToServer(client, uri)) { | ||
session.getBasicRemote().sendText("Hello"); | ||
assertTrue(latch.await(5, SECONDS)); | ||
} finally { | ||
((LifeCycle) container).stop(); | ||
} | ||
assertEquals("mocked:Hello", client.message); | ||
} | ||
|
||
@ClientEndpoint | ||
public static class Client { | ||
private final Runnable callback; | ||
public String message; | ||
|
||
public Client(final Runnable callback) { | ||
this.callback = callback; | ||
} | ||
|
||
@OnMessage | ||
public void onWebSocketText(final String message) throws IOException { | ||
this.message = message; | ||
callback.run(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.