-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapiService.test.js
More file actions
80 lines (69 loc) · 2.33 KB
/
apiService.test.js
File metadata and controls
80 lines (69 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const apiService = require('./apiService');
const { ConnectionError, NotFoundError} = require('./exception');
describe('apiCall', () => {
it('returns a successful response for a GET request', async () => {
// Arrange
const http_method = 'GET';
const api_endpoint = 'gtt_by_instruction_id';
const expected_url = 'http://example.com/example_endpoint';
const expected_response = { message: 'success' };
// Mock the request library
jest.mock('request', () => {
return jest.fn((options, callback) => {
expect(options.method).toBe(http_method);
expect(options.url).toBe(expected_url);
callback(null, { statusCode: 200 }, JSON.stringify(expected_response));
});
});
await expect(() => apiService.apiCall(api_endpoint, http_method)).toThrow(("Token is invalid"));
});
it('throws a ConnectionError for a 401 response', async () => {
// Arrange
const http_method = 'POST';
const api_endpoint = 'example_endpoint';
const expected_url = 'http://example.com/example_endpoint';
const expected_error = { message: 'Unauthorized' };
// Mock the request library
jest.mock('request', () => {
return jest.fn((options, callback) => {
expect(options.method).toBe(http_method);
expect(options.url).toBe(expected_url);
callback(null, { statusCode: 401 }, JSON.stringify(expected_error));
});
});
expect(() => apiService.apiCall(api_endpoint, http_method)).toThrowError(NotFoundError);
});
test("api_call_path_param", () => {
var path_params = {
'id' : 234
}
expect(() => apiService.apiCall(
api='gtt_by_instruction_id',
http_method='GET',
payload=null,
parmas=null,
path_params=path_params
)).toThrowError(NotFoundError);
});
test("api_call_query_param", () => {
this.public_access_token = "public";
var params = {
'source':'source',
'exchange':'exchange',
'segment':'segment',
'security_id':'security_id',
'txn_type':'txn_type',
'quantity':'quantity',
'price':'price',
'product':'product',
'trigger_price':'trigger_price'
}
expect(() => apiService.apiCall(
api='order_margin',
http_method='GET',
payload=null,
parmas=params,
path_params=null
)).toThrowError(NotFoundError);
});
});