| // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| import 'dart:convert'; |
| |
| import 'package:analysis_server/src/lsp/lsp_packet_transformer.dart'; |
| import 'package:test/test.dart'; |
| |
| main() { |
| group('lsp_packet_transformer', () { |
| test('transforms data received as individual bytes', () async { |
| final payload = '{ json payload }'; |
| final lspPacket = makeLspPacket(payload); |
| final output = await new Stream.fromIterable([lspPacket]) |
| .transform(new LspPacketTransformer()) |
| .toList(); |
| expect(output, equals([payload])); |
| }); |
| |
| test('transforms data received in chunks', () async { |
| final payload = '{ json\n payload\n }'; |
| final lspPacket = makeLspPacket(payload); |
| // Separate each byte into it's own "packet" to simulate chunked data |
| // where all the bytes for a single LSP packet don't arrive in one |
| // item to the stream. |
| final dataPackets = lspPacket.map((b) => [b]); |
| final output = await new Stream.fromIterable(dataPackets) |
| .transform(new LspPacketTransformer()) |
| .toList(); |
| expect(output, equals([payload])); |
| }); |
| |
| test('handles unicode characters', () async { |
| // This file is saved as UTF8. |
| final payload = '{ json payload 🎉 }'; |
| final lspPacket = makeLspPacket(payload); |
| final output = await new Stream.fromIterable([lspPacket]) |
| .transform(new LspPacketTransformer()) |
| .toList(); |
| expect(output, equals([payload])); |
| }); |
| }); |
| } |
| |
| List<int> makeLspPacket(String json, [String contentType]) { |
| final utf8EncodedBody = utf8.encode(json); |
| final header = 'Content-Length: ${utf8EncodedBody.length}\r\n' |
| 'Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n'; |
| final asciiEncodedHeader = ascii.encode(header); |
| |
| return asciiEncodedHeader.followedBy(utf8EncodedBody).toList(); |
| } |