8
8
import sys
9
9
from unittest import mock
10
10
import pytest
11
+ import json
11
12
12
13
from pylsp_jsonrpc .streams import JsonRpcStreamReader , JsonRpcStreamWriter
13
14
@@ -31,7 +32,6 @@ def reader(rfile):
31
32
def writer (wfile ):
32
33
return JsonRpcStreamWriter (wfile , sort_keys = True )
33
34
34
-
35
35
def test_reader (rfile , reader ):
36
36
rfile .write (
37
37
b'Content-Length: 49\r \n '
@@ -84,13 +84,41 @@ def test_writer(wfile, writer):
84
84
}
85
85
writer .write (data )
86
86
87
- raw_result = wfile .getvalue ().decode ()
88
- raw_result_lines = raw_result .split ()
87
+ assert wfile .getvalue () == (
88
+ b'Content-Length: 44\r \n '
89
+ b'Content-Type: application/vscode-jsonrpc; charset=utf8\r \n '
90
+ b'\r \n '
91
+ b'{"id":"hello","method":"method","params":{}}'
92
+ )
89
93
90
- assert raw_result_lines [0 ].split (":" ) == "Content-Length"
91
- assert raw_result_lines [1 ] == 'Content-Type: application/vscode-jsonrpc; charset=utf8'
92
- assert raw_result_lines [2 ] == ''
93
- assert json .loads (raw_result_lines [3 ]) == data
94
+
95
+ def test_writer_builtin_json (wfile ):
96
+ """Test the stream writer using the standard json lib."""
97
+ data = {
98
+ 'id' : 'hello' ,
99
+ 'method' : 'method' ,
100
+ 'params' : {}
101
+ }
102
+ orig_modules = sys .modules
103
+ try :
104
+ # Pretend orjson wasn't imported when initializing the writer.
105
+ sys .modules = {'json' : json }
106
+ std_json_writer = JsonRpcStreamWriter (wfile , sort_keys = True )
107
+ finally :
108
+ sys .modules = orig_modules
109
+
110
+ with mock .patch ('pylsp_jsonrpc.streams.json' ) as streams_json :
111
+ # Mock the imported json's dumps function to use the stdlib's dumps,
112
+ # whether orjson is available or not.
113
+ streams_json .dumps = json .dumps
114
+ std_json_writer .write (data )
115
+
116
+ assert wfile .getvalue () == (
117
+ b'Content-Length: 44\r \n '
118
+ b'Content-Type: application/vscode-jsonrpc; charset=utf8\r \n '
119
+ b'\r \n '
120
+ b'{"id":"hello","method":"method","params":{}}'
121
+ )
94
122
95
123
96
124
class JsonDatetime (datetime .datetime ):
0 commit comments