Skip to content

Commit bf655d2

Browse files
Rodrigoplp-workbhufmann
authored andcommitted
Documenting functions of rest-client's mock
Add documenting comments to the functions in the mock file used to test rest-client.ts. Signed-off-by: Rodrigo Pinto <rodrigo.pinto@calian.ca>.
1 parent 58b8f5e commit bf655d2

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# LC_COLLATE=C sort
22
/*.log
33
/.DS_Store
4+
/coverage/
45
/lib/
56
/node_modules/

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ To keep tests running do:
2525
```shell
2626
yarn test --verbose --watch
2727
```
28+
29+
### Test coverage
30+
31+
The following command prints a coverage report to the terminal. As of now it covers all typescript files of the project, including those that are not supposed to have tests.
32+
33+
```shell
34+
yarn test --coverage --collectCoverageFrom='src/**/*.ts'
35+
```

src/protocol/__mocks__/rest-client.ts

+46
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const output: OutputDescriptor = {
2424
};
2525

2626
export class RestClient {
27+
/**
28+
* Perform request
29+
* Mocks the request call without calling the real backend.
30+
* @param verb RestAPI verb
31+
* @param url URL to query
32+
* @param body Query object as defined by the Query interface
33+
* @return A TspClientResponse object
34+
*/
2735
private static async performRequest<T>(verb: string, url: string, body?: any): Promise<TspClientResponse<T>> {
2836
return new Promise((resolve, reject) => {
2937
let client: TspClientResponse<T> = new TspClientResponse(response.text, response.status, response.statusText);
@@ -62,6 +70,13 @@ export class RestClient {
6270
});
6371
}
6472

73+
/**
74+
* Perform GET
75+
* T is the expected type of the json object returned by this request
76+
* @param url URL to query without query parameters
77+
* @param parameters Query parameters. Mapped keys and values are used to build the final URL
78+
* @return A TspClientResponse object
79+
*/
6580
public static async get<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<T>> {
6681
let getUrl = url;
6782
if (parameters) {
@@ -71,14 +86,35 @@ export class RestClient {
7186
return this.performRequest<T>('get', getUrl);
7287
}
7388

89+
/**
90+
* Perform POST
91+
* T is the expected type of the json object returned by this request
92+
* @param url URL to query
93+
* @param body Query object as defined by the Query interface
94+
* @return A TspClientResponse object
95+
*/
7496
public static async post<T>(url: string, body?: any): Promise<TspClientResponse<T>> {
7597
return this.performRequest<T>('post', url, body);
7698
}
7799

100+
/**
101+
* Perform PUT
102+
* T is the expected type of the json object returned by this request
103+
* @param url URL to query
104+
* @param body Query object as defined by the Query interface
105+
* @return A TspClientResponse object
106+
*/
78107
public static async put<T>(url: string, body?: any): Promise<TspClientResponse<T>> {
79108
return this.performRequest<T>('put', url, body);
80109
}
81110

111+
/**
112+
* Perform DELETE
113+
* T is the expected type of the json object returned by this request
114+
* @param url URL to query without query parameters
115+
* @param parameters Query parameters. Mapped keys and values are use to build the final URL
116+
* @return A TspClientResponse object
117+
*/
82118
public static async delete<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<T>> {
83119
let deleteUrl = url;
84120
if (parameters) {
@@ -88,6 +124,11 @@ export class RestClient {
88124
return this.performRequest<T>('delete', deleteUrl);
89125
}
90126

127+
/**
128+
* Encode URL parameters
129+
* @param parameters Query parameters. Mapped keys and values are used to build the final URL
130+
* @return Encoded string
131+
*/
91132
private static encodeURLParameters(parameters: Map<string, string>): string {
92133
if (parameters.size) {
93134
const urlParameters: string = '?';
@@ -100,6 +141,11 @@ export class RestClient {
100141
return '';
101142
}
102143

144+
/**
145+
* Extract parameters
146+
* @param url URL string
147+
* @return String containing the parameters extracted from the url
148+
*/
103149
public static extractParameters(url: string): string {
104150
const topic = url.includes('traces') ? 'traces' : 'experiments';
105151
const ar = url.split(topic + '/');

src/protocol/rest-client.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export class RestClient {
1515
'Content-Type': 'application/json'
1616
},
1717
method: verb,
18-
body: jsonBody});
18+
body: jsonBody
19+
});
1920
const text = await response.text();
2021
return new TspClientResponse(text, response.status, response.statusText);
2122
}
@@ -24,7 +25,7 @@ export class RestClient {
2425
* Perform GET
2526
* T is the expected type of the json object returned by this request
2627
* @param url URL to query without query parameters
27-
* @param parameters Query parameters. Map keys and values are used to build the final URL
28+
* @param parameters Query parameters. Mapped keys and values are used to build the final URL
2829
*/
2930
public static async get<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<T>> {
3031
let getUrl = url;
@@ -59,7 +60,7 @@ export class RestClient {
5960
* Perform DELETE
6061
* T is the expected type of the json object returned by this request
6162
* @param url URL to query without query parameters
62-
* @param parameters Query parameters. Map keys and values are used to build the final URL
63+
* @param parameters Query parameters. Mapped keys and values are used to build the final URL
6364
*/
6465
public static async delete<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<T>> {
6566
let deleteUrl = url;

0 commit comments

Comments
 (0)