summaryrefslogtreecommitdiff
path: root/src/test/java/org/javacs/lsp/LspTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/javacs/lsp/LspTest.java')
-rw-r--r--src/test/java/org/javacs/lsp/LspTest.java65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/test/java/org/javacs/lsp/LspTest.java b/src/test/java/org/javacs/lsp/LspTest.java
new file mode 100644
index 0000000..3f65774
--- /dev/null
+++ b/src/test/java/org/javacs/lsp/LspTest.java
@@ -0,0 +1,65 @@
+package org.javacs.lsp;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import com.google.common.base.Charsets;
+import com.google.gson.JsonObject;
+import java.io.IOException;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LspTest {
+ PipedInputStream buffer = new PipedInputStream(10 * 1024 * 1024); // 10 MB buffer
+ PipedOutputStream writer = new PipedOutputStream();
+
+ @Before
+ public void connectBuffer() throws IOException {
+ writer.connect(buffer);
+ }
+
+ String bufferToString() {
+ try {
+ var available = buffer.available();
+ var bytes = new byte[available];
+ var read = buffer.read(bytes);
+ assert read == available;
+ return new String(bytes, Charsets.UTF_8);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ public void writeResponse() {
+ LSP.respond(writer, 1, 2);
+ var expected = "Content-Length: 35\r\n\r\n{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":2}";
+ assertThat(bufferToString(), equalTo(expected));
+ }
+
+ @Test
+ public void writeMultibyteCharacters() {
+ LSP.respond(writer, 1, "🔥");
+ var expected = "Content-Length: 40\r\n\r\n{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"🔥\"}";
+ assertThat(bufferToString(), equalTo(expected));
+ }
+
+ @Test
+ public void readMessage() throws IOException {
+ var message = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}";
+ var header = String.format("Content-Length: %d\r\n\r\n", message.getBytes().length);
+ writer.write(header.getBytes());
+ writer.write(message.getBytes());
+
+ var token = LSP.nextToken(buffer);
+ assertThat(token, equalTo(message));
+
+ var parse = LSP.parseMessage(token);
+ assertThat(parse.jsonrpc, equalTo("2.0"));
+ assertThat(parse.id, equalTo(1));
+ assertThat(parse.method, equalTo("initialize"));
+ assertThat(parse.params, equalTo(new JsonObject()));
+ }
+}