Skip to content

Commit 974d9a6

Browse files
authored
Merge pull request #47 from oslabs-beta/testing
Testing for gRPC updated with some new tests. Added multiple protofile support for saving current protofile with each collection or history set
2 parents 9490f26 + 67910b2 commit 974d9a6

File tree

7 files changed

+161
-16
lines changed

7 files changed

+161
-16
lines changed

__tests__/businessReducer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,28 @@ describe('Business reducer', () => {
346346
graphQL: true
347347
}
348348
}
349+
const requestStreamsAction = {
350+
type: 'SET_NEW_REQUEST_STREAMS',
351+
payload: {
352+
353+
streamsArr: [],
354+
count: 0,
355+
streamContent: [],
356+
selectedPackage: 'helloworld',
357+
selectedRequest: 'helloRequest',
358+
selectedService: 'hello',
359+
selectedStreamingType: null,
360+
initialQuery: null,
361+
queryArr: null,
362+
protoPath: null,
363+
services: null,
364+
365+
},
366+
}
367+
it('sets the newRequestStreams on SET_NEW_REQUEST_STREAMS', () => {
368+
const { newRequestStreams } = reducer(state, requestStreamsAction);
369+
expect(newRequestStreams).toEqual(requestStreamsAction.payload);
370+
})
349371
it('sets the newRequestFields on POST', () => {
350372
const { newRequestFields } = reducer(state, postAction);
351373
expect(newRequestFields).toEqual(postAction.payload);

__tests__/protoParserTests.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import protoParser from '../src/client/components/composer/protos/protoParser';
2+
3+
4+
describe('testing protoParser', ()=> {
5+
6+
const protoFile = `syntax = 'proto3';
7+
8+
package helloworld;
9+
10+
// The greeting service definition.
11+
service Greeter {
12+
// Sends a greeting
13+
rpc SayHello (HelloRequest) returns (HelloReply) {}
14+
rpc SayHelloCS (stream HelloRequest) returns (HelloReply) {}
15+
rpc SayHellos (HelloRequest) returns (stream HelloReply) {}
16+
rpc SayHelloBidi (stream HelloRequest) returns (stream HelloReply) {}
17+
}
18+
19+
// The request message containing the user's name.
20+
message HelloRequest {
21+
string name = 1;
22+
}
23+
24+
// The response message containing the greetings
25+
message HelloReply {
26+
string message = 1;
27+
}
28+
29+
// The request message containing the user's name.
30+
message HelloHowOldRequest {
31+
int32 age = 1;
32+
}
33+
message HelloAge {
34+
int32 age = 1;
35+
}`
36+
describe('parser parses protos correctly', ()=> {
37+
it('should get packageName', () => {
38+
const parsedProto = protoParser(protoFile)
39+
.then(data => {
40+
// console.log(d)
41+
expect(data.packageName).toEqual('helloworld')
42+
})
43+
44+
})
45+
it('should get serviceArray', () => {
46+
const testArr = [{
47+
messages: [{}, {}, {}, {}],
48+
name: 'Greeter',
49+
packageName: 'helloworld',
50+
rpcs: [{}, {}, {}, {}],
51+
52+
}]
53+
const parsedProto = protoParser(protoFile)
54+
.then(data => {
55+
// console.log(d)
56+
expect(data.serviceArr[0].messages).toHaveLength(4);
57+
expect(data.serviceArr[0].rpcs).toHaveLength(4);
58+
expect(data.serviceArr[0].name).toEqual('Greeter');
59+
expect(data.serviceArr[0].packageName).toEqual('helloworld');
60+
})
61+
62+
})
63+
64+
it('should fill message content', () => {
65+
const testArr = [{
66+
messages: [
67+
{name: "HelloRequest", def: {name: {type: "TYPE_STRING", nested: false, dependent: ''}}},
68+
{name: "HelloRequest", def: {name: {type: "TYPE_STRING", nested: false, dependent: ''}}},
69+
{name: "HelloRequest", def: {name: {type: "TYPE_STRING", nested: false, dependent: ''}}},
70+
{name: "HelloRequest", def: {name: {type: "TYPE_STRING", nested: false, dependent: ''}}}
71+
],
72+
name: 'Greeter',
73+
packageName: 'helloworld',
74+
rpcs: [{}, {}, {}, {}],
75+
76+
}]
77+
const parsedProto = protoParser(protoFile)
78+
.then(data => {
79+
expect(data.serviceArr[0].messages[0]).toEqual(testArr[0].messages[0]);
80+
expect(data.serviceArr[0].messages[0].def.name.type).toEqual("TYPE_STRING");
81+
})
82+
83+
})
84+
85+
86+
})
87+
})
88+

__tests__/responseTests.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('ResponseContainer', () => {
2626
wrapper.setState({ openTab: 'Response Headers' });
2727
expect(wrapper.find('ResponseHeadersDisplay')).toBeTruthy();
2828
});
29+
2930

3031
it('should render Response Cookies when selected', () => {
3132
const wrapper = shallow(<ResponseContainer {...props} />);
@@ -47,15 +48,35 @@ describe('ResponseContainer', () => {
4748
const wrapper = shallow(<ResponseContainer {...props} />);
4849
expect(wrapper.find('ResponseEventsDisplay')).toBeTruthy();
4950
});
51+
52+
props = {
53+
content: {
54+
gRPC: true,
55+
request: {
56+
method: null
57+
},
58+
}
59+
}
60+
it('gRPC req should render Response Metadata when selected', () => {
61+
const wrapper = shallow(<ResponseContainer {...props} />);
62+
wrapper.setState({ openTab: 'Response Metadata' });
63+
expect(wrapper.find('ResponseHeadersDisplay')).toBeTruthy();
64+
});
65+
5066
});
5167

5268
describe('ResponseTabs', () => {
5369
it('should render three tabs', () => {
54-
const wrapper = shallow(<ResponseTabs />);
70+
const wrapper = shallow(<ResponseTabs content={'not grpc'}/>);
5571
expect(wrapper.find('Tab')).toHaveLength(3);
5672
});
73+
it('grpc should render two tabs', () => {
74+
const wrapper = shallow(<ResponseTabs content={{gRPC: true}}/>);
75+
expect(wrapper.find('Tab')).toHaveLength(2);
76+
});
5777
});
5878

79+
5980
describe('ResponseEventsDisplay', () => {
6081
it('if SSE, should render event rows', () => {
6182
const props = {
@@ -67,16 +88,7 @@ describe('ResponseEventsDisplay', () => {
6788
const wrapper = shallow(<ResponseEventsDisplay {...props} />);
6889
expect(wrapper.find('SSERow')).toHaveLength(2);
6990
});
70-
// it('if gRPC, should render all events', () => {
71-
// const props = {
72-
// response: {
73-
// headers: { 'content-type': 'text/event-stream' },
74-
// events: ['event 1', 'event 2'],
75-
// }
76-
// };
77-
// const wrapper = shallow(<ResponseEventsDisplay {...props} />);
78-
// expect(wrapper.find('JSONPretty').props.data).toHaveLength(2);
79-
// });
91+
8092
it('if not SSE, should render single event', () => {
8193
const props = {
8294
response: {

src/client/components/composer/protos/protoParser.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const fs = require('fs');
22
const grpc = require('grpc');
33
const protoLoader = require('@grpc/proto-loader');
4-
const path = require('path')
4+
const path = require('path');
5+
const uuid = require('uuid');
56

67
//temp for testing >>>>
78
// let tempData = fs.readFileSync(path.join(process.cwd(), 'grpc_mockData/protos/route_guide.proto'), 'utf-8')
@@ -16,16 +17,26 @@ async function protoParserFunc(protoBodyData) {
1617
let protoStorage = {};
1718
//store the original .proto content in the storage before parsing
1819
protoStorage.protoMaster = protoBodyData;
19-
20+
//make unique protoID for file we are saving
21+
let protoID = Math.floor(Math.random() * 1000);
22+
//if file path for that ID already exists, increment the ID
23+
try {
24+
while (fs.existsSync('src/client/components/composer/protos/' + protoID + '.proto')) {
25+
//file exists
26+
protoID += 1;
27+
}
28+
} catch(err) {
29+
console.error(err)
30+
}
2031
// write to saveProto file for interaction with the server
2132
// const dirName = remote.app.getAppPath(); // uncomment when done testing above
22-
fs.writeFileSync(path.join(process.cwd(), 'src/client/components/composer/protos/saveProto.proto'), protoBodyData, 'utf-8')
33+
fs.writeFileSync(path.join(process.cwd(), 'src/client/components/composer/protos/' + protoID + '.proto'), protoBodyData, 'utf-8')
2334
console.log('Proto file has been saved')
2435
// });
2536

2637
// define the modular client for testing
2738
// declare path variable of imported proto file
28-
const PROTO_PATH = path.join(process.cwd(), 'src/client/components/composer/protos/saveProto.proto');
39+
const PROTO_PATH = path.join(process.cwd(), 'src/client/components/composer/protos/' + protoID + '.proto');
2940

3041
// create gRPC package options
3142
const protoOptionsObj = {

src/client/components/composer/protos/saveProto.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
// Copyright 2015 gRPC authors.
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -109,3 +110,4 @@ message RouteSummary {
109110
// The duration of the traversal in seconds.
110111
int32 elapsed_time = 4;
111112
}
113+

src/client/components/display/ResponseEventsDisplay.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import createDOMPurify from 'dompurify';
66
const ResponseEventsDisplay = ({ response }) => {
77
const { events, headers } = response;
88
const displayContents = [];
9+
console.log('what is events' , events)
910

1011
// If it's an SSE, render event rows
1112
if (headers && headers['content-type'] && headers['content-type'].includes('text/event-stream')) {
@@ -19,7 +20,7 @@ const ResponseEventsDisplay = ({ response }) => {
1920
<div className="okay" dangerouslySetInnerHTML={{__html: createDOMPurify.sanitize(events[0])}} />
2021
)
2122
}
22-
else if (events.length > 1) {
23+
else if (events && events.length > 1) {
2324
if (events) {
2425
displayContents.push(
2526
<div className="json-response" key="jsonresponsediv">
@@ -36,6 +37,7 @@ const ResponseEventsDisplay = ({ response }) => {
3637
}
3738

3839
}
40+
3941
// Otherwise, render a single display
4042
else {
4143
if (events) {

src/client/controllers/grpcController.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ let grpcController = {};
1919

2020
grpcController.openGrpcConnection = (reqResObj, connectionArray) => {
2121
//check for connection, if not open one
22+
console.log(reqResObj)
2223
if (false) {
2324
//use existing connection
2425
}
@@ -31,6 +32,12 @@ grpcController.openGrpcConnection = (reqResObj, connectionArray) => {
3132
let packageName = reqResObj.packageName;
3233
let url = reqResObj.url;
3334
let queryArr = reqResObj.queryArr;
35+
if (reqResObj.response.events === null) {
36+
reqResObj.response.events = [];
37+
}
38+
if (reqResObj.response.headers === null) {
39+
reqResObj.response.headers = {};
40+
}
3441

3542
// go through services object, find service where name matches our passed
3643
// in service, then grab the rpc list of that service, also save that service
@@ -186,6 +193,7 @@ grpcController.openGrpcConnection = (reqResObj, connectionArray) => {
186193
reqResObj.connection = 'open';
187194
reqResObj.connectionType = 'plain';
188195
reqResObj.timeSent = Date.now();
196+
console.log('rpc', rpc, 'reqresquery', reqResObj.queryArr[0])
189197
const call = client[rpc](reqResObj.queryArr[0], meta);
190198
call.on("data", resp => {
191199
// console.log('server streaming message:', data);

0 commit comments

Comments
 (0)