-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction.spec.js
151 lines (123 loc) · 4.64 KB
/
function.spec.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
jest.mock('https');
const func = require('./function');
const generateDropboxUrl = func.generateDropboxUrl;
const respondWithError = func.respondWithError;
const upstreamResponseHandler = func.upstreamResponseHandler;
test('default with dropbox url', () => {
const context = { done: jest.fn() };
const request = {
query: {
dropboxUrl: 'https://www.dropbox.com/s/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0'
}
};
const result = {
headers: {
location: 'https://uc062dde02acb0618d3ba6b67ce3.dl.dropboxusercontent.com/cd/0/inline/ARdB4wHUPicq44yKqb9hajAm7XNgMo9EUy8qZgfC4_lVoKmW--Kqf_NeIlGAqcHLViCOJsEFrKN0aGZvQ4UbyyZ0AZ0AUL_uhXw5opFBKMiSAz-523O8S_HpFlibkApxL2Db8NbBsHcONl2hp_712Bx-1BJt0_-4Sbbbh9AJ5Ma2D1qv42i499E59c0DRm1vd08/file'
},
statusCode: 302,
statusMessage: 'Found'
};
require('https').get = (url, handler) => {
expect(url).toBe('https://www.dropbox.com/s/raw/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0');
handler(result);
};
func(context, request);
expect(context.res).toStrictEqual({
body: null,
headers: {
'access-control-allow-origin': '*',
location: 'https://uc062dde02acb0618d3ba6b67ce3.dl.dropboxusercontent.com/cd/0/inline/ARdB4wHUPicq44yKqb9hajAm7XNgMo9EUy8qZgfC4_lVoKmW--Kqf_NeIlGAqcHLViCOJsEFrKN0aGZvQ4UbyyZ0AZ0AUL_uhXw5opFBKMiSAz-523O8S_HpFlibkApxL2Db8NbBsHcONl2hp_712Bx-1BJt0_-4Sbbbh9AJ5Ma2D1qv42i499E59c0DRm1vd08/file'
},
status: 302
});
expect(context.done.mock.calls.length).toBe(1);
});
test('default without dropbox url', () => {
const context = { done: jest.fn() };
const request = { query: { } };
func(context, request);
expect(context.res).toStrictEqual({
body: { error: 'Forbidden' },
status: 403
});
expect(context.done.mock.calls.length).toBe(1);
});
test('default with non-dropbox url', () => {
const context = { done: jest.fn() };
const request = {
query: {
dropboxUrl: 'https://www.example.com/s/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0'
}
};
func(context, request);
expect(context.res).toStrictEqual({
body: { error: 'Forbidden' },
status: 403
});
expect(context.done.mock.calls.length).toBe(1);
});
test('default with non-HTTPS url', () => {
const context = { done: jest.fn() };
const request = {
query: {
dropboxUrl: 'http://www.dropbox.com/s/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0'
}
};
func(context, request);
expect(context.res).toStrictEqual({
body: { error: 'Forbidden' },
status: 403
});
expect(context.done.mock.calls.length).toBe(1);
});
test('generateDropboxUrl with dropbox url', () => {
const result = generateDropboxUrl('https://www.dropbox.com/s/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0');
expect(result).toBe('https://www.dropbox.com/s/raw/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0');
});
test('generateDropboxUrl with non-dropbox url', () => {
expect(() => {
generateDropboxUrl('https://www.example.com/s/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0');
}).toThrow('Forbidden');
});
test('generateDropboxUrl with non-HTTPS url', () => {
expect(() => {
generateDropboxUrl('http://www.dropbox.com/s/4ef30lt0c51o1uf/CalabiYau5.jpg?dl=0');
}).toThrow('Forbidden');
});
test('respondWithError', () => {
const context = { done: jest.fn() };
respondWithError(context, 403, 'Forbidden');
expect(context.res).toStrictEqual({
body: { error: 'Forbidden' },
status: 403
});
expect(context.done.mock.calls.length).toBe(1);
});
test('upstreamResponseHandler with found result', () => {
const context = { done: jest.fn() };
const result = {
headers: {
location: 'https://uc062dde02acb0618d3ba6b67ce3.dl.dropboxusercontent.com/cd/0/inline/ARdB4wHUPicq44yKqb9hajAm7XNgMo9EUy8qZgfC4_lVoKmW--Kqf_NeIlGAqcHLViCOJsEFrKN0aGZvQ4UbyyZ0AZ0AUL_uhXw5opFBKMiSAz-523O8S_HpFlibkApxL2Db8NbBsHcONl2hp_712Bx-1BJt0_-4Sbbbh9AJ5Ma2D1qv42i499E59c0DRm1vd08/file'
},
statusCode: 302,
statusMessage: 'Found'
};
upstreamResponseHandler(context)(result);
expect(context.res).toStrictEqual({
body: null,
headers: {
'access-control-allow-origin': '*',
location: 'https://uc062dde02acb0618d3ba6b67ce3.dl.dropboxusercontent.com/cd/0/inline/ARdB4wHUPicq44yKqb9hajAm7XNgMo9EUy8qZgfC4_lVoKmW--Kqf_NeIlGAqcHLViCOJsEFrKN0aGZvQ4UbyyZ0AZ0AUL_uhXw5opFBKMiSAz-523O8S_HpFlibkApxL2Db8NbBsHcONl2hp_712Bx-1BJt0_-4Sbbbh9AJ5Ma2D1qv42i499E59c0DRm1vd08/file'
},
status: 302
});
expect(context.done.mock.calls.length).toBe(1);
});
test('upstreamResponseHandler with not found result', () => {
const context = { done: jest.fn() };
const result = {
statusCode: 404,
statusMessage: 'Not Found'
};
expect(() => upstreamResponseHandler(context)(result)).toThrow('Not Found');
});