2
2
// Licensed under the MIT License.
3
3
4
4
import * as assert from 'assert' ;
5
- import * as http from 'http ' ;
5
+ import * as net from 'net ' ;
6
6
import * as sinon from 'sinon' ;
7
7
import * as crypto from 'crypto' ;
8
8
import { OutputChannel , Uri } from 'vscode' ;
9
9
import { IPythonExecutionFactory , IPythonExecutionService } from '../../../client/common/process/types' ;
10
- import { createDeferred } from '../../../client/common/utils/async' ;
11
10
import { PythonTestServer } from '../../../client/testing/testController/common/server' ;
12
- import * as logging from '../../../client/logging' ;
13
11
import { ITestDebugLauncher } from '../../../client/testing/common/types' ;
14
- import { IJSONRPCMessage , jsonRPCProcessor } from '../../../client/testing/testController/ common/utils' ;
12
+ import { createDeferred } from '../../../client/common/utils/async ' ;
15
13
16
14
suite ( 'Python Test Server' , ( ) => {
17
15
const fakeUuid = 'fake-uuid' ;
@@ -22,13 +20,11 @@ suite('Python Test Server', () => {
22
20
let sandbox : sinon . SinonSandbox ;
23
21
let execArgs : string [ ] ;
24
22
let v4Stub : sinon . SinonStub ;
25
- let traceLogStub : sinon . SinonStub ;
26
23
let debugLauncher : ITestDebugLauncher ;
27
24
28
25
setup ( ( ) => {
29
26
sandbox = sinon . createSandbox ( ) ;
30
27
v4Stub = sandbox . stub ( crypto , 'randomUUID' ) ;
31
- traceLogStub = sandbox . stub ( logging , 'traceLog' ) ;
32
28
33
29
v4Stub . returns ( fakeUuid ) ;
34
30
stubExecutionService = ( {
@@ -121,157 +117,42 @@ suite('Python Test Server', () => {
121
117
} ) ;
122
118
123
119
test ( 'If the server receives malformed data, it should display a log message, and not fire an event' , async ( ) => {
120
+ let eventData : string | undefined ;
121
+ const client = new net . Socket ( ) ;
124
122
const deferred = createDeferred ( ) ;
123
+
125
124
const options = {
126
125
command : { script : 'myscript' , args : [ '-foo' , 'foo' ] } ,
127
126
workspaceFolder : Uri . file ( '/foo/bar' ) ,
128
127
cwd : '/foo/bar' ,
129
128
uuid : fakeUuid ,
130
129
} ;
131
130
132
- let response ;
131
+ stubExecutionService = ( {
132
+ exec : async ( ) => {
133
+ client . connect ( server . getPort ( ) ) ;
134
+ return Promise . resolve ( { stdout : '' , stderr : '' } ) ;
135
+ } ,
136
+ } as unknown ) as IPythonExecutionService ;
133
137
134
138
server = new PythonTestServer ( stubExecutionFactory , debugLauncher ) ;
135
139
await server . serverReady ( ) ;
136
-
137
140
server . onDataReceived ( ( { data } ) => {
138
- response = data ;
141
+ eventData = data ;
139
142
deferred . resolve ( ) ;
140
143
} ) ;
141
144
142
- await server . sendCommand ( options ) ;
143
-
144
- // Send data back.
145
- const port = server . getPort ( ) ;
146
- const requestOptions = {
147
- hostname : 'localhost' ,
148
- method : 'POST' ,
149
- port,
150
- headers : { 'Request-uuid' : fakeUuid } ,
151
- } ;
152
-
153
- const request = http . request ( requestOptions , ( res ) => {
154
- res . setEncoding ( 'utf8' ) ;
145
+ client . on ( 'connect' , ( ) => {
146
+ console . log ( 'Socket connected, local port:' , client . localPort ) ;
147
+ client . write ( 'malformed data' ) ;
148
+ client . end ( ) ;
155
149
} ) ;
156
- const postData = '[test' ;
157
- request . write ( postData ) ;
158
- request . end ( ) ;
159
-
160
- await deferred . promise ;
161
-
162
- sinon . assert . calledOnce ( traceLogStub ) ;
163
- assert . deepStrictEqual ( response , '' ) ;
164
- } ) ;
165
- test ( 'If the server receives data, it should not fire an event if it is an unknown uuid' , async ( ) => {
166
- const deferred = createDeferred ( ) ;
167
- const options = {
168
- command : { script : 'myscript' , args : [ '-foo' , 'foo' ] } ,
169
- workspaceFolder : Uri . file ( '/foo/bar' ) ,
170
- cwd : '/foo/bar' ,
171
- } ;
172
-
173
- let response ;
174
-
175
- server = new PythonTestServer ( stubExecutionFactory , debugLauncher ) ;
176
- await server . serverReady ( ) ;
177
-
178
- server . onDataReceived ( ( { data } ) => {
179
- response = data ;
180
- deferred . resolve ( ) ;
150
+ client . on ( 'error' , ( error ) => {
151
+ console . log ( 'Socket connection error:' , error ) ;
181
152
} ) ;
182
153
183
154
await server . sendCommand ( options ) ;
184
-
185
- // Send data back.
186
- const port = server . getPort ( ) ;
187
- const requestOptions = {
188
- hostname : 'localhost' ,
189
- method : 'POST' ,
190
- port,
191
- headers : { 'Request-uuid' : fakeUuid } ,
192
- } ;
193
- // request.hasHeader()
194
- const request = http . request ( requestOptions , ( res ) => {
195
- res . setEncoding ( 'utf8' ) ;
196
- } ) ;
197
- const postData = JSON . stringify ( { status : 'success' , uuid : fakeUuid , payload : 'foo' } ) ;
198
- request . write ( postData ) ;
199
- request . end ( ) ;
200
-
201
155
await deferred . promise ;
202
-
203
- assert . deepStrictEqual ( response , postData ) ;
204
- } ) ;
205
-
206
- test ( 'If the server receives data, it should not fire an event if there is no uuid' , async ( ) => {
207
- const deferred = createDeferred ( ) ;
208
- const options = {
209
- command : { script : 'myscript' , args : [ '-foo' , 'foo' ] } ,
210
- workspaceFolder : Uri . file ( '/foo/bar' ) ,
211
- cwd : '/foo/bar' ,
212
- } ;
213
-
214
- let response ;
215
-
216
- server = new PythonTestServer ( stubExecutionFactory , debugLauncher ) ;
217
- await server . serverReady ( ) ;
218
-
219
- server . onDataReceived ( ( { data } ) => {
220
- response = data ;
221
- deferred . resolve ( ) ;
222
- } ) ;
223
-
224
- await server . sendCommand ( options ) ;
225
-
226
- // Send data back.
227
- const port = server . getPort ( ) ;
228
- const requestOptions = {
229
- hostname : 'localhost' ,
230
- method : 'POST' ,
231
- port,
232
- headers : { 'Request-uuid' : 'some-other-uuid' } ,
233
- } ;
234
- const requestOptions2 = {
235
- hostname : 'localhost' ,
236
- method : 'POST' ,
237
- port,
238
- headers : { 'Request-uuid' : fakeUuid } ,
239
- } ;
240
- const requestOne = http . request ( requestOptions , ( res ) => {
241
- res . setEncoding ( 'utf8' ) ;
242
- } ) ;
243
- const postDataOne = JSON . stringify ( { status : 'success' , uuid : 'some-other-uuid' , payload : 'foo' } ) ;
244
- requestOne . write ( postDataOne ) ;
245
- requestOne . end ( ) ;
246
-
247
- const requestTwo = http . request ( requestOptions2 , ( res ) => {
248
- res . setEncoding ( 'utf8' ) ;
249
- } ) ;
250
- const postDataTwo = JSON . stringify ( { status : 'success' , uuid : fakeUuid , payload : 'foo' } ) ;
251
- requestTwo . write ( postDataTwo ) ;
252
- requestTwo . end ( ) ;
253
-
254
- await deferred . promise ;
255
-
256
- assert . deepStrictEqual ( response , postDataTwo ) ;
257
- } ) ;
258
- test ( 'jsonRPCProcessor' , async ( ) => {
259
- const rawDataString = `Content-Length: 160
260
- Content-Type: application/json
261
- Request-uuid: xxxxxx-1012-xxxx-xxxx-xxx
262
-
263
- {"cwd": "/path/to/folder", "status": "success", "tests": {"name": "test", "path": "/path/to/test", "type_": "folder", "children": []}], "id_": "/path/to/test"}}` ;
264
- const headers = new Map < string , string > ( [
265
- [ 'Content-Length' , '160' ] ,
266
- [ 'Content-Type' , 'application/json' ] ,
267
- [ 'Request-uuid' , 'xxxxxx-1012-xxxx-xxxx-xxx' ] ,
268
- ] ) ;
269
- const expected : IJSONRPCMessage = {
270
- headers,
271
- extractedData : `{"cwd": "/path/to/folder", "status": "success", "tests": {"name": "test", "path": "/path/to/test", "type_": "folder", "children": []}], "id_": "/path/to/test"}}` ,
272
- remainingRawData : '' ,
273
- } ;
274
- const output : IJSONRPCMessage = jsonRPCProcessor ( rawDataString ) ;
275
- assert . deepStrictEqual ( output , expected ) ;
156
+ assert . deepStrictEqual ( eventData , '' ) ;
276
157
} ) ;
277
158
} ) ;
0 commit comments